attr_permit 0.0.1 → 0.0.2

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: f9c5282583796947f5fa8dbfe8d52d403face943
4
- data.tar.gz: 6a017843c7a79715a9e7f2d39db51c4021e824f9
3
+ metadata.gz: 60085da6f28a79531a08f8a04d9c68494fe0c44f
4
+ data.tar.gz: a645d055a3f44b56b20dc30db4bda4b8b0d7535c
5
5
  SHA512:
6
- metadata.gz: f54cff42ef598ccc27d720e86477643f678d097ec3ddd8a404e8d96bded5ee3013b1a8eff1e28d8f9aa1b9d0d8c68dcf9d4d246d76d8ea4d3f8ef650ab648ce6
7
- data.tar.gz: 08047aee3e18f211bc6461b3778e0ecbeb4128a73b8a413dfb08dbfb4c73f3f54043635d6056a6d759a0375c293ddb8df00fe96e8f7b7992950ea59c39b25590
6
+ metadata.gz: d6da0f2e29721338bede8902ddc6d5bc35faad38b5cefebd61e7f1e571943dde4815883b421b2922018b098a21ff5d78539a9be52f8badeac29e95f482c0133b
7
+ data.tar.gz: d206db4ae5890cee787af67efd1ce92122edcbcf350d8d21cd5c3400e39b228742446f26b9d2ba7aa9cbedfb0eaa07e21cda4134e7dbe6bc012844cbe9b1dad4
data/attr_permit.gemspec CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_runtime_dependency "virtus", '~> 1.0'
22
- spec.add_runtime_dependency "activesupport", "~>4.1"
22
+ spec.add_runtime_dependency "activesupport", ">= 4.0"
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.6"
25
25
  spec.add_development_dependency "rake", "~> 10.3"
@@ -1,3 +1,3 @@
1
1
  class AttrPermit
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,196 @@
1
+ require 'rspec'
2
+ require 'virtus'
3
+ require 'active_support/hash_with_indifferent_access'
4
+ require_relative '../lib/attr_permit'
5
+
6
+ RSpec.describe AttrPermit, unit: true do
7
+
8
+ before do
9
+ class TestStruct2 < AttrPermit
10
+ attr_permit :baz, :bar
11
+ end
12
+
13
+ class TestStruct < AttrPermit
14
+ attr_permit :foo, :bar
15
+
16
+ end
17
+ end
18
+
19
+ describe '#to_hash' do
20
+
21
+ it 'will recursively convert all nested objects into hashs' do
22
+ expect(TestStruct.new(bar: TestStruct.new(bar: 1)).to_hash).to eq({:foo => nil, :bar => {:foo => nil, :bar => 1}})
23
+ expect(TestStruct.new(bar: [TestStruct.new(bar: 1)]).to_hash).to eq({:foo => nil, :bar => [{:foo => nil, :bar => 1}]})
24
+ end
25
+
26
+ end
27
+
28
+ describe '#call_method' do
29
+
30
+ it 'is a protected method that will call a proc or return a value' do
31
+ class CallMethodExample < AttrPermit
32
+ attr_permit :foo
33
+
34
+ def _foo
35
+ call_method(:foo)
36
+ end
37
+
38
+ end
39
+ expect(CallMethodExample.new(foo: -> { 1 })._foo).to eq 1
40
+ expect(CallMethodExample.new(foo: 1)._foo).to eq 1
41
+ end
42
+
43
+ end
44
+
45
+ it 'can be initialized with an object with correctly named attribute readers' do
46
+ src = OpenStruct.new(foo: 'hello', bar: nil)
47
+ dest = TestStruct.new(src.to_h)
48
+ hash = dest.to_hash
49
+ expect(hash).to eq({:foo => "hello", :bar => nil})
50
+ expect(dest.foo).to eq('hello')
51
+ expect(hash[:foo]).to eq('hello')
52
+ end
53
+
54
+ it 'can be initialized with a hash with correctly named keys' do
55
+ src = {foo: 'hello'}
56
+ dest = TestStruct.new(src)
57
+ hash = dest.to_hash
58
+ expect(dest.foo).to eq('hello')
59
+ expect(hash[:foo]).to eq('hello')
60
+ end
61
+
62
+ it 'can be initialized with a HashWithIndifferentAccess with correctly named keys' do
63
+ src = HashWithIndifferentAccess.new({foo: 'hello'})
64
+ dest = TestStruct.new(src)
65
+ hash = dest.to_hash
66
+ expect(dest.foo).to eq('hello')
67
+ expect(hash[:foo]).to eq('hello')
68
+ end
69
+
70
+ it 'options big_decimal_as_string: true' do
71
+ obj = TestStruct.new(foo: BigDecimal.new('1.21'))
72
+ expect(obj.to_hash(big_decimal_as_string: true)).to eq({:foo => "1.21", :bar => nil})
73
+ end
74
+
75
+ it 'will convert object inside of arrays into hashes' do
76
+ obj = TestStruct.new(foo: [TestStruct.new(bar: BigDecimal.new('1.21'))])
77
+ expect(obj.to_hash(big_decimal_as_string: true)).to eq({:foo => [{:foo => nil, :bar => "1.21"}], :bar => nil})
78
+ end
79
+
80
+ context 'to_hash(all_values_as_string: true)' do
81
+
82
+ it 'options all_values_as_string: true' do
83
+ obj = TestStruct.new(foo: BigDecimal.new('1.21'))
84
+ expect(obj.to_hash(all_values_as_string: true)).to eq({:foo => "1.21", :bar => ''})
85
+ end
86
+
87
+ end
88
+
89
+ describe '#non_nil_values' do
90
+
91
+ it 'will only return initialized values' do
92
+ struct = TestStruct.new(foo: true)
93
+ expect(struct.non_nil_values).to eq({foo: true})
94
+ end
95
+
96
+ end
97
+
98
+ describe 'sub classing' do
99
+
100
+ before do
101
+ class APermit < AttrPermit
102
+ attr_permit :foo
103
+ end
104
+
105
+ class BPermit < APermit
106
+ attr_permit :bar
107
+ end
108
+
109
+ class CPermit < BPermit
110
+ attr_permit :baz
111
+ end
112
+ end
113
+
114
+ it 'class B will have attributes from A and its own' do
115
+ expect(BPermit.permissible_methods).to eq [:bar, :foo]
116
+ expect(BPermit.instance_methods).to include *[:bar, :bar=, :foo, :foo=]
117
+ end
118
+
119
+ it 'class C will have attributes from A, B and its own' do
120
+ expect(CPermit.permissible_methods).to eq [:baz, :bar, :foo]
121
+ expect(CPermit.instance_methods).to include(:baz, :baz=, :bar, :bar=, :foo, :foo=)
122
+ end
123
+
124
+ after do
125
+ %w[APermit BPermit CPermit].each { |klass| Object.send(:remove_const, klass) }
126
+ end
127
+
128
+ end
129
+
130
+ describe 'include Virtus.model' do
131
+
132
+ class TestStructVirtus < AttrPermit
133
+ include Virtus.model
134
+ attribute :foo, Boolean
135
+ attribute :bar, BigDecimal
136
+ end
137
+
138
+ it 'can be initialized with a hash with correctly named keys' do
139
+ src = {foo: 'true'}
140
+ dest = TestStructVirtus.new(src)
141
+ hash = dest.to_hash
142
+ expect(hash).to eq({:foo => true, :bar => nil})
143
+ expect(dest.foo).to eq(true)
144
+ expect(hash[:foo]).to eq(true)
145
+ end
146
+
147
+ it 'can be initialized with a HashWithIndifferentAccess with correctly named keys' do
148
+ src = HashWithIndifferentAccess.new({bar: '123'})
149
+ dest = TestStructVirtus.new(src)
150
+ hash = dest.to_hash
151
+ expect(dest.bar).to eq(123)
152
+ expect(hash[:bar]).to eq(123)
153
+ end
154
+
155
+ describe '#non_nil_values' do
156
+
157
+ it 'will only return initialized values' do
158
+ struct = TestStructVirtus.new(foo: 'true')
159
+ expect(struct.non_nil_values).to eq({foo: true})
160
+ end
161
+
162
+ end
163
+
164
+ end
165
+
166
+ describe 'lazy load' do
167
+
168
+ before do
169
+
170
+ class Lazy < AttrPermit
171
+ attr_permit :name
172
+ end
173
+
174
+ end
175
+
176
+ it 'once called it will memoized' do
177
+ value = 0
178
+ proc = -> { value += 1 }
179
+ lazy = Lazy.new(name: proc)
180
+ lazy.name
181
+ expect(lazy.name).to eq 1
182
+ end
183
+
184
+ it 'will lazy load name' do
185
+ lazy = Lazy.new(name: -> { raise('Name was called') })
186
+ expect { lazy.name }.to raise_error('Name was called')
187
+ end
188
+
189
+ it 'will call block on to_hash' do
190
+ lazy = Lazy.new(name: -> { raise('Name was called') })
191
+ expect { lazy.to_hash }.to raise_error('Name was called')
192
+ end
193
+
194
+ end
195
+
196
+ 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.1
4
+ version: 0.0.2
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-08-25 00:00:00.000000000 Z
11
+ date: 2014-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '4.1'
33
+ version: '4.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '4.1'
40
+ version: '4.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -96,6 +96,7 @@ files:
96
96
  - attr_permit.gemspec
97
97
  - lib/attr_permit.rb
98
98
  - lib/attr_permit/version.rb
99
+ - spec/attr_permit_spec.rb
99
100
  homepage: https://github.com/zeisler/attr_permit
100
101
  licenses:
101
102
  - MIT
@@ -120,4 +121,5 @@ rubygems_version: 2.2.2
120
121
  signing_key:
121
122
  specification_version: 4
122
123
  summary: Simple parametable object creator with lazy loading options.
123
- test_files: []
124
+ test_files:
125
+ - spec/attr_permit_spec.rb