strict_struct 0.0.4 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -3
- data/README.md +26 -0
- data/lib/strict_struct.rb +32 -27
- data/lib/strict_struct/version.rb +1 -1
- data/spec/spec_helper.rb +4 -0
- data/spec/strict_struct_spec.rb +31 -26
- data/strict_struct.gemspec +1 -1
- metadata +6 -7
- data/Gemfile.travis +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c74573f10c24a66bc073f1246d89c381b94adc4b
|
4
|
+
data.tar.gz: 0dac3d83c4dd1d86972abb9521c3ae618885ab19
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c36bbfe1ed0fad71cd0909ea43b586fd463d7df1c839c3e1727271e54959bd19364196c86d17ded8100de56fcff9ec2151e0ec5d0568f0f25d5fa229052ce03a
|
7
|
+
data.tar.gz: 0093c350d87be13f0160a97a814a2854fd9ef5fc9a6a37a55ffdaae0aa59e41f3a32c5fb02cb26381f5a534e34d537a2cf7b5f3207b8baa92b7084f8b96a9f94
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -34,6 +34,17 @@ class Rectange
|
|
34
34
|
@y = y
|
35
35
|
end
|
36
36
|
|
37
|
+
def to_h
|
38
|
+
to_h
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_hash
|
42
|
+
{
|
43
|
+
x: x,
|
44
|
+
y: y
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
37
48
|
def area
|
38
49
|
x * y
|
39
50
|
end
|
@@ -42,6 +53,21 @@ end
|
|
42
53
|
|
43
54
|
Since this is meant to create immutable objects, the values aren't actually assigned to instance variables but safed internally in a hash.
|
44
55
|
|
56
|
+
### Changing behaviour
|
57
|
+
|
58
|
+
You can also choose to override behaviour. You can just use super in the initialization block like you are used to with normal classes:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
Rectangle = StrictStruct.new(:x, :y) do
|
62
|
+
def area
|
63
|
+
x * y
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_hash
|
67
|
+
super.merge({area: area})
|
68
|
+
end
|
69
|
+
end
|
70
|
+
```
|
45
71
|
|
46
72
|
## Installation
|
47
73
|
|
data/lib/strict_struct.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require "strict_struct/version"
|
2
2
|
|
3
3
|
module StrictStruct
|
4
4
|
module Helper
|
@@ -19,40 +19,45 @@ module StrictStruct
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
22
|
+
class AttributeModule < Module
|
23
|
+
def initialize *attributes
|
24
|
+
super() do
|
25
|
+
define_method :initialize do |hash={}|
|
26
|
+
Helper.validate_arguments(hash.keys, attributes)
|
27
|
+
@_strict_struct_hash = Hash[hash.to_a].freeze
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
attributes.each do |attribute|
|
31
|
+
define_method(attribute) do
|
32
|
+
@_strict_struct_hash[attribute]
|
33
|
+
end
|
32
34
|
end
|
33
|
-
end
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
36
|
+
def to_h
|
37
|
+
to_hash
|
38
|
+
end
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
40
|
+
def to_hash
|
41
|
+
Hash[@_strict_struct_hash.to_a]
|
42
|
+
end
|
42
43
|
|
43
|
-
|
44
|
-
|
44
|
+
define_method :== do |other|
|
45
|
+
return false unless self.class == other.class
|
45
46
|
|
46
|
-
|
47
|
-
|
47
|
+
attributes.all? {|name| self.send(name) == other.send(name)}
|
48
|
+
end
|
48
49
|
|
49
|
-
|
50
|
-
|
50
|
+
define_method :hash do
|
51
|
+
@_strict_struct_hash.hash
|
52
|
+
end
|
51
53
|
end
|
52
54
|
end
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.new(*attributes, &block)
|
58
|
+
Class.new do
|
59
|
+
include AttributeModule.new(*attributes)
|
60
|
+
class_eval(&block) if block_given?
|
61
|
+
end
|
57
62
|
end
|
58
63
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/strict_struct_spec.rb
CHANGED
@@ -8,7 +8,7 @@ describe 'StrictStruct' do
|
|
8
8
|
|
9
9
|
a_foo = foo.new
|
10
10
|
|
11
|
-
a_foo.
|
11
|
+
expect(a_foo).to_not be_nil
|
12
12
|
end
|
13
13
|
|
14
14
|
it "works with parameters" do
|
@@ -16,7 +16,7 @@ describe 'StrictStruct' do
|
|
16
16
|
|
17
17
|
a_foo = foo.new(bar: 'baz')
|
18
18
|
|
19
|
-
a_foo.
|
19
|
+
expect(a_foo).to_not be_nil
|
20
20
|
end
|
21
21
|
|
22
22
|
it "raises an error when an argument is missing" do
|
@@ -77,7 +77,7 @@ describe 'StrictStruct' do
|
|
77
77
|
|
78
78
|
rectangle = rectangle_class.new(x: 3, y: 5)
|
79
79
|
|
80
|
-
rectangle.area.
|
80
|
+
expect(rectangle.area).to eq 15
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
@@ -87,8 +87,8 @@ describe 'StrictStruct' do
|
|
87
87
|
|
88
88
|
a_foo = foo.new(bar: 'baz')
|
89
89
|
|
90
|
-
a_foo.bar.
|
91
|
-
a_foo.
|
90
|
+
expect(a_foo.bar).to eq 'baz'
|
91
|
+
expect(a_foo).to respond_to 'bar'
|
92
92
|
end
|
93
93
|
|
94
94
|
it "is not defined when the value isn't defined by the class" do
|
@@ -96,7 +96,7 @@ describe 'StrictStruct' do
|
|
96
96
|
|
97
97
|
a_foo = foo.new(bar: 'baz')
|
98
98
|
|
99
|
-
a_foo.
|
99
|
+
expect(a_foo).to_not respond_to 'gerard'
|
100
100
|
end
|
101
101
|
|
102
102
|
it "raises a NoMethodError when called for a value which isn't defined" do
|
@@ -117,7 +117,7 @@ describe 'StrictStruct' do
|
|
117
117
|
k = klass.new(params)
|
118
118
|
params[:x] = 'bar'
|
119
119
|
|
120
|
-
k.x.
|
120
|
+
expect(k.x).to eq 'foo'
|
121
121
|
end
|
122
122
|
|
123
123
|
it "isn't broken by modifying output of to_h" do
|
@@ -125,7 +125,7 @@ describe 'StrictStruct' do
|
|
125
125
|
hash = k.to_h
|
126
126
|
hash[:x] = 'bar'
|
127
127
|
|
128
|
-
k.x.
|
128
|
+
expect(k.x).to eq 'foo'
|
129
129
|
end
|
130
130
|
end
|
131
131
|
|
@@ -135,7 +135,7 @@ describe 'StrictStruct' do
|
|
135
135
|
|
136
136
|
a_foo = foo.new(bar: 'baz', baz: 3)
|
137
137
|
|
138
|
-
a_foo.to_h.
|
138
|
+
expect(a_foo.to_h).to eq({ bar: 'baz', baz: 3 })
|
139
139
|
end
|
140
140
|
|
141
141
|
it "is possible to override, and reuse using super" do
|
@@ -147,7 +147,7 @@ describe 'StrictStruct' do
|
|
147
147
|
|
148
148
|
a_foo = foo.new(bar: 'baz', baz: 3)
|
149
149
|
|
150
|
-
a_foo.to_h.
|
150
|
+
expect(a_foo.to_h).to eq({ bar: 'baz', baz: 3, pirate: 'captain' })
|
151
151
|
end
|
152
152
|
end
|
153
153
|
|
@@ -157,7 +157,7 @@ describe 'StrictStruct' do
|
|
157
157
|
|
158
158
|
a_foo = foo.new(bar: 'baz', baz: 3)
|
159
159
|
|
160
|
-
a_foo.to_hash.
|
160
|
+
expect(a_foo.to_hash).to eq({ bar: 'baz', baz: 3 })
|
161
161
|
end
|
162
162
|
end
|
163
163
|
|
@@ -171,8 +171,8 @@ describe 'StrictStruct' do
|
|
171
171
|
|
172
172
|
a_foo = foo.new(bar: 'baz', baz: 3)
|
173
173
|
|
174
|
-
a_foo.to_h.
|
175
|
-
a_foo.to_hash.
|
174
|
+
expect(a_foo.to_h).to eq({ bar: 'baz', baz: 3, pirate: 'captain' })
|
175
|
+
expect(a_foo.to_hash).to eq({ bar: 'baz', baz: 3 })
|
176
176
|
end
|
177
177
|
end
|
178
178
|
|
@@ -186,8 +186,8 @@ describe 'StrictStruct' do
|
|
186
186
|
|
187
187
|
a_foo = foo.new(bar: 'baz', baz: 3)
|
188
188
|
|
189
|
-
a_foo.to_h.
|
190
|
-
a_foo.to_hash.
|
189
|
+
expect(a_foo.to_h).to eq({ bar: 'baz', baz: 3, pirate: 'captain' })
|
190
|
+
expect(a_foo.to_hash).to eq({ bar: 'baz', baz: 3, pirate: 'captain' })
|
191
191
|
end
|
192
192
|
end
|
193
193
|
|
@@ -205,9 +205,8 @@ describe 'StrictStruct' do
|
|
205
205
|
|
206
206
|
a_foo = foo.new(bar: 'baz', baz: 3)
|
207
207
|
|
208
|
-
a_foo.to_h.
|
209
|
-
a_foo.to_hash.
|
210
|
-
|
208
|
+
expect(a_foo.to_h).to eq({ bar: 'baz', baz: 3, pirate: 'captain', samurai: 'sword' })
|
209
|
+
expect(a_foo.to_hash).to eq({ bar: 'baz', baz: 3, pirate: 'captain' })
|
211
210
|
end
|
212
211
|
end
|
213
212
|
|
@@ -218,7 +217,7 @@ describe 'StrictStruct' do
|
|
218
217
|
a_foo = foo.new(bar: 'baz')
|
219
218
|
another_foo = foo.new(bar: 'baz')
|
220
219
|
|
221
|
-
a_foo.
|
220
|
+
expect(a_foo).to eq another_foo
|
222
221
|
end
|
223
222
|
|
224
223
|
it "returns false if the values are not the same" do
|
@@ -227,7 +226,7 @@ describe 'StrictStruct' do
|
|
227
226
|
a_foo = foo.new(bar: 'baz')
|
228
227
|
another_foo = foo.new(bar: 'not_baz')
|
229
228
|
|
230
|
-
a_foo.
|
229
|
+
expect(a_foo).to_not eq another_foo
|
231
230
|
end
|
232
231
|
|
233
232
|
it "returns false if two values are different StrictStructs" do
|
@@ -237,14 +236,14 @@ describe 'StrictStruct' do
|
|
237
236
|
chicken = chicken_class.new(wings: 2)
|
238
237
|
plane = plane_class.new(wings: 2)
|
239
238
|
|
240
|
-
plane.
|
239
|
+
expect(plane).to_not eq chicken
|
241
240
|
end
|
242
241
|
end
|
243
242
|
|
244
243
|
describe '#hash' do
|
245
244
|
it "returns the same value for two empty objects" do
|
246
245
|
empty = StrictStruct.new
|
247
|
-
empty.new.hash.
|
246
|
+
expect(empty.new.hash).to eq empty.new.hash
|
248
247
|
end
|
249
248
|
|
250
249
|
it "returns a different value for two non-empty objects with different content" do
|
@@ -253,7 +252,7 @@ describe 'StrictStruct' do
|
|
253
252
|
first = container.new(a: 1)
|
254
253
|
second = container.new(a: 2)
|
255
254
|
|
256
|
-
first.hash.
|
255
|
+
expect(first.hash).to_not eq second.hash
|
257
256
|
end
|
258
257
|
|
259
258
|
it "returns the same value for two non-empty objects with the same content" do
|
@@ -262,10 +261,16 @@ describe 'StrictStruct' do
|
|
262
261
|
first = container.new(a: 1)
|
263
262
|
second = container.new(a: 1)
|
264
263
|
|
265
|
-
first.hash.
|
264
|
+
expect(first.hash).to eq second.hash
|
266
265
|
end
|
267
266
|
|
268
|
-
|
269
|
-
|
267
|
+
it "returns a different value for two different objects" do |variable|
|
268
|
+
container = StrictStruct.new(:a)
|
269
|
+
|
270
|
+
first = container.new(a: 1)
|
271
|
+
second = container.new(a: 2)
|
272
|
+
|
273
|
+
expect(first.hash).to_not eq second.hash
|
274
|
+
end
|
270
275
|
end
|
271
276
|
end
|
data/strict_struct.gemspec
CHANGED
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: 1.0.0
|
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-
|
11
|
+
date: 2014-05-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -42,16 +42,16 @@ dependencies:
|
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '='
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 3.0.0.beta2
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - '='
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 3.0.0.beta2
|
55
55
|
description: Extremely simple value objects
|
56
56
|
email:
|
57
57
|
- markijbema@gmail.com
|
@@ -62,7 +62,6 @@ files:
|
|
62
62
|
- ".gitignore"
|
63
63
|
- ".travis.yml"
|
64
64
|
- Gemfile
|
65
|
-
- Gemfile.travis
|
66
65
|
- LICENSE.txt
|
67
66
|
- README.md
|
68
67
|
- Rakefile
|