immutable-struct 2.3.0.rc1 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/immutable-struct.rb +3 -9
- data/spec/immutable_struct_spec.rb +17 -62
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90315cc7aa75a487cf037aefed966ae52c2a1826
|
4
|
+
data.tar.gz: 6f6c1750a942a9ab4ec45fa015fb0f75c03d3036
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf5c63f1f22ccd08744d02ae6afa583bbd901df66fdb8f68798d7b689bb3c54f487f7279f877f9525c03e98eec824d667d27769e37a828b87f279a4421a2e602
|
7
|
+
data.tar.gz: 22507c2b9558426d29574c7aa8d1206894a1b9d00d5c89f679765b5ac89c42f9b40425967afe831b71362f6c684db5c14b7eea3657d10296594cf07cc6553cd1
|
data/Gemfile.lock
CHANGED
data/lib/immutable-struct.rb
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
# will be evaluated as if it were inside a class definition, allowing you
|
7
7
|
# to add methods, include or extend modules, or do whatever else you want.
|
8
8
|
class ImmutableStruct
|
9
|
-
VERSION='2.3.0
|
9
|
+
VERSION='2.3.0' #:nodoc:
|
10
10
|
# Create a new class with the given read-only attributes.
|
11
11
|
#
|
12
12
|
# attributes:: list of symbols or strings that can be used to create attributes.
|
@@ -124,17 +124,11 @@ class ImmutableStruct
|
|
124
124
|
klass.class_exec(imethods) do |imethods|
|
125
125
|
define_method(:to_h) do
|
126
126
|
imethods.inject({}) do |hash, method|
|
127
|
-
next hash if [
|
127
|
+
next hash if [:==, :eql?, :merge, :hash].include?(method)
|
128
128
|
hash.merge(method.to_sym => self.send(method))
|
129
129
|
end
|
130
130
|
end
|
131
|
-
|
132
|
-
define_method(:to_json) do |*args|
|
133
|
-
imethods.inject({}) do |hash, method|
|
134
|
-
next hash if [:to_json, :to_hash, :to_h, :==, :eql?, :merge, :hash].include?(method)
|
135
|
-
hash.merge(method.to_sym => self.send(method))
|
136
|
-
end.to_json(*args)
|
137
|
-
end
|
131
|
+
alias_method :to_hash, :to_h
|
138
132
|
end
|
139
133
|
klass
|
140
134
|
end
|
@@ -152,6 +152,20 @@ describe ImmutableStruct do
|
|
152
152
|
}
|
153
153
|
end
|
154
154
|
end
|
155
|
+
context "to_hash is its alias" do
|
156
|
+
it "is identical" do
|
157
|
+
klass = ImmutableStruct.new(:name, :minor?, :location, [:aliases]) do
|
158
|
+
def nick_name
|
159
|
+
'bob'
|
160
|
+
end
|
161
|
+
def location_near?(other_location)
|
162
|
+
false
|
163
|
+
end
|
164
|
+
end
|
165
|
+
instance = klass.new(name: "Rudy", minor: "ayup", aliases: [ "Rudyard", "Roozoola" ])
|
166
|
+
instance.to_h.should == instance.to_hash
|
167
|
+
end
|
168
|
+
end
|
155
169
|
|
156
170
|
context "no-arg method that uses to_h" do
|
157
171
|
it "blows up" do
|
@@ -159,76 +173,18 @@ describe ImmutableStruct do
|
|
159
173
|
def nick_name
|
160
174
|
'bob'
|
161
175
|
end
|
162
|
-
def
|
163
|
-
to_h.
|
176
|
+
def to_json
|
177
|
+
to_h.to_json
|
164
178
|
end
|
165
179
|
end
|
166
180
|
instance = klass.new(name: "Rudy", minor: "ayup", aliases: [ "Rudyard", "Roozoola" ])
|
167
181
|
expect {
|
168
|
-
instance.
|
182
|
+
instance.to_json.should == instance.to_h.to_json
|
169
183
|
}.to raise_error(SystemStackError)
|
170
184
|
end
|
171
185
|
end
|
172
186
|
end
|
173
187
|
|
174
|
-
|
175
|
-
describe "to_json" do
|
176
|
-
it 'recursively handles to_json' do
|
177
|
-
klass = ImmutableStruct.new(:name, :subclass)
|
178
|
-
|
179
|
-
subklass = ImmutableStruct.new(:number) do
|
180
|
-
def triple
|
181
|
-
3 * number
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
instance = klass.new(
|
186
|
-
name: 'Rudy',
|
187
|
-
subclass: subklass.new(
|
188
|
-
number: 1,
|
189
|
-
)
|
190
|
-
)
|
191
|
-
instance.to_json.should ==
|
192
|
-
"{\"name\":\"Rudy\",\"subclass\":{\"number\":1,\"triple\":3}}"
|
193
|
-
end
|
194
|
-
|
195
|
-
it 'handles arrays gracefully' do
|
196
|
-
klass = ImmutableStruct.new(:name, [:aliases] )
|
197
|
-
|
198
|
-
instance = klass.new(
|
199
|
-
name: 'Rudy',
|
200
|
-
aliases: ['Jones', 'Silly']
|
201
|
-
)
|
202
|
-
instance.to_json.should ==
|
203
|
-
"{\"name\":\"Rudy\",\"aliases\":[\"Jones\",\"Silly\"]}"
|
204
|
-
end
|
205
|
-
|
206
|
-
it 'recursively handles arrays to_json' do
|
207
|
-
klass = ImmutableStruct.new(:name, [:subclasses])
|
208
|
-
|
209
|
-
subklass = ImmutableStruct.new(:number) do
|
210
|
-
def triple
|
211
|
-
3 * number
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
instance = klass.new(
|
216
|
-
name: 'Rudy',
|
217
|
-
subclasses:
|
218
|
-
[
|
219
|
-
subklass.new(
|
220
|
-
number: 2
|
221
|
-
),
|
222
|
-
subklass.new(
|
223
|
-
number: 3,
|
224
|
-
)
|
225
|
-
]
|
226
|
-
)
|
227
|
-
instance.to_json.should ==
|
228
|
-
"{\"name\":\"Rudy\",\"subclasses\":[{\"number\":2,\"triple\":6},{\"number\":3,\"triple\":9}]}"
|
229
|
-
end
|
230
|
-
end
|
231
|
-
|
232
188
|
describe "merge" do
|
233
189
|
it "returns a new object as a result of merging attributes" do
|
234
190
|
klass = ImmutableStruct.new(:food, :snacks, :butter)
|
@@ -338,6 +294,5 @@ describe ImmutableStruct do
|
|
338
294
|
|
339
295
|
end
|
340
296
|
|
341
|
-
|
342
297
|
end
|
343
298
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: immutable-struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.3.0
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stitch Fix Engineering
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2017-08-
|
13
|
+
date: 2017-08-14 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -94,9 +94,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
94
94
|
version: '0'
|
95
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
96
|
requirements:
|
97
|
-
- - "
|
97
|
+
- - ">="
|
98
98
|
- !ruby/object:Gem::Version
|
99
|
-
version:
|
99
|
+
version: '0'
|
100
100
|
requirements: []
|
101
101
|
rubyforge_project:
|
102
102
|
rubygems_version: 2.4.5
|