collapsium 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -58,4 +58,20 @@ describe Collapsium::UberHash do
58
58
  x = ::Collapsium::UberHash.new
59
59
  expect(x.respond_to?(:prototype_match)).to be_truthy
60
60
  end
61
+
62
+ it "creates an UberHash when duplicated" do
63
+ x = ::Collapsium::UberHash.new
64
+ y = x.dup
65
+ expect(y.is_a?(::Collapsium::UberHash)).to be_truthy
66
+ end
67
+
68
+ it "creates nested UberHashes from a nested Hash input" do
69
+ data = {
70
+ some: {
71
+ nested: true,
72
+ }
73
+ }
74
+ x = ::Collapsium::UberHash.new(data)
75
+ expect(x[:some].is_a?(::Collapsium::UberHash)).to be_truthy
76
+ end
61
77
  end
@@ -0,0 +1,248 @@
1
+ require 'spec_helper'
2
+ require_relative '../lib/collapsium/viral_capabilities'
3
+
4
+ module TestModule
5
+ extend ::Collapsium::ViralCapabilities
6
+
7
+ def find_me
8
+ end
9
+ end # TestModule
10
+
11
+ class PrependedHash < Hash
12
+ prepend TestModule
13
+ end
14
+
15
+ class IncludedHash < Hash
16
+ include TestModule
17
+ end
18
+
19
+ class ExtendedHash < Hash
20
+ extend TestModule
21
+ end
22
+
23
+ class DirectPrependedHash < Hash
24
+ prepend ::Collapsium::ViralCapabilities
25
+
26
+ def find_me
27
+ end
28
+ end
29
+
30
+ class DirectIncludedHash < Hash
31
+ include ::Collapsium::ViralCapabilities
32
+
33
+ def find_me
34
+ end
35
+ end
36
+
37
+ class DirectExtendedHash < Hash
38
+ extend ::Collapsium::ViralCapabilities
39
+
40
+ def find_me
41
+ end
42
+ end
43
+
44
+ class LoggingHash < Hash
45
+ include ::Collapsium::Support::HashMethods
46
+ class << self
47
+ include ::Collapsium::Support::Methods
48
+ end
49
+
50
+ attr_accessor :each_called
51
+
52
+ # Overwrite :each method, so that it remembers when it's been called. This is
53
+ # based on the knowledge that the module's way of enhancing Hash values calls
54
+ # :each on parameters, i.e. that the :merge! call in the test case below ends
55
+ # calling :each.
56
+ # Or rather, it should. The test case checks whether it does.
57
+ wrap_method(self, :each) do |super_method, *args, &block|
58
+ @each_called ||= 0
59
+ @each_called += 1
60
+ result = super_method.call(*args, &block)
61
+ result.each_called = @each_called
62
+ next result
63
+ end
64
+ end
65
+
66
+ module ViralityModule
67
+ extend ::Collapsium::ViralCapabilities
68
+ include ::Collapsium::Support::Methods
69
+
70
+ def virality(value)
71
+ # Wrap :delete to become a no-op
72
+ wrap_method(value, :delete) do
73
+ next true
74
+ end
75
+ return value
76
+ end
77
+ end
78
+
79
+ describe ::Collapsium::ViralCapabilities do
80
+ context PrependedHash do
81
+ let(:tester) do
82
+ x = PrependedHash.new
83
+ x.merge!(foo: { bar: { baz: true } })
84
+ end
85
+
86
+ before do
87
+ expect(tester.respond_to?(:find_me)).to be_truthy
88
+ end
89
+
90
+ it "replicates itself" do
91
+ expect(tester[:foo].respond_to?(:find_me)).to be_truthy
92
+ expect(tester[:foo][:bar].respond_to?(:find_me)).to be_truthy
93
+ end
94
+ end
95
+
96
+ context IncludedHash do
97
+ let(:tester) do
98
+ x = IncludedHash.new
99
+ x.merge!(foo: { bar: { baz: true } })
100
+ end
101
+
102
+ before do
103
+ expect(tester.respond_to?(:find_me)).to be_truthy
104
+ end
105
+
106
+ it "replicates itself" do
107
+ expect(tester[:foo].respond_to?(:find_me)).to be_truthy
108
+ expect(tester[:foo][:bar].respond_to?(:find_me)).to be_truthy
109
+ end
110
+ end
111
+
112
+ context ExtendedHash do
113
+ let(:tester) do
114
+ x = ExtendedHash.new
115
+ x.merge!(foo: { bar: { baz: true } })
116
+ end
117
+
118
+ it "does not receive viral capabilities" do
119
+ expect(tester.respond_to?(:find_me)).to be_falsy
120
+ end
121
+ end
122
+
123
+ context DirectPrependedHash do
124
+ let(:tester) do
125
+ x = DirectPrependedHash.new
126
+ x.merge!(foo: { bar: { baz: true } })
127
+ end
128
+
129
+ before do
130
+ expect(tester.respond_to?(:find_me)).to be_truthy
131
+ end
132
+
133
+ it "replicates itself" do
134
+ expect(tester[:foo].respond_to?(:find_me)).to be_truthy
135
+ end
136
+ end
137
+
138
+ context DirectIncludedHash do
139
+ let(:tester) do
140
+ x = DirectIncludedHash.new
141
+ x.merge!(foo: { bar: { baz: true } })
142
+ end
143
+
144
+ before do
145
+ expect(tester.respond_to?(:find_me)).to be_truthy
146
+ end
147
+
148
+ it "replicates itself" do
149
+ expect(tester[:foo].respond_to?(:find_me)).to be_truthy
150
+ expect(tester[:foo][:bar].respond_to?(:find_me)).to be_truthy
151
+ end
152
+ end
153
+
154
+ context DirectExtendedHash do
155
+ let(:tester) do
156
+ x = DirectExtendedHash.new
157
+ x.merge!(foo: { bar: { baz: true } })
158
+ end
159
+
160
+ before do
161
+ expect(tester.respond_to?(:find_me)).to be_truthy
162
+ end
163
+
164
+ it "replicates itself" do
165
+ expect(tester[:foo].respond_to?(:find_me)).to be_truthy
166
+ expect(tester[:foo][:bar].respond_to?(:find_me)).to be_truthy
167
+ end
168
+ end
169
+
170
+ context "writing" do
171
+ let(:tester) { DirectExtendedHash.new }
172
+
173
+ it "enhances values when writing" do
174
+ data = LoggingHash.new
175
+ # rubocop:disable Performance/RedundantMerge
176
+ data.merge!(bar: true)
177
+ # rubocop:enable Performance/RedundantMerge
178
+
179
+ # This calls a write method ([]=) and should call :each on data.
180
+ tester[:foo] = data
181
+ expect(data.each_called).to eql 1
182
+ end
183
+ end
184
+
185
+ context "non-class based enhancements" do
186
+ let(:tester) do
187
+ x = {}
188
+ x.extend(TestModule)
189
+ x.merge!(foo: { bar: { baz: true } })
190
+ end
191
+
192
+ before do
193
+ expect(tester.respond_to?(:find_me)).to be_truthy
194
+ end
195
+
196
+ it "replicates itself" do
197
+ expect(tester[:foo].respond_to?(:find_me)).to be_truthy
198
+ expect(tester[:foo][:bar].respond_to?(:find_me)).to be_truthy
199
+ end
200
+ end
201
+
202
+ context ViralityModule do
203
+ let(:tester) do
204
+ x = {}
205
+ x.extend(ViralityModule)
206
+ x.merge!(foo: { bar: { baz: true } })
207
+ end
208
+
209
+ before do
210
+ expect(tester.respond_to?(:virality)).to be_truthy
211
+ end
212
+
213
+ it "replicates itself" do
214
+ # Just check that the module went viral; it doesn't check the viral
215
+ # enhancements work.
216
+ expect(tester[:foo].respond_to?(:virality)).to be_truthy
217
+ expect(tester[:foo][:bar].respond_to?(:virality)).to be_truthy
218
+
219
+ # Now also check the viral enhancements. In this case, :delete becomes a
220
+ # no-op.
221
+ expect(tester.delete(:foo)).to be_truthy
222
+ expect(tester[:foo]).not_to be_nil
223
+ expect(tester[:foo].delete(:bar)).to be_truthy
224
+ expect(tester[:foo][:bar]).not_to be_nil
225
+ end
226
+ end
227
+
228
+ context "values already of the same class" do
229
+ let(:innermost) do
230
+ innermost = PrependedHash.new
231
+ innermost.merge!(quux: true)
232
+ end
233
+
234
+ let(:inner) do
235
+ inner = {}
236
+ inner.merge!(foo: true, bar: innermost)
237
+ end
238
+
239
+ let(:tester) do
240
+ PrependedHash.new
241
+ end
242
+
243
+ it "peforms a no-op when adding the right class" do
244
+ tester[:bar] = inner
245
+ expect(tester[:bar][:bar].object_id).to eql innermost.object_id
246
+ end
247
+ end
248
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collapsium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Finkhaeuser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-13 00:00:00.000000000 Z
11
+ date: 2016-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.41'
33
+ version: '0.42'
34
34
  type: :development
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: '0.41'
40
+ version: '0.42'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -114,14 +114,19 @@ files:
114
114
  - Rakefile
115
115
  - collapsium.gemspec
116
116
  - lib/collapsium.rb
117
+ - lib/collapsium/environment_override.rb
117
118
  - lib/collapsium/indifferent_access.rb
118
119
  - lib/collapsium/pathed_access.rb
119
120
  - lib/collapsium/prototype_match.rb
120
121
  - lib/collapsium/recursive_dup.rb
121
122
  - lib/collapsium/recursive_merge.rb
122
123
  - lib/collapsium/recursive_sort.rb
124
+ - lib/collapsium/support/hash_methods.rb
125
+ - lib/collapsium/support/methods.rb
123
126
  - lib/collapsium/uber_hash.rb
124
127
  - lib/collapsium/version.rb
128
+ - lib/collapsium/viral_capabilities.rb
129
+ - spec/environment_override_spec.rb
125
130
  - spec/indifferent_access_spec.rb
126
131
  - spec/pathed_access_spec.rb
127
132
  - spec/prototype_match_spec.rb
@@ -129,7 +134,9 @@ files:
129
134
  - spec/recursive_merge_spec.rb
130
135
  - spec/recursive_sort_spec.rb
131
136
  - spec/spec_helper.rb
137
+ - spec/support_methods_spec.rb
132
138
  - spec/uber_hash_spec.rb
139
+ - spec/viral_capabilities_spec.rb
133
140
  homepage: https://github.com/jfinkhaeuser/collapsium
134
141
  licenses:
135
142
  - MITNFA
@@ -155,6 +162,7 @@ signing_key:
155
162
  specification_version: 4
156
163
  summary: Provides various Hash extensions, and an UberHash class that uses them all.
157
164
  test_files:
165
+ - spec/environment_override_spec.rb
158
166
  - spec/indifferent_access_spec.rb
159
167
  - spec/pathed_access_spec.rb
160
168
  - spec/prototype_match_spec.rb
@@ -162,5 +170,6 @@ test_files:
162
170
  - spec/recursive_merge_spec.rb
163
171
  - spec/recursive_sort_spec.rb
164
172
  - spec/spec_helper.rb
173
+ - spec/support_methods_spec.rb
165
174
  - spec/uber_hash_spec.rb
166
- has_rdoc:
175
+ - spec/viral_capabilities_spec.rb