king_placeholder 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/king_placeholder.gemspec +0 -1
- data/lib/king_placeholder/parser.rb +1 -5
- data/lib/king_placeholder/version.rb +1 -1
- data/lib/king_placeholder.rb +9 -10
- data/placeholder_state.dia +0 -0
- data/spec/king_placeholder_spec.rb +88 -88
- metadata +4 -20
data/king_placeholder.gemspec
CHANGED
@@ -14,7 +14,7 @@ module KingPlaceholder
|
|
14
14
|
state :matching do
|
15
15
|
on_entry :parse
|
16
16
|
end
|
17
|
-
trans :matching, :finished_matching, :finished
|
17
|
+
trans :matching, :finished_matching, :finished
|
18
18
|
context machine_context
|
19
19
|
end
|
20
20
|
end
|
@@ -79,10 +79,6 @@ module KingPlaceholder
|
|
79
79
|
@sm.finished_matching
|
80
80
|
end
|
81
81
|
|
82
|
-
def cleanup
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
82
|
private
|
87
83
|
|
88
84
|
# Final destination of each placeholder in it's simple notation without
|
data/lib/king_placeholder.rb
CHANGED
@@ -6,18 +6,17 @@ require 'active_support/version'
|
|
6
6
|
# A Placeholder can be used inside any text string and will be replaced with a
|
7
7
|
# stringified, formatted value(by KingViews gem => KingFormat::FormattingHelper.strfval )
|
8
8
|
# Used for text snippets, PDF creation or E-Mail templates.
|
9
|
-
|
10
9
|
module KingPlaceholder
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
include ActiveSupport::Callbacks
|
11
12
|
|
12
|
-
|
13
|
-
def self.included(base)
|
13
|
+
included do
|
14
14
|
if ActiveSupport::VERSION::MAJOR == 3 && ActiveSupport::VERSION::MINOR > 0
|
15
|
-
|
15
|
+
class_attribute :placeholders
|
16
16
|
else
|
17
|
-
|
17
|
+
class_inheritable_accessor :placeholders
|
18
18
|
end
|
19
|
-
|
20
|
-
base.extend(ClassMethods)
|
19
|
+
placeholders = []
|
21
20
|
end
|
22
21
|
|
23
22
|
module ClassMethods
|
@@ -26,10 +25,11 @@ module KingPlaceholder
|
|
26
25
|
# `before/after_expand_placeholders` hooks are run before the statemachine
|
27
26
|
# parsing. Define those methods to setup env variables like I18n.locale or
|
28
27
|
# whatever is required to format output strings.
|
29
|
-
# The block
|
28
|
+
# The block is called in scope of the current object(self).
|
30
29
|
# @example
|
31
30
|
# def before_expand_placeholders
|
32
31
|
# I18n.locale = self.language
|
32
|
+
# # self == current object
|
33
33
|
# end
|
34
34
|
# def after_expand_placeholders
|
35
35
|
# I18n.locale = nil
|
@@ -38,14 +38,13 @@ module KingPlaceholder
|
|
38
38
|
# @param [Array[<Symbol>] fieldnames
|
39
39
|
def has_placeholders(*fieldnames)
|
40
40
|
self.placeholders = fieldnames
|
41
|
-
include ActiveSupport::Callbacks
|
42
41
|
define_callbacks :expand_placeholders
|
43
42
|
set_callback :expand_placeholders, :before, :before_parsing
|
44
43
|
set_callback :expand_placeholders, :after, :after_parsing
|
45
|
-
include InstanceMethods
|
46
44
|
end
|
47
45
|
end
|
48
46
|
|
47
|
+
# TODO inclusion deprecated in ActiveSupport 3.2.7, when gone move methods up into module
|
49
48
|
module InstanceMethods
|
50
49
|
|
51
50
|
# Check if a given field is declared as placeholder
|
data/placeholder_state.dia
CHANGED
Binary file
|
@@ -1,131 +1,131 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
# Construct dummy classes
|
4
|
-
class
|
4
|
+
class Company
|
5
5
|
include KingPlaceholder
|
6
|
-
attr_accessor :
|
7
|
-
attr_accessor :
|
8
|
-
attr_accessor :
|
9
|
-
has_placeholders :
|
6
|
+
attr_accessor :name
|
7
|
+
attr_accessor :clients # has_many
|
8
|
+
attr_accessor :user # belongs_to
|
9
|
+
has_placeholders :name
|
10
10
|
end
|
11
11
|
|
12
|
-
class
|
12
|
+
class User
|
13
13
|
include KingPlaceholder
|
14
|
-
attr_accessor :
|
15
|
-
attr_accessor :
|
16
|
-
has_placeholders :
|
14
|
+
attr_accessor :email
|
15
|
+
attr_accessor :company
|
16
|
+
has_placeholders :email
|
17
17
|
end
|
18
18
|
|
19
|
-
class
|
19
|
+
class Client
|
20
20
|
include KingPlaceholder
|
21
|
-
attr_accessor :
|
22
|
-
attr_accessor :
|
23
|
-
has_placeholders :
|
21
|
+
attr_accessor :number, :money_field, :secret_field, :currency
|
22
|
+
attr_accessor :company
|
23
|
+
has_placeholders :number, :money_field
|
24
24
|
end
|
25
25
|
|
26
26
|
describe 'Class with placeholders' do
|
27
27
|
|
28
28
|
before :each do
|
29
|
-
@
|
30
|
-
@
|
31
|
-
@
|
29
|
+
@client = Client.new
|
30
|
+
@client.number = 1002
|
31
|
+
@client.money_field = 12.333
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'should have native values' do
|
35
|
-
@
|
36
|
-
@
|
35
|
+
@client.number.should == 1002
|
36
|
+
@client.money_field.should == 12.333
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'should have placeholder value' do
|
40
|
-
@
|
41
|
-
@
|
40
|
+
@client.expand_placeholders('[number]').should == '1002'
|
41
|
+
@client.expand_placeholders('[money_field]').should == '12.333'
|
42
42
|
end
|
43
43
|
it 'should expand placeholders in an array' do
|
44
|
-
@
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
44
|
+
@client.expand_placeholders(['[number]', '[money_field]', 'static']).should == ['1002', '12.333', 'static']
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should expand placeholders in a hash' do
|
48
|
+
@client.expand_placeholders( :key1 => '[number]',
|
49
|
+
:key2 => '[money_field]',
|
50
|
+
:key3 => 'static'
|
51
|
+
).should ==
|
52
|
+
{ :key1 => '1002',
|
53
|
+
:key2 => '12.333',
|
54
|
+
:key3 => 'static' }
|
55
|
+
end
|
56
56
|
end
|
57
57
|
|
58
58
|
describe 'Placeholder substitution' do
|
59
59
|
|
60
60
|
before :each do
|
61
61
|
I18n.locale = :en
|
62
|
-
@
|
63
|
-
@
|
64
|
-
@
|
65
|
-
@
|
66
|
-
@
|
67
|
-
@
|
68
|
-
|
69
|
-
@
|
70
|
-
@
|
71
|
-
@
|
72
|
-
@
|
73
|
-
@
|
74
|
-
|
75
|
-
@
|
76
|
-
@
|
77
|
-
@
|
78
|
-
@
|
79
|
-
@
|
80
|
-
@
|
62
|
+
@user = User.new
|
63
|
+
@user.email = 'a@b.com'
|
64
|
+
@company = Company.new
|
65
|
+
@company.name = 'BigMoney Inc.'
|
66
|
+
@company.user = @user
|
67
|
+
@user.company = @company
|
68
|
+
|
69
|
+
@client = Client.new
|
70
|
+
@client.number = 1001
|
71
|
+
@client.money_field = 12.34
|
72
|
+
@client.secret_field = 'top-secret'
|
73
|
+
@client.company = @company
|
74
|
+
|
75
|
+
@client_1 = Client.new
|
76
|
+
@client_1.number = 1002
|
77
|
+
@client_1.money_field = 45.67
|
78
|
+
@client_1.secret_field = 'little secret'
|
79
|
+
@client_1.company = @company
|
80
|
+
@company.clients = [@client, @client_1]
|
81
81
|
end
|
82
82
|
|
83
83
|
context 'with direct lookup' do
|
84
84
|
|
85
85
|
it 'should return without placeholders' do
|
86
|
-
@
|
87
|
-
@
|
88
|
-
@
|
89
|
-
@
|
90
|
-
@
|
86
|
+
@client.expand_placeholders('without placeholder').should == 'without placeholder'
|
87
|
+
@client.expand_placeholders('[]').should == '[]'
|
88
|
+
@client.expand_placeholders('').should == ''
|
89
|
+
@client.expand_placeholders(nil).should == nil
|
90
|
+
@client.expand_placeholders("\n").should == "\n"
|
91
91
|
end
|
92
92
|
|
93
93
|
it 'should expand with simple fieldname' do
|
94
|
-
@
|
95
|
-
@
|
96
|
-
@
|
94
|
+
@client.expand_placeholders('[number]').should == '1001'
|
95
|
+
@client.expand_placeholders("[number]\n").should == "1001\n"
|
96
|
+
@client.expand_placeholders('[number]---[number]--[money_field]').should == '1001---1001--12.34'
|
97
97
|
end
|
98
98
|
|
99
99
|
it 'should not parse unknown field' do
|
100
|
-
@
|
100
|
+
@client.expand_placeholders('[secret_field]').should == 'UNKNOWN for Client: secret_field'
|
101
101
|
end
|
102
102
|
|
103
103
|
it 'should expand placeholder with not existing namespaces' do
|
104
|
-
@
|
104
|
+
@client.expand_placeholders('[nothing.name]').should == 'UNKNOWN for Client: nothing.name'
|
105
105
|
end
|
106
106
|
|
107
107
|
it 'should expand placeholder with wrong namespaces' do
|
108
|
-
@
|
109
|
-
@
|
110
|
-
@
|
111
|
-
@
|
108
|
+
@client.expand_placeholders('[company.this.are.too.much.namespaces]').should == 'UNKNOWN for Company: this.are.too.much.namespaces'
|
109
|
+
@client.expand_placeholders('[this.are.too.much.namespaces]').should == 'UNKNOWN for Client: this.are.too.much.namespaces'
|
110
|
+
@client.expand_placeholders('[...]').should == 'UNKNOWN for Client: ...'
|
111
|
+
@client.expand_placeholders('[unknown]').should == 'UNKNOWN for Client: unknown'
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
115
115
|
context 'with namespace' do
|
116
116
|
|
117
117
|
it 'should parse referenced object fields' do
|
118
|
-
@
|
119
|
-
@
|
118
|
+
@client.expand_placeholders('[company.name]').should == 'BigMoney Inc.'
|
119
|
+
@client.expand_placeholders('[company.name] [number]').should == 'BigMoney Inc. 1001'
|
120
120
|
end
|
121
121
|
|
122
122
|
it 'should parse self referenced field' do
|
123
|
-
@
|
124
|
-
@
|
123
|
+
@client.expand_placeholders('[client.number]').should == '1001'
|
124
|
+
@client.expand_placeholders('[client.company.name] [client.number]').should == 'BigMoney Inc. 1001'
|
125
125
|
end
|
126
126
|
|
127
127
|
it 'should parse multiple steps' do
|
128
|
-
@
|
128
|
+
@client.expand_placeholders('[company.user.email]').should == 'a@b.com'
|
129
129
|
end
|
130
130
|
|
131
131
|
end
|
@@ -133,52 +133,52 @@ describe 'Placeholder substitution' do
|
|
133
133
|
context 'with empty fields' do
|
134
134
|
|
135
135
|
it 'should parse valid but empty placeholder group with empty string' do
|
136
|
-
|
137
|
-
|
138
|
-
|
136
|
+
company = Company.new
|
137
|
+
company.clients = []
|
138
|
+
company.expand_placeholders('[clients][number][/clients]').should == ''
|
139
139
|
end
|
140
140
|
|
141
141
|
it 'should parse single item group with empty string' do
|
142
|
-
@
|
142
|
+
@company.expand_placeholders('[clients.10.number]').should == ''
|
143
143
|
end
|
144
144
|
|
145
|
-
it 'should parse related object
|
146
|
-
@
|
147
|
-
@
|
145
|
+
it 'should parse related object email with empty string' do
|
146
|
+
@company.name = nil
|
147
|
+
@client.expand_placeholders('[company.name]').should == ''
|
148
148
|
end
|
149
149
|
|
150
150
|
it 'should parse empty related object with empty string' do
|
151
|
-
@
|
152
|
-
@
|
151
|
+
@client.company = nil
|
152
|
+
@client.expand_placeholders('[company.name]').should == ''
|
153
153
|
end
|
154
154
|
end
|
155
155
|
|
156
156
|
context 'with collection' do
|
157
157
|
it 'should expand' do
|
158
|
-
@
|
159
|
-
@
|
160
|
-
@
|
161
|
-
@
|
158
|
+
@company.expand_placeholders('[clients][number]\n[/clients]').should == '1001\n1002\n'
|
159
|
+
@company.expand_placeholders('[clients]Test:[number][/clients]').should == 'Test:1001Test:1002'
|
160
|
+
@company.expand_placeholders("[clients][number]\n[/clients]").should == "1001\n1002\n"
|
161
|
+
@company.expand_placeholders('[clients][foo][/clients]').should == 'UNKNOWN for Client: fooUNKNOWN for Client: foo'
|
162
162
|
end
|
163
163
|
|
164
164
|
it 'should expand single item ' do
|
165
|
-
@
|
166
|
-
@
|
165
|
+
@company.expand_placeholders('[clients.1.number]').should == '1001'
|
166
|
+
@company.expand_placeholders('[clients.2.number]').should == '1002'
|
167
167
|
end
|
168
168
|
|
169
169
|
it 'should show error for missing closing' do
|
170
|
-
@
|
170
|
+
@company.expand_placeholders('[clients][number]').should == 'END MISSING FOR clientsUNKNOWN for Company: number'
|
171
171
|
end
|
172
172
|
end
|
173
173
|
|
174
174
|
context 'with custom formatter' do
|
175
175
|
before :each do
|
176
|
-
I18n.locale = :
|
176
|
+
I18n.locale = :en_company
|
177
177
|
# Thread.current[:default_currency_format] = I18n.t(:'number.currency.format')
|
178
178
|
end
|
179
179
|
|
180
180
|
xit 'should format money' do
|
181
|
-
@
|
181
|
+
@client.expand_placeholders('[money_field]').should == '$12.34'
|
182
182
|
end
|
183
183
|
end
|
184
184
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: king_placeholder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: statemachine
|
@@ -27,22 +27,6 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: i18n
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
|
-
requirements:
|
35
|
-
- - ! '>='
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
version: '0'
|
38
|
-
type: :runtime
|
39
|
-
prerelease: false
|
40
|
-
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
|
-
requirements:
|
43
|
-
- - ! '>='
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: '0'
|
46
30
|
- !ruby/object:Gem::Dependency
|
47
31
|
name: activesupport
|
48
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -173,7 +157,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
173
157
|
version: '0'
|
174
158
|
segments:
|
175
159
|
- 0
|
176
|
-
hash:
|
160
|
+
hash: 961112527656133584
|
177
161
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
178
162
|
none: false
|
179
163
|
requirements:
|
@@ -182,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
182
166
|
version: '0'
|
183
167
|
segments:
|
184
168
|
- 0
|
185
|
-
hash:
|
169
|
+
hash: 961112527656133584
|
186
170
|
requirements: []
|
187
171
|
rubyforge_project:
|
188
172
|
rubygems_version: 1.8.24
|