king_placeholder 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,7 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.version = KingPlaceholder::VERSION
17
17
 
18
18
  gem.add_runtime_dependency 'statemachine'
19
- gem.add_runtime_dependency 'i18n'
20
19
  gem.add_runtime_dependency 'activesupport'
21
20
 
22
21
  gem.add_development_dependency 'activerecord'
@@ -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, :cleanup
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
@@ -1,3 +1,3 @@
1
1
  module KingPlaceholder
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
@@ -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
- # sets :placeholders and init Class.placeholders array on inclusion
13
- def self.included(base)
13
+ included do
14
14
  if ActiveSupport::VERSION::MAJOR == 3 && ActiveSupport::VERSION::MINOR > 0
15
- base.class_attribute :placeholders
15
+ class_attribute :placeholders
16
16
  else
17
- base.send :class_inheritable_accessor, :placeholders
17
+ class_inheritable_accessor :placeholders
18
18
  end
19
- base.placeholders = []
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 gets the current parsed object as method argument
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
Binary file
@@ -1,131 +1,131 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  # Construct dummy classes
4
- class Master
4
+ class Company
5
5
  include KingPlaceholder
6
- attr_accessor :string_field
7
- attr_accessor :details
8
- attr_accessor :side
9
- has_placeholders :string_field
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 Side
12
+ class User
13
13
  include KingPlaceholder
14
- attr_accessor :field
15
- attr_accessor :master
16
- has_placeholders :field
14
+ attr_accessor :email
15
+ attr_accessor :company
16
+ has_placeholders :email
17
17
  end
18
18
 
19
- class Detail
19
+ class Client
20
20
  include KingPlaceholder
21
- attr_accessor :int_field, :money_field, :secret_field, :currency
22
- attr_accessor :master
23
- has_placeholders :int_field, :money_field
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
- @record = Detail.new
30
- @record.int_field = 1002
31
- @record.money_field = 12.333
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
- @record.int_field.should == 1002
36
- @record.money_field.should == 12.333
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
- @record.expand_placeholders('[int_field]').should == '1002'
41
- @record.expand_placeholders('[money_field]').should == '12.333'
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
- @record.expand_placeholders(['[int_field]', '[money_field]', 'static']).should == ['1002', '12.333', 'static']
45
- end
46
-
47
- it 'should expand placeholders in a hash' do
48
- @record.expand_placeholders( :key1 => '[int_field]',
49
- :key2 => '[money_field]',
50
- :key3 => 'static'
51
- ).should ==
52
- { :key1 => '1002',
53
- :key2 => '12.333',
54
- :key3 => 'static' }
55
- end
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
- @side = Side.new
63
- @side.field = 123
64
- @master = Master.new
65
- @master.string_field = 'foo'
66
- @master.side = @side
67
- @side.master = @master
68
-
69
- @detail1 = Detail.new
70
- @detail1.int_field = 1001
71
- @detail1.money_field = 12.34
72
- @detail1.secret_field = 'top-secret'
73
- @detail1.master = @master
74
-
75
- @detail2 = Detail.new
76
- @detail2.int_field = 1002
77
- @detail2.money_field = 45.67
78
- @detail2.secret_field = 'little secret'
79
- @detail2.master = @master
80
- @master.details = [@detail1, @detail2]
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
- @detail1.expand_placeholders('without placeholder').should == 'without placeholder'
87
- @detail1.expand_placeholders('[]').should == '[]'
88
- @detail1.expand_placeholders('').should == ''
89
- @detail1.expand_placeholders(nil).should == nil
90
- @detail1.expand_placeholders("\n").should == "\n"
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
- @detail1.expand_placeholders('[int_field]').should == '1001'
95
- @detail1.expand_placeholders("[int_field]\n").should == "1001\n"
96
- @detail1.expand_placeholders('[int_field]---[int_field]--[money_field]').should == '1001---1001--12.34'
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
- @detail1.expand_placeholders('[secret_field]').should == 'UNKNOWN for Detail: secret_field'
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
- @detail1.expand_placeholders('[nothing.string_field]').should == 'UNKNOWN for Detail: nothing.string_field'
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
- @detail1.expand_placeholders('[master.this.are.too.much.namespaces]').should == 'UNKNOWN for Master: this.are.too.much.namespaces'
109
- @detail1.expand_placeholders('[this.are.too.much.namespaces]').should == 'UNKNOWN for Detail: this.are.too.much.namespaces'
110
- @detail1.expand_placeholders('[...]').should == 'UNKNOWN for Detail: ...'
111
- @detail1.expand_placeholders('[unknown]').should == 'UNKNOWN for Detail: unknown'
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
- @detail1.expand_placeholders('[master.string_field]').should == 'foo'
119
- @detail1.expand_placeholders('[master.string_field] [int_field]').should == 'foo 1001'
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
- @detail1.expand_placeholders('[detail.int_field]').should == '1001'
124
- @detail1.expand_placeholders('[detail.master.string_field] [detail.int_field]').should == 'foo 1001'
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
- @detail1.expand_placeholders('[master.side.field]').should == '123'
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
- master = Master.new
137
- master.details = []
138
- master.expand_placeholders('[details][int_field][/details]').should == ''
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
- @master.expand_placeholders('[details.10.int_field]').should == ''
142
+ @company.expand_placeholders('[clients.10.number]').should == ''
143
143
  end
144
144
 
145
- it 'should parse related object field with empty string' do
146
- @master.string_field = nil
147
- @detail1.expand_placeholders('[master.string_field]').should == ''
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
- @detail1.master = nil
152
- @detail1.expand_placeholders('[master.string_field]').should == ''
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
- @master.expand_placeholders('[details][int_field]\n[/details]').should == '1001\n1002\n'
159
- @master.expand_placeholders('[details]Test:[int_field][/details]').should == 'Test:1001Test:1002'
160
- @master.expand_placeholders("[details][int_field]\n[/details]").should == "1001\n1002\n"
161
- @master.expand_placeholders('[details][foo][/details]').should == 'UNKNOWN for Detail: fooUNKNOWN for Detail: foo'
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
- @master.expand_placeholders('[details.1.int_field]').should == '1001'
166
- @master.expand_placeholders('[details.2.int_field]').should == '1002'
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
- @master.expand_placeholders('[details][int_field]').should == 'END MISSING FOR detailsUNKNOWN for Master: int_field'
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 = :en_master
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
- @record.expand_placeholders('[money_field]').should == '$12.34'
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.2
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-07-31 00:00:00.000000000 Z
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: -1074118578263294842
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: -1074118578263294842
169
+ hash: 961112527656133584
186
170
  requirements: []
187
171
  rubyforge_project:
188
172
  rubygems_version: 1.8.24