dynamic_attribute_declaration 0.0.11 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b76e3142d2eed5bc086805161e9bc55595999f3
4
- data.tar.gz: da66b9d1a260a72a7ef735d1eb8453334a1df032
3
+ metadata.gz: a98ea1bafab32de11aa18aa7e7cd6eaa5f797a1c
4
+ data.tar.gz: 8f3e53ed329c029bb37ee7da35c0223f18cd401d
5
5
  SHA512:
6
- metadata.gz: 7be553e1972b3c823bc46e808f70474d88cddf96d86a421842577c0d47634db2307202ecdc6e3e0cfff0ef1003d183647abd660c6bd9d54e91c1b6b63db44c6d
7
- data.tar.gz: 1c61921ef47b63b42135b87be4f54eb1d88cb30d1ac79f980df6812edb0470124723a5590610d04dc212e90f70ae62fdaade207b61a7d2a751574e9ccaf02467
6
+ metadata.gz: 168d33f7e90d7da8680b61455d25b3bfd9d12dfcc05616723ff84abcb17c2ceee14e4336f3ac045798a365933e64ebb341b832be17334de88c4debc80d6e7288
7
+ data.tar.gz: 8710a7fd7e50b084215a03bd8093f757b026c86b2f9c35d6890a3735619d4e36d819d7f73c77ce887f0c6f293f07e1169bb7573db9bf942bbb8f2538dde35249
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard :rspec, cmd: 'rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.6"
22
22
  spec.add_development_dependency "rake", "~> 10"
23
23
  spec.add_development_dependency "rspec", "~> 3"
24
+ spec.add_development_dependency "guard-rspec", "~> 4.2"
24
25
  spec.add_development_dependency "rspec-its", "~> 1"
25
26
  spec.add_development_dependency "sqlite3", "~> 1.3"
26
27
  spec.add_development_dependency "database_cleaner", "~> 1.3"
@@ -33,24 +33,41 @@ module DynamicAttributeDeclaration
33
33
  self._dynamic_attr_state_if = proc
34
34
  end
35
35
 
36
- def define_attrs *args
37
- attrs = Hash[*args.flatten]
38
- self._dynamic_attrs.merge! attrs
39
- build_validations_from_dynamic_attrs attrs
36
+ def define_attrs args
37
+ add_dynamic_attrs args
38
+ build_validations_from_dynamic_attrs args
40
39
  end
41
40
 
42
- def attrs_for state=nil, device=nil
43
- if state
44
- device ||= :desktop
45
- _dynamic_attrs.select do |key, val|
46
- if val.class == Symbol
47
- comparer = val
48
- elsif val.respond_to?(:key) && val.key?(:on)
49
- comparer = val[:on] if val[:on].class == Symbol
50
- comparer = val[:on][device.to_sym] if val[:on].respond_to?(:key) && val[:on].key?(device.to_sym)
41
+ def attrs_on_for attr_name
42
+ attr = _dynamic_attrs[attr_name]
43
+ rtn = {}
44
+ attr.each do |a|
45
+ if a.key? :on
46
+ obj = a[:on]
47
+ obj.each do |key, value|
48
+ if rtn.key? key
49
+ rtn[key].push(value).flatten!
50
+ else
51
+ rtn[key] = value
52
+ end
51
53
  end
54
+ end
55
+ end
56
+ rtn
57
+ end
58
+
59
+ def attrs_for state=nil, device=nil
60
+ if state && state.to_sym != :all
61
+ state = state.to_sym
62
+ device = device ? device.to_sym : nil
63
+ devices = [:all, device].compact.uniq
52
64
 
53
- [*comparer].map(&:to_sym).include? state.to_sym
65
+ _dynamic_attrs.select do |attr_name, definitions|
66
+ on_attrs = attrs_on_for(attr_name)
67
+ if on_attrs
68
+ comparer = (devices & on_attrs.keys).map { |k| on_attrs[k] }.flatten.uniq
69
+ end
70
+ comparer.map(&:to_sym).include? state.to_sym
54
71
  end
55
72
  else
56
73
  _dynamic_attrs
@@ -63,13 +80,14 @@ module DynamicAttributeDeclaration
63
80
 
64
81
  def build_validations_from_dynamic_attrs attrs
65
82
  # throw "No validation state if defined" unless _rdynamic_attr_state_if
66
- attrs.each do |key, val|
67
- if val.key?(:validates) && !val[:validates].empty?
68
- opts = val[:validates].deep_symbolize_keys
83
+ attrs.each do |attr|
84
+ key, value = *attr.flatten
85
+ if value.key?(:validates) && !value[:validates].empty?
86
+ opts = value[:validates].deep_symbolize_keys
69
87
 
70
88
  # Check if validation should only be used in specific state
71
- if val.key?(:on) && _dynamic_attr_state_if && _dynamic_attr_state_if.class == Proc
72
- validates_on = val[:on]
89
+ if value.key?(:on) && _dynamic_attr_state_if && _dynamic_attr_state_if.class == Proc
90
+ validates_on = value[:on]
73
91
  # If validates contains if statement, wrap that statement in state check
74
92
  if opts.key?(:if)
75
93
  original_if = opts.delete(:if)
@@ -83,6 +101,40 @@ module DynamicAttributeDeclaration
83
101
  end
84
102
  end
85
103
  end
104
+
105
+ def add_dynamic_attrs attrs
106
+ attrs.each do |attr|
107
+ key, value = *attr.flatten
108
+ if self._dynamic_attrs.keys.include? key
109
+ self._dynamic_attrs[key].push process_attr(value)
110
+ else
111
+ self._dynamic_attrs[key] = [process_attr(value)]
112
+ end
113
+ end
114
+ end
115
+
116
+ def process_attr attr
117
+ rtn = {}
118
+ attr.each do |key, value|
119
+ if key == :on
120
+ case value
121
+ when Symbol
122
+ new_value = { all: [value] }
123
+ when Array
124
+ new_value = { all: value }
125
+ when Hash
126
+ new_value = {}
127
+ value.each { |k,v| new_value[k] = *v }
128
+ else
129
+ throw "Process attr #{value} cannot be of class #{value.class}"
130
+ end
131
+ else
132
+ new_value = value
133
+ end
134
+ rtn[key] = new_value
135
+ end
136
+ rtn
137
+ end
86
138
  end
87
139
  end
88
140
 
@@ -1,3 +1,3 @@
1
1
  module DynamicAttributeDeclaration
2
- VERSION = "0.0.11"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -4,6 +4,11 @@ describe "BASE TESTS" do
4
4
 
5
5
  let(:cls) { Phone }
6
6
 
7
+ before do
8
+ cls.clear_validators!
9
+ cls.clear_dynamic_attrs!
10
+ end
11
+
7
12
  describe "Basic gem tests" do
8
13
 
9
14
  it "Accessor _dynamic_attrs should be Hash" do
@@ -23,49 +28,176 @@ describe "BASE TESTS" do
23
28
  expect(cls.validators).to eq []
24
29
  end
25
30
 
26
- describe "Simple definition of presence validator" do
27
- let(:defition_array) { [{name:{validates:{presence: true}}}] }
28
- let(:definition) { Hash[*defition_array.flatten] }
31
+ describe "Simple definition of on" do
32
+
33
+ types = {
34
+ "on a symbol" => :right,
35
+ "on an array" => [:right],
36
+ "on a hash" => { all: :right }
37
+ }
38
+
39
+ types.each do |name, definition|
40
+ describe name do
41
+ before do
42
+ cls.clear_dynamic_attrs!
43
+ cls.define_attrs [
44
+ {
45
+ name: { on: definition }
46
+ }
47
+ ]
48
+ end
49
+
50
+ describe "_dynamic_attrs" do
51
+ it "Should hold an array comparable to the definition" do
52
+ expect(cls._dynamic_attrs[:name]).not_to eq nil
53
+ expect(cls._dynamic_attrs[:name].class).to eq Array
54
+ expect(cls._dynamic_attrs[:name].length).to eq 1
55
+ end
56
+ it "Should hold on defintions comparable to the definition" do
57
+ cls._dynamic_attrs[:name].each do |attr|
58
+ expect(attr[:on].class).to eq Hash
59
+ expect(attr[:on].length).to eq 1
60
+ end
61
+ end
62
+ end
63
+
64
+ describe 'attrs_for' do
65
+ it "With no state should return definition" do
66
+ expect(cls.attrs_for.keys).to eq [:name]
67
+ end
68
+ it "With :all state should return definition" do
69
+ expect(cls.attrs_for(:all).keys).to eq [:name]
70
+ end
71
+ it "With right state should return definition" do
72
+ expect(cls.attrs_for(:right).keys).to eq [:name]
73
+ end
74
+ it "With wrong state should return definition" do
75
+ expect(cls.attrs_for(:wrong).keys).to eq []
76
+ end
77
+ end
29
78
 
79
+ describe 'attrs_names_for' do
80
+ it "With no state should return definition" do
81
+ expect(cls.attrs_names_for).to eq [:name]
82
+ end
83
+ it "With :all state should return definition" do
84
+ expect(cls.attrs_names_for(:all)).to eq [:name]
85
+ end
86
+ it "With right state should return definition" do
87
+ expect(cls.attrs_names_for(:right)).to eq [:name]
88
+ end
89
+ it "With wrong state should return definition" do
90
+ expect(cls.attrs_names_for(:wrong)).to eq []
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ describe "Advanced definition of on" do
30
98
  before do
31
- cls.define_attrs definition
99
+ @definition = [
100
+ {
101
+ name: { on: :foo }
102
+ },
103
+ {
104
+ name: { on: :bar }
105
+ }
106
+ ]
107
+ cls.define_attrs @definition
32
108
  end
33
109
 
34
- it "Should have at least one validator" do
35
- expect(cls.validators).not_to eq []
36
- expect(cls.validators.length).to be >= 1
110
+ describe "_dynamic_attrs" do
111
+ it "Should hold a hash for the defines on" do
112
+ expect(cls._dynamic_attrs[:name]).not_to eq nil
113
+ expect(cls._dynamic_attrs[:name].class).to eq Array
114
+ expect(cls._dynamic_attrs[:name].length).to eq 2
115
+ end
116
+ end
117
+
118
+ describe 'attrs_names_for' do
119
+ it "With no state should return definition" do
120
+ expect(cls.attrs_names_for).to eq [:name]
121
+ end
122
+ it "With :all state should return definition" do
123
+ expect(cls.attrs_names_for(:all)).to eq [:name]
124
+ end
125
+ it "With foo state should return definition" do
126
+ expect(cls.attrs_names_for(:foo)).to eq [:name]
127
+ end
128
+ it "With bar state should return definition" do
129
+ expect(cls.attrs_names_for(:bar)).to eq [:name]
130
+ end
131
+ it "With wrong state should return definition" do
132
+ expect(cls.attrs_names_for(:wrong)).to eq []
133
+ end
134
+ end
135
+ end
136
+
137
+ describe "Simple definition of presence validator" do
138
+ before do
139
+ @definition = [
140
+ {
141
+ name:
142
+ {
143
+ validates: { presence: true }
144
+ }
145
+ }
146
+ ]
147
+ @definition_keys = @definition.map { |d| d.keys }.flatten
148
+ cls.define_attrs @definition
37
149
  end
38
150
 
39
- it "Should have a specific validator" do
40
- obj = definition
41
- name = obj.keys.first
42
- validators = obj[name][:validates]
43
- validator_type = validators.keys.first
151
+ describe "validators" do
152
+ it "Should have at least one validator" do
153
+ expect(cls.validators).not_to eq []
154
+ expect(cls.validators.length).to be >= 1
155
+ end
156
+
157
+ it "Should have validators from definition" do
158
+ definition_validators = @definition.map { |obj|obj.keys.map { |k| obj[k][:validates] } }.flatten
159
+ definition_validator_classes = definition_validators.map { |validator| validator.keys.map { |k| "ActiveRecord::Validations::#{k.capitalize}Validator".constantize } }.flatten
44
160
 
45
- validator = cls.validators.first
46
- expect(validator.class).to eq "ActiveRecord::Validations::#{validator_type.capitalize}Validator".constantize
161
+ expect(cls.validators.length).to eq definition_validators.length
162
+ expect(cls.validators.map(&:class)).to eq definition_validator_classes
163
+ end
47
164
  end
48
165
 
49
- it "_dynamic_attrs should have configuration from define_attrs" do
50
- expect(cls._dynamic_attrs).to eq definition
166
+ describe "_dynamic_attrs" do
167
+ it "Should have keys from definition" do
168
+ expect(cls._dynamic_attrs.keys).to eq @definition_keys
169
+ end
51
170
  end
52
171
 
53
- it "attrs_for with no state should return definition" do
54
- expect(cls.attrs_for).to eq definition
172
+ describe "attrs_for" do
173
+ it "With no state should have the same keys as the definition" do
174
+ response = cls.attrs_for
175
+ expect(response.class).to eq Hash
176
+ expect(cls.attrs_for.keys).to eq @definition_keys
177
+ expect(cls.attrs_for.keys.length).to eq @definition_keys.length
178
+ end
55
179
  end
56
180
 
57
- it "attrs_names_for with no state should return definition" do
58
- expect(cls.attrs_names_for).to eq definition.keys
181
+ describe "attrs_names_for" do
182
+ it "With no state should return definition" do
183
+ expect(cls.attrs_names_for).to eq @definition_keys
184
+ end
59
185
  end
60
186
  end
61
187
 
62
188
  describe "Simple definition of presence validator, with on parameter" do
63
- let(:defition_array) { [{name:{validates:{presence: true}, on: :right}}] }
64
- let(:definition) { Hash[*defition_array.flatten] }
65
- let(:instance) { cls.new }
66
-
67
189
  before do
68
- cls.define_attrs definition
190
+ @definition = [
191
+ {
192
+ name:
193
+ {
194
+ validates: { presence: true },
195
+ on: :right
196
+ }
197
+ }
198
+ ]
199
+ @definition_keys = @definition.map { |d| d.keys }.flatten
200
+ cls.define_attrs @definition
69
201
  end
70
202
 
71
203
  it "Should have at least one validator" do
@@ -73,52 +205,50 @@ describe "BASE TESTS" do
73
205
  expect(cls.validators.length).to be >= 1
74
206
  end
75
207
 
76
- it "Should have a specific validator" do
77
- obj = definition
78
- name = obj.keys.first
79
- validators = obj[name][:validates]
80
- validator_type = validators.keys.first
208
+ it "Should have validators from definition" do
209
+ definition_validators = @definition.map { |obj|obj.keys.map { |k| obj[k][:validates] } }.flatten
210
+ definition_validator_classes = definition_validators.map { |validator| validator.keys.map { |k| "ActiveRecord::Validations::#{k.capitalize}Validator".constantize } }.flatten
81
211
 
82
- validator = cls.validators.first
83
- expect(validator.class).to eq "ActiveRecord::Validations::#{validator_type.capitalize}Validator".constantize
212
+ expect(cls.validators.length).to eq definition_validators.length
213
+ expect(cls.validators.map(&:class)).to eq definition_validator_classes
84
214
  end
85
215
 
86
- it "_dynamic_attrs should have configuration from define_attrs" do
87
- expect(cls._dynamic_attrs).to eq definition
216
+ it "_dynamic_attrs should have keys from definition" do
217
+ expect(cls._dynamic_attrs.keys).to eq @definition_keys
88
218
  end
89
219
 
90
220
  describe "attrs_for" do
91
221
  it "with no state should return definition" do
92
- expect(cls.attrs_for).to eq definition
222
+ expect(cls.attrs_for.keys).to eq @definition_keys
93
223
  end
94
224
  describe "with right state" do
95
225
  it "with symbol should return definition" do
96
- expect(cls.attrs_for(:right)).to eq definition
226
+ expect(cls.attrs_for(:right).keys).to eq @definition_keys
97
227
  end
98
228
  it "with string should return definition" do
99
- expect(cls.attrs_for("right")).to eq definition
229
+ expect(cls.attrs_for("right").keys).to eq @definition_keys
100
230
  end
101
231
  end
102
232
  describe "with wrong state" do
103
233
  it "with symbol should return empty object" do
104
- expect(cls.attrs_for(:wrong)).to eq({})
234
+ expect(cls.attrs_for(:wrong).keys).to eq []
105
235
  end
106
236
  it "with string should return empty object" do
107
- expect(cls.attrs_for("wrong")).to eq({})
237
+ expect(cls.attrs_for("wrong").keys).to eq []
108
238
  end
109
239
  end
110
240
  end
111
241
 
112
242
  describe "attrs_names_for" do
113
243
  it "with no state should return definition keys" do
114
- expect(cls.attrs_names_for).to eq definition.keys
244
+ expect(cls.attrs_names_for).to eq @definition_keys
115
245
  end
116
246
  describe "with right state" do
117
247
  it "with symbol should return definition keys" do
118
- expect(cls.attrs_names_for(:right)).to eq definition.keys
248
+ expect(cls.attrs_names_for(:right)).to eq @definition_keys
119
249
  end
120
250
  it "with string should return definition keys" do
121
- expect(cls.attrs_names_for("right")).to eq definition.keys
251
+ expect(cls.attrs_names_for("right")).to eq @definition_keys
122
252
  end
123
253
  end
124
254
  describe "with wrong state" do
data/spec/spec_helper.rb CHANGED
@@ -23,4 +23,7 @@ RSpec.configure do |config|
23
23
  config.after(:each) do
24
24
  DatabaseCleaner.clean
25
25
  end
26
+
27
+ config.filter_run focus: true
28
+ config.run_all_when_everything_filtered = true
26
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamic_attribute_declaration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikkel Wied Frederiksen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-02 00:00:00.000000000 Z
11
+ date: 2014-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.2'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rspec-its
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -133,6 +147,7 @@ files:
133
147
  - ".rspec"
134
148
  - ".ruby-version"
135
149
  - Gemfile
150
+ - Guardfile
136
151
  - LICENSE.txt
137
152
  - README.md
138
153
  - Rakefile