strict_struct 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/strict_struct/version.rb +1 -1
- data/lib/strict_struct.rb +13 -1
- data/spec/strict_struct_spec.rb +112 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5940d1c83ecfa23afa0cb7d348cbb34b72f397a
|
4
|
+
data.tar.gz: 8dfc4803fd53e15b7b975651a2db7283a6bdffde
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa96254650eabfe6f8d0fc2aae953c5bdca43cd9a9087a958700c78f0d38bea5ea93e4a3b84dad161e860bde509ecf5b4f79bc4a9e546816c8b9f1169d4c09db
|
7
|
+
data.tar.gz: ad2b62577ef0f6f8ae44eb466be05ce161ee0799a82d62b668c182f437a3f27c5f65e19a5a623da92811b7d67e252de6f98f63c8b6e8c27f70b6a35d96d56928
|
data/lib/strict_struct.rb
CHANGED
@@ -20,7 +20,7 @@ module StrictStruct
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def self.new(*attributes, &block)
|
23
|
-
|
23
|
+
mod = Module.new do
|
24
24
|
define_method :initialize do |hash={}|
|
25
25
|
Helper.validate_arguments(hash.keys, attributes)
|
26
26
|
@_strict_struct_hash = Hash[hash.to_a].freeze
|
@@ -33,13 +33,25 @@ module StrictStruct
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def to_h
|
36
|
+
to_hash
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_hash
|
36
40
|
Hash[@_strict_struct_hash.to_a]
|
37
41
|
end
|
38
42
|
|
39
43
|
define_method :== do |other|
|
44
|
+
return false unless self.class == other.class
|
45
|
+
|
40
46
|
attributes.all? {|name| self.send(name) == other.send(name)}
|
41
47
|
end
|
48
|
+
|
49
|
+
define_method :hash do
|
50
|
+
@_strict_struct_hash.hash
|
51
|
+
end
|
42
52
|
end
|
53
|
+
klass = Class.new
|
54
|
+
klass.send :include, mod
|
43
55
|
klass.class_eval(&block) if block_given?
|
44
56
|
klass
|
45
57
|
end
|
data/spec/strict_struct_spec.rb
CHANGED
@@ -137,6 +137,78 @@ describe 'StrictStruct' do
|
|
137
137
|
|
138
138
|
a_foo.to_h.should eq({bar: 'baz', baz: 3})
|
139
139
|
end
|
140
|
+
|
141
|
+
it "is possible to override, and reuse using super" do
|
142
|
+
foo = StrictStruct.new(:bar, :baz) do
|
143
|
+
def to_h
|
144
|
+
super.merge pirate: 'captain'
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
a_foo = foo.new(bar: 'baz', baz: 3)
|
149
|
+
|
150
|
+
a_foo.to_h.should eq({bar: 'baz', baz: 3, pirate: 'captain'})
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe '#to_hash' do
|
155
|
+
it "returns all the values the object is defined by" do
|
156
|
+
foo = StrictStruct.new(:bar, :baz)
|
157
|
+
|
158
|
+
a_foo = foo.new(bar: 'baz', baz: 3)
|
159
|
+
|
160
|
+
a_foo.to_hash.should eq({bar: 'baz', baz: 3})
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
describe "extending to_h with extra fields" do
|
165
|
+
it "extends to_h, leaves to_hash alone" do
|
166
|
+
foo = StrictStruct.new(:bar, :baz) do
|
167
|
+
def to_h
|
168
|
+
super.merge pirate: 'captain'
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
a_foo = foo.new(bar: 'baz', baz: 3)
|
173
|
+
|
174
|
+
a_foo.to_h.should eq({bar: 'baz', baz: 3, pirate: 'captain'})
|
175
|
+
a_foo.to_hash.should eq({bar: 'baz', baz: 3})
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
describe "extending to_hash with extra fields" do
|
180
|
+
it "extends both to_hash and to_h" do
|
181
|
+
foo = StrictStruct.new(:bar, :baz) do
|
182
|
+
def to_hash
|
183
|
+
super.merge pirate: 'captain'
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
a_foo = foo.new(bar: 'baz', baz: 3)
|
188
|
+
|
189
|
+
a_foo.to_h.should eq({bar: 'baz', baz: 3, pirate: 'captain'})
|
190
|
+
a_foo.to_hash.should eq({bar: 'baz', baz: 3, pirate: 'captain'})
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
describe "extending to_h and to_hash" do
|
195
|
+
it "extends both with the extensions to to_hash, and to_h also with its own extensions" do
|
196
|
+
foo = StrictStruct.new(:bar, :baz) do
|
197
|
+
def to_hash
|
198
|
+
super.merge pirate: 'captain'
|
199
|
+
end
|
200
|
+
|
201
|
+
def to_h
|
202
|
+
super.merge samurai: 'sword'
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
a_foo = foo.new(bar: 'baz', baz: 3)
|
207
|
+
|
208
|
+
a_foo.to_h.should eq({bar: 'baz', baz: 3, pirate: 'captain', samurai: 'sword'})
|
209
|
+
a_foo.to_hash.should eq({bar: 'baz', baz: 3, pirate: 'captain'})
|
210
|
+
|
211
|
+
end
|
140
212
|
end
|
141
213
|
|
142
214
|
describe '#==' do
|
@@ -148,7 +220,8 @@ describe 'StrictStruct' do
|
|
148
220
|
|
149
221
|
a_foo.should eq another_foo
|
150
222
|
end
|
151
|
-
|
223
|
+
|
224
|
+
it "returns false if the values are not the same" do
|
152
225
|
foo = StrictStruct.new(:bar)
|
153
226
|
|
154
227
|
a_foo = foo.new(bar: 'baz')
|
@@ -156,5 +229,43 @@ describe 'StrictStruct' do
|
|
156
229
|
|
157
230
|
a_foo.should_not eq another_foo
|
158
231
|
end
|
232
|
+
|
233
|
+
it "returns false if two values are different StrictStructs" do
|
234
|
+
chicken_class = StrictStruct.new(:wings)
|
235
|
+
plane_class = StrictStruct.new(:wings)
|
236
|
+
|
237
|
+
chicken = chicken_class.new(wings: 2)
|
238
|
+
plane = plane_class.new(wings: 2)
|
239
|
+
|
240
|
+
plane.should_not eq chicken
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
describe '#hash' do
|
245
|
+
it "returns the same value for two empty objects" do
|
246
|
+
empty = StrictStruct.new
|
247
|
+
empty.new.hash.should == empty.new.hash
|
248
|
+
end
|
249
|
+
|
250
|
+
it "returns a different value for two non-empty objects with different content" do
|
251
|
+
container = StrictStruct.new(:a)
|
252
|
+
|
253
|
+
first = container.new(a: 1)
|
254
|
+
second = container.new(a: 2)
|
255
|
+
|
256
|
+
first.hash.should_not == second.hash
|
257
|
+
end
|
258
|
+
|
259
|
+
it "returns the same value for two non-empty objects with the same content" do
|
260
|
+
container = StrictStruct.new(:a)
|
261
|
+
|
262
|
+
first = container.new(a: 1)
|
263
|
+
second = container.new(a: 1)
|
264
|
+
|
265
|
+
first.hash.should == second.hash
|
266
|
+
end
|
267
|
+
|
268
|
+
# we need no guarantee that objects with the same content, but different classes
|
269
|
+
# return different hashes
|
159
270
|
end
|
160
271
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strict_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark IJbema
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
91
|
version: '0'
|
92
92
|
requirements: []
|
93
93
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.2.
|
94
|
+
rubygems_version: 2.2.0
|
95
95
|
signing_key:
|
96
96
|
specification_version: 4
|
97
97
|
summary: StrictStruct is an immutable, named parameters based, replacement for Struct
|