wahashie 1.2.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.
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Wahashie::Hash do
4
+ it "should be convertible to a Wahashie::Mash" do
5
+ mash = Wahashie::Hash[:some => "hash"].to_mash
6
+ mash.is_a?(Wahashie::Mash).should be_true
7
+ mash.some.should == "hash"
8
+ end
9
+
10
+ it "#stringify_keys! should turn all keys into strings" do
11
+ hash = Wahashie::Hash[:a => "hey", 123 => "bob"]
12
+ hash.stringify_keys!
13
+ hash.should == Wahashie::Hash["a" => "hey", "123" => "bob"]
14
+ end
15
+
16
+ it "#stringify_keys should return a hash with stringified keys" do
17
+ hash = Wahashie::Hash[:a => "hey", 123 => "bob"]
18
+ stringified_hash = hash.stringify_keys
19
+ hash.should == Wahashie::Hash[:a => "hey", 123 => "bob"]
20
+ stringified_hash.should == Wahashie::Hash["a" => "hey", "123" => "bob"]
21
+ end
22
+
23
+ describe '#to_hash' do
24
+ it 'should convert it to a hash with string keys by default' do
25
+ Wahashie::Hash.new.merge(:a => 'hey', :b => 'foo').to_hash.should == {'a' => 'hey', 'b' => 'foo'}
26
+ end
27
+
28
+ it 'should convert to a hash with symbol keys if :symbolize_keys is passed in' do
29
+ Wahashie::Hash.new.merge('a' => 'hey', 'b' => 'doo').to_hash(:symbolize_keys => true).should == {:a => 'hey', :b => 'doo'}
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,281 @@
1
+ require 'spec_helper'
2
+ require 'delegate'
3
+
4
+ describe Wahashie::Mash do
5
+ before(:each) do
6
+ @mash = Wahashie::Mash.new
7
+ end
8
+
9
+ it "should inherit from hash" do
10
+ @mash.is_a?(Hash).should be_true
11
+ end
12
+
13
+ it "should be able to set hash values through method= calls" do
14
+ @mash.test = "abc"
15
+ @mash["test"].should == "abc"
16
+ end
17
+
18
+ it "should be able to retrieve set values through method calls" do
19
+ @mash["test"] = "abc"
20
+ @mash.test.should == "abc"
21
+ end
22
+
23
+ it "should be able to retrieve set values through blocks" do
24
+ @mash["test"] = "abc"
25
+ value = nil
26
+ @mash.[]("test") { |v| value = v }
27
+ value.should == "abc"
28
+ end
29
+
30
+ it "should be able to retrieve set values through blocks with method calls" do
31
+ @mash["test"] = "abc"
32
+ value = nil
33
+ @mash.test { |v| value = v }
34
+ value.should == "abc"
35
+ end
36
+
37
+ it "should test for already set values when passed a ? method" do
38
+ @mash.test?.should be_false
39
+ @mash.test = "abc"
40
+ @mash.test?.should be_true
41
+ end
42
+
43
+ it "should return false on a ? method if a value has been set to nil or false" do
44
+ @mash.test = nil
45
+ @mash.should_not be_test
46
+ @mash.test = false
47
+ @mash.should_not be_test
48
+ end
49
+
50
+ it "should make all [] and []= into strings for consistency" do
51
+ @mash["abc"] = 123
52
+ @mash.key?('abc').should be_true
53
+ @mash["abc"].should == 123
54
+ end
55
+
56
+ it "should have a to_s that is identical to its inspect" do
57
+ @mash.abc = 123
58
+ @mash.to_s.should == @mash.inspect
59
+ end
60
+
61
+ it "should return nil instead of raising an error for attribute-esque method calls" do
62
+ @mash.abc.should be_nil
63
+ end
64
+
65
+ it "should return a Wahashie::Mash when passed a bang method to a non-existenct key" do
66
+ @mash.abc!.is_a?(Wahashie::Mash).should be_true
67
+ end
68
+
69
+ it "should return the existing value when passed a bang method for an existing key" do
70
+ @mash.name = "Bob"
71
+ @mash.name!.should == "Bob"
72
+ end
73
+
74
+ it "#initializing_reader should return a Wahashie::Mash when passed a non-existent key" do
75
+ @mash.initializing_reader(:abc).is_a?(Wahashie::Mash).should be_true
76
+ end
77
+
78
+ it "should allow for multi-level assignment through bang methods" do
79
+ @mash.author!.name = "Michael Bleigh"
80
+ @mash.author.should == Wahashie::Mash.new(:name => "Michael Bleigh")
81
+ @mash.author!.website!.url = "http://www.mbleigh.com/"
82
+ @mash.author.website.should == Wahashie::Mash.new(:url => "http://www.mbleigh.com/")
83
+ end
84
+
85
+ # it "should call super if type is not a key" do
86
+ # @mash.type.should == Wahashie::Mash
87
+ # end
88
+
89
+ it "should return the value if type is a key" do
90
+ @mash.type = "Steve"
91
+ @mash.type.should == "Steve"
92
+ end
93
+
94
+ context "updating" do
95
+ subject {
96
+ described_class.new :first_name => "Michael", :last_name => "Bleigh",
97
+ :details => {:email => "michael@asf.com", :address => "Nowhere road"}
98
+ }
99
+
100
+ describe "#deep_update" do
101
+ it "should recursively Wahashie::Mash Wahashie::Mashes and hashes together" do
102
+ subject.deep_update(:details => {:email => "michael@intridea.com", :city => "Imagineton"})
103
+ subject.first_name.should == "Michael"
104
+ subject.details.email.should == "michael@intridea.com"
105
+ subject.details.address.should == "Nowhere road"
106
+ subject.details.city.should == "Imagineton"
107
+ end
108
+
109
+ it "should make #update deep by default" do
110
+ subject.update(:details => {:address => "Fake street"}).should eql(subject)
111
+ subject.details.address.should == "Fake street"
112
+ subject.details.email.should == "michael@asf.com"
113
+ end
114
+
115
+ it "should clone before a #deep_merge" do
116
+ duped = subject.deep_merge(:details => {:address => "Fake street"})
117
+ duped.should_not eql(subject)
118
+ duped.details.address.should == "Fake street"
119
+ subject.details.address.should == "Nowhere road"
120
+ duped.details.email.should == "michael@asf.com"
121
+ end
122
+
123
+ it "regular #merge should be deep" do
124
+ duped = subject.merge(:details => {:email => "michael@intridea.com"})
125
+ duped.should_not eql(subject)
126
+ duped.details.email.should == "michael@intridea.com"
127
+ duped.details.address.should == "Nowhere road"
128
+ end
129
+ end
130
+
131
+ describe "shallow update" do
132
+ it "should shallowly Wahashie::Mash Wahashie::Mashes and hashes together" do
133
+ subject.shallow_update(:details => {
134
+ :email => "michael@intridea.com", :city => "Imagineton"
135
+ }).should eql(subject)
136
+
137
+ subject.first_name.should == "Michael"
138
+ subject.details.email.should == "michael@intridea.com"
139
+ subject.details.address.should be_nil
140
+ subject.details.city.should == "Imagineton"
141
+ end
142
+
143
+ it "should clone before a #regular_merge" do
144
+ duped = subject.shallow_merge(:details => {:address => "Fake street"})
145
+ duped.should_not eql(subject)
146
+ end
147
+
148
+ it "regular merge should be shallow" do
149
+ duped = subject.shallow_merge(:details => {:address => "Fake street"})
150
+ duped.details.address.should == "Fake street"
151
+ subject.details.address.should == "Nowhere road"
152
+ duped.details.email.should be_nil
153
+ end
154
+ end
155
+
156
+ describe 'delete' do
157
+ it 'should delete with String key' do
158
+ subject.delete('details')
159
+ subject.details.should be_nil
160
+ subject.should_not be_respond_to :details
161
+ end
162
+
163
+ it 'should delete with Symbol key' do
164
+ subject.delete(:details)
165
+ subject.details.should be_nil
166
+ subject.should_not be_respond_to :details
167
+ end
168
+ end
169
+ end
170
+
171
+ it "should convert hash assignments into Wahashie::Mashes" do
172
+ @mash.details = {:email => 'randy@asf.com', :address => {:state => 'TX'} }
173
+ @mash.details.email.should == 'randy@asf.com'
174
+ @mash.details.address.state.should == 'TX'
175
+ end
176
+
177
+ it "should not convert the type of Wahashie::Mashes childs to Wahashie::Mash" do
178
+ class MyMash < Wahashie::Mash
179
+ end
180
+
181
+ record = MyMash.new
182
+ record.son = MyMash.new
183
+ record.son.class.should == MyMash
184
+ end
185
+
186
+ it "should not change the class of Mashes when converted" do
187
+ class SubMash < Wahashie::Mash
188
+ end
189
+
190
+ record = Wahashie::Mash.new
191
+ son = SubMash.new
192
+ record['submash'] = son
193
+ record['submash'].should be_kind_of(SubMash)
194
+ end
195
+
196
+ it "should respect the class when passed a bang method for a non-existent key" do
197
+ record = Wahashie::Mash.new
198
+ record.non_existent!.should be_kind_of(Wahashie::Mash)
199
+
200
+ class SubMash < Wahashie::Mash
201
+ end
202
+
203
+ son = SubMash.new
204
+ son.non_existent!.should be_kind_of(SubMash)
205
+ end
206
+
207
+ it "should respect the class when converting the value" do
208
+ record = Wahashie::Mash.new
209
+ record.details = Wahashie::Mash.new({:email => "randy@asf.com"})
210
+ record.details.should be_kind_of(Wahashie::Mash)
211
+
212
+ class SubMash < Wahashie::Mash
213
+ end
214
+
215
+ son = SubMash.new
216
+ son.details = Wahashie::Mash.new({:email => "randyjr@asf.com"})
217
+ son.details.should be_kind_of(SubMash)
218
+ end
219
+
220
+ describe '#respond_to?' do
221
+ it 'should respond to a normal method' do
222
+ Wahashie::Mash.new.should be_respond_to(:key?)
223
+ end
224
+
225
+ it 'should respond to a set key' do
226
+ Wahashie::Mash.new(:abc => 'def').should be_respond_to(:abc)
227
+ end
228
+ end
229
+
230
+ context "#initialize" do
231
+ it "should convert an existing hash to a Wahashie::Mash" do
232
+ converted = Wahashie::Mash.new({:abc => 123, :name => "Bob"})
233
+ converted.abc.should == 123
234
+ converted.name.should == "Bob"
235
+ end
236
+
237
+ it "should convert hashes recursively into Wahashie::Mashes" do
238
+ converted = Wahashie::Mash.new({:a => {:b => 1, :c => {:d => 23}}})
239
+ converted.a.is_a?(Wahashie::Mash).should be_true
240
+ converted.a.b.should == 1
241
+ converted.a.c.d.should == 23
242
+ end
243
+
244
+ it "should convert hashes in arrays into Wahashie::Mashes" do
245
+ converted = Wahashie::Mash.new({:a => [{:b => 12}, 23]})
246
+ converted.a.first.b.should == 12
247
+ converted.a.last.should == 23
248
+ end
249
+
250
+ it "should convert an existing Wahashie::Mash into a Wahashie::Mash" do
251
+ initial = Wahashie::Mash.new(:name => 'randy', :address => {:state => 'TX'})
252
+ copy = Wahashie::Mash.new(initial)
253
+ initial.name.should == copy.name
254
+ initial.object_id.should_not == copy.object_id
255
+ copy.address.state.should == 'TX'
256
+ copy.address.state = 'MI'
257
+ initial.address.state.should == 'TX'
258
+ copy.address.object_id.should_not == initial.address.object_id
259
+ end
260
+
261
+ it "should accept a default block" do
262
+ initial = Wahashie::Mash.new { |h,i| h[i] = []}
263
+ initial.default_proc.should_not be_nil
264
+ initial.default.should be_nil
265
+ initial.test.should == []
266
+ initial.test?.should be_true
267
+ end
268
+
269
+ describe '.subkey_class' do
270
+ it 'should be able to define a subkey class different from itself' do
271
+ class CustomSubkeyHash < Wahashie::Mash
272
+ def self.subkey_class; Wahashie::Mash end
273
+ end
274
+
275
+ subhash = CustomSubkeyHash.new(:subhash => {:abc => :def})[:subhash]
276
+ subhash.should_not be_kind_of(CustomSubkeyHash)
277
+ subhash.should be_kind_of(Wahashie::Mash)
278
+ end
279
+ end
280
+ end
281
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe Wahashie::Trash do
4
+ class TrashTest < Wahashie::Trash
5
+ property :first_name, :from => :firstName
6
+ end
7
+
8
+ let(:trash) { TrashTest.new }
9
+
10
+ describe 'translating properties' do
11
+ it 'adds the property to the list' do
12
+ TrashTest.properties.should include(:first_name)
13
+ end
14
+
15
+ it 'creates a method for reading the property' do
16
+ trash.should respond_to(:first_name)
17
+ end
18
+
19
+ it 'creates a method for writing the property' do
20
+ trash.should respond_to(:first_name=)
21
+ end
22
+
23
+ it 'creates a method for writing the translated property' do
24
+ trash.should respond_to(:firstName=)
25
+ end
26
+
27
+ it 'does not create a method for reading the translated property' do
28
+ trash.should_not respond_to(:firstName)
29
+ end
30
+ end
31
+
32
+ describe 'writing to properties' do
33
+
34
+ it 'does not write to a non-existent property using []=' do
35
+ lambda{trash['abc'] = 123}.should raise_error(NoMethodError)
36
+ end
37
+
38
+ it 'writes to an existing property using []=' do
39
+ lambda{trash['first_name'] = 'Bob'}.should_not raise_error
40
+ end
41
+
42
+ it 'writes to a translated property using []=' do
43
+ lambda{trash['firstName'] = 'Bob'}.should_not raise_error
44
+ end
45
+
46
+ it 'reads/writes to an existing property using a method call' do
47
+ trash.first_name = 'Franklin'
48
+ trash.first_name.should == 'Franklin'
49
+ end
50
+
51
+ it 'writes to an translated property using a method call' do
52
+ trash.firstName = 'Franklin'
53
+ trash.first_name.should == 'Franklin'
54
+ end
55
+ end
56
+
57
+ describe ' initializing with a Hash' do
58
+ it 'does not initialize non-existent properties' do
59
+ lambda{TrashTest.new(:bork => 'abc')}.should raise_error(NoMethodError)
60
+ end
61
+
62
+ it 'sets the desired properties' do
63
+ TrashTest.new(:first_name => 'Michael').first_name.should == 'Michael'
64
+ end
65
+
66
+ it 'sets the translated properties' do
67
+ TrashTest.new(:firstName => 'Michael').first_name.should == 'Michael'
68
+ end
69
+ end
70
+ end
data/wahashie.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ require File.expand_path('../lib/wahashie/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["originally authored by Michael Bleigh -- no longer supported"]
5
+ gem.email = ["rubygems@cyper.us"]
6
+ gem.description = %q{Wahashie is the 1.x release of Hashie pulled into a separate namespace in order to allow projects that utilize different major versions to coexist.}
7
+ gem.summary = %q{Your friendly neighborhood hash toolkit.}
8
+ gem.homepage = 'https://github.com/intridea/hashie'
9
+
10
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ gem.name = "wahashie"
14
+ gem.require_paths = ['lib']
15
+ gem.version = Wahashie::VERSION
16
+
17
+ gem.add_development_dependency 'rake', '~> 0.9.2'
18
+ gem.add_development_dependency 'rspec', '~> 2.5'
19
+ gem.add_development_dependency 'guard'
20
+ gem.add_development_dependency 'guard-rspec'
21
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wahashie
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: ruby
6
+ authors:
7
+ - originally authored by Michael Bleigh -- no longer supported
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-12-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Wahashie is the 1.x release of Hashie pulled into a separate namespace
70
+ in order to allow projects that utilize different major versions to coexist.
71
+ email:
72
+ - rubygems@cyper.us
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".document"
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - ".yardopts"
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - Guardfile
85
+ - LICENSE
86
+ - README.rdoc
87
+ - Rakefile
88
+ - lib/wahashie.rb
89
+ - lib/wahashie/clash.rb
90
+ - lib/wahashie/dash.rb
91
+ - lib/wahashie/hash.rb
92
+ - lib/wahashie/hash_extensions.rb
93
+ - lib/wahashie/mash.rb
94
+ - lib/wahashie/trash.rb
95
+ - lib/wahashie/version.rb
96
+ - spec/spec.opts
97
+ - spec/spec_helper.rb
98
+ - spec/wahashie/clash_spec.rb
99
+ - spec/wahashie/dash_spec.rb
100
+ - spec/wahashie/hash_spec.rb
101
+ - spec/wahashie/mash_spec.rb
102
+ - spec/wahashie/trash_spec.rb
103
+ - wahashie.gemspec
104
+ homepage: https://github.com/intridea/hashie
105
+ licenses: []
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.6.13
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Your friendly neighborhood hash toolkit.
127
+ test_files:
128
+ - spec/spec.opts
129
+ - spec/spec_helper.rb
130
+ - spec/wahashie/clash_spec.rb
131
+ - spec/wahashie/dash_spec.rb
132
+ - spec/wahashie/hash_spec.rb
133
+ - spec/wahashie/mash_spec.rb
134
+ - spec/wahashie/trash_spec.rb