attr_permit 0.0.2 → 0.0.3

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: 60085da6f28a79531a08f8a04d9c68494fe0c44f
4
- data.tar.gz: a645d055a3f44b56b20dc30db4bda4b8b0d7535c
3
+ metadata.gz: f4d534037b060cc2a56910ea66ffcf2fe20c4d1d
4
+ data.tar.gz: 3aa06e273bf5c3d6d06f245dfbe6963dc2d1d439
5
5
  SHA512:
6
- metadata.gz: d6da0f2e29721338bede8902ddc6d5bc35faad38b5cefebd61e7f1e571943dde4815883b421b2922018b098a21ff5d78539a9be52f8badeac29e95f482c0133b
7
- data.tar.gz: d206db4ae5890cee787af67efd1ce92122edcbcf350d8d21cd5c3400e39b228742446f26b9d2ba7aa9cbedfb0eaa07e21cda4134e7dbe6bc012844cbe9b1dad4
6
+ metadata.gz: 324b9d1f6190cb54d3fe1dae24bc883095fb58f33c3c3d6e8be2f01562ab1dbcd46f7a53e7c013941fe23f657f3bc03708d1a2d5d4cc136adab94fd5ea0bfa32
7
+ data.tar.gz: f0c5b34709b0944c5e7b3b88e77fc8d497d0a422045874f484ce611a12ad499f8b45e6f44945d6a0f1c4868e5b3bed882c875accd14df73922a7902d9bdc3eb2
@@ -1,4 +1,3 @@
1
- require 'attr_permit/version'
2
1
  require 'active_support/core_ext/big_decimal'
3
2
 
4
3
  class AttrPermit
@@ -9,8 +8,12 @@ class AttrPermit
9
8
  @permissible_methods ||= []
10
9
  end
11
10
 
11
+ def mapped_methods
12
+ @mapped_methods ||= []
13
+ end
14
+
12
15
  def attr_permit(*permissible_methods)
13
- self.permissible_methods.concat [*permissible_methods, *get_super_attr]
16
+ self.permissible_methods.concat [*permissible_methods, *get_super_permissible_methods]
14
17
  self.permissible_methods.each do |meth|
15
18
 
16
19
  send(:define_method, meth) do
@@ -21,12 +24,35 @@ class AttrPermit
21
24
  end
22
25
  end
23
26
 
27
+ def map_attributes(map_hash)
28
+ map_hash.each do |to, from|
29
+ map_attribute to, from
30
+ end
31
+ end
32
+
33
+ def map_attribute(to, from)
34
+ self.mapped_methods.concat [*to, *get_super_mapped_methods]
35
+ if from.is_a? Proc
36
+ send(:define_method, to) do
37
+ instance_exec(&from)
38
+ end
39
+ else
40
+ send(:define_method, to) do
41
+ call_method(from)
42
+ end
43
+ end
44
+ end
45
+
24
46
  protected
25
47
 
26
- def get_super_attr
48
+ def get_super_permissible_methods
27
49
  superclass.permissible_methods unless superclass == AttrPermit
28
50
  end
29
51
 
52
+ def get_super_mapped_methods
53
+ superclass.mapped_methods unless superclass == AttrPermit
54
+ end
55
+
30
56
  end
31
57
  protected
32
58
  attr_reader :source
@@ -53,26 +79,23 @@ class AttrPermit
53
79
 
54
80
  protected :call_method
55
81
 
82
+ def map_hash(big_decimal_as_string: false, all_values_as_string: false)
83
+ hasher([*self.class.mapped_methods], big_decimal_as_string, all_values_as_string)
84
+ end
85
+
86
+ def permit_hash(big_decimal_as_string: false, all_values_as_string: false)
87
+ hasher([*self.class.permissible_methods], big_decimal_as_string, all_values_as_string)
88
+ end
89
+
56
90
  def to_hash(big_decimal_as_string: false, all_values_as_string: false)
57
- @big_decimal_as_string = big_decimal_as_string
58
- @all_values_as_string = all_values_as_string
59
- hash = {}
60
- self.class.permissible_methods.each do |var|
61
- value = send(var)
62
- value = to_hash_object(value)
63
- value = big_decimal_as_string_convert(value)
64
- value = all_values_as_string_convert(value)
65
- value = array_to_hash(value)
66
- hash[var] = value
67
- end
68
- hash
91
+ hasher([*self.class.permissible_methods, *self.class.mapped_methods], big_decimal_as_string, all_values_as_string)
69
92
  end
70
93
 
71
94
  alias_method :to_h, :to_hash
72
95
 
73
- def non_nil_values
96
+ def non_nil_values(hash_type=:to_hash)
74
97
  hash = {}
75
- to_hash.each { |k, v| hash[k] = v unless v.nil? }
98
+ send(hash_type).each { |k, v| hash[k] = v unless v.nil? }
76
99
  hash
77
100
  end
78
101
 
@@ -80,6 +103,14 @@ class AttrPermit
80
103
  self.hash == obj.hash
81
104
  end
82
105
 
106
+ def is_equivalent?(obj)
107
+ self_hash = self.to_hash
108
+ obj_hash = obj.to_hash
109
+ self_hash.each { |k, v| self_hash[k] = v.to_s }
110
+ obj_hash.each { |k, v| obj_hash[k] = v.to_s }
111
+ self_hash == obj_hash
112
+ end
113
+
83
114
  def hash
84
115
  self.to_hash.hash
85
116
  end
@@ -138,4 +169,38 @@ class AttrPermit
138
169
  value
139
170
  end
140
171
 
172
+ def hasher(methods, big_decimal_as_string, all_values_as_string)
173
+ @big_decimal_as_string = big_decimal_as_string
174
+ @all_values_as_string = all_values_as_string
175
+ hash = {}
176
+ methods.each do |var|
177
+ value = send(var)
178
+ value = to_hash_object(value)
179
+ value = big_decimal_as_string_convert(value)
180
+ value = all_values_as_string_convert(value)
181
+ value = array_to_hash(value)
182
+ hash[var] = value
183
+ end
184
+ hash
185
+ end
186
+
141
187
  end
188
+
189
+ class AttrPermitLazy < AttrPermit
190
+
191
+ def self.attr_permit(*permissible_methods)
192
+ self.permissible_methods.concat [*permissible_methods, *get_super_premissible_methods]
193
+ self.permissible_methods.each do |meth|
194
+
195
+ send(:define_method, meth) do
196
+ callable = instance_variable_get("@#{meth}")
197
+ instance_variable_set("@#{meth}", callable.call) if callable.respond_to?(:call)
198
+ instance_variable_get("@#{meth}")
199
+ end
200
+
201
+ attr_writer meth unless public_instance_methods.include?("#{meth}=")
202
+ end
203
+ end
204
+
205
+ end
206
+
@@ -1,3 +1,3 @@
1
1
  class AttrPermit
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -95,6 +95,14 @@ RSpec.describe AttrPermit, unit: true do
95
95
 
96
96
  end
97
97
 
98
+ describe "is_equivalent?" do
99
+ it "will convert all values to strings before checking ==" do
100
+ obj1 = TestStruct.new(foo: "1", bar: "2")
101
+ obj2 = TestStruct.new(foo: 1, bar: 2)
102
+ expect(obj1.is_equivalent?(obj2)).to eq true
103
+ end
104
+ end
105
+
98
106
  describe 'sub classing' do
99
107
 
100
108
  before do
@@ -193,4 +201,68 @@ RSpec.describe AttrPermit, unit: true do
193
201
 
194
202
  end
195
203
 
204
+ describe 'map_attribute' do
205
+
206
+ it 'will return value from mapped attribute' do
207
+ class MapAttributeOne < AttrPermit
208
+ attr_permit :attr
209
+ map_attribute :my_attr, :attr
210
+ end
211
+
212
+ expect(MapAttributeOne.new(attr: 'attr').my_attr).to eq 'attr'
213
+ end
214
+
215
+ it 'will call proc from map' do
216
+ class MapAttributeTwo < AttrPermit
217
+ map_attribute :my_attr, -> { source.item }
218
+ end
219
+
220
+ expect(MapAttributeTwo.new(OpenStruct.new(item: 'My Item')).my_attr).to eq 'My Item'
221
+ end
222
+
223
+ end
224
+
225
+ describe 'map_attributes' do
226
+
227
+ before do
228
+ class MapAttributeOne < AttrPermit
229
+ attr_permit :attr, :attr2
230
+ map_attributes :my_attr => :attr, :my_attr2 => :attr2
231
+ end
232
+ end
233
+
234
+ it 'will return value from mapped attribute' do
235
+ expect(MapAttributeOne.new(attr: 'attr1').my_attr).to eq 'attr1'
236
+ expect(MapAttributeOne.new(attr2: 'attr2').my_attr2).to eq 'attr2'
237
+ end
238
+
239
+ it 'map_hash' do
240
+ expect(MapAttributeOne.new(attr2: 'attr2', attr: 'attr1').map_hash).to eq({:my_attr => "attr1", :my_attr2 => "attr2"})
241
+ end
242
+
243
+ it 'permit_hash' do
244
+ expect(MapAttributeOne.new(attr2: 'attr2', attr: 'attr1').permit_hash).to eq({:attr => "attr1", :attr2 => "attr2"})
245
+ end
246
+
247
+ it 'to_hash' do
248
+ expect(MapAttributeOne.new(attr2: 'attr2', attr: 'attr1').to_hash).to eq({:attr => "attr1", :attr2 => "attr2", :my_attr => "attr1", :my_attr2 => "attr2"})
249
+ end
250
+
251
+ it 'non_nil_values' do
252
+ expect(MapAttributeOne.new(attr2: 'attr2').non_nil_values(:map_hash)).to eq({:my_attr2 => "attr2"})
253
+ expect(MapAttributeOne.new(attr2: 'attr2').non_nil_values(:permit_hash)).to eq({:attr2 => "attr2"})
254
+ expect(MapAttributeOne.new(attr2: 'attr2').non_nil_values).to eq({:attr2 => "attr2", :my_attr2 => "attr2"})
255
+ end
256
+
257
+ it 'will call proc from map' do
258
+ class MapAttributeTwo < AttrPermit
259
+ map_attributes :my_attr => -> { source.item }
260
+ end
261
+
262
+ expect(MapAttributeTwo.new(OpenStruct.new(item: 'My Item')).my_attr).to eq 'My Item'
263
+ expect(MapAttributeTwo.new(OpenStruct.new(item: 'My Item')).to_hash).to eq({:my_attr => "My Item"})
264
+ end
265
+
266
+ end
267
+
196
268
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attr_permit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Zeisler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-30 00:00:00.000000000 Z
11
+ date: 2014-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus