hamsterdam 1.0.8 → 1.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hamsterdam.rb +13 -1
- data/lib/hamsterdam/version.rb +1 -1
- data/spec/hamsterdam_spec.rb +25 -1
- metadata +2 -2
data/lib/hamsterdam.rb
CHANGED
@@ -112,6 +112,19 @@ module Hamsterdam
|
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
115
|
+
def self.safe_create(values=Hamsterdam.hash)
|
116
|
+
h = field_names.inject(Hamsterdam.hash) do |memo, field_name|
|
117
|
+
if values.has_key?(field_name)
|
118
|
+
memo.put(field_name, values[field_name])
|
119
|
+
elsif values.has_key?(field_name.to_s)
|
120
|
+
memo.put(field_name.to_s, values[field_name.to_s])
|
121
|
+
else
|
122
|
+
memo
|
123
|
+
end
|
124
|
+
end
|
125
|
+
new(h)
|
126
|
+
end
|
127
|
+
|
115
128
|
def merge(values)
|
116
129
|
self.class.new(@data.merge(ensure_expected_hash(values)))
|
117
130
|
end
|
@@ -167,7 +180,6 @@ module Hamsterdam
|
|
167
180
|
end
|
168
181
|
|
169
182
|
def flesh_out(data)
|
170
|
-
# binding.pry
|
171
183
|
fnames = self.class.field_names
|
172
184
|
data = symbolize_keys(data)
|
173
185
|
miss = fnames - data.keys
|
data/lib/hamsterdam/version.rb
CHANGED
data/spec/hamsterdam_spec.rb
CHANGED
@@ -63,7 +63,7 @@ describe "Hamsterdam structures" do
|
|
63
63
|
struct.bottom.should == "all the way down"
|
64
64
|
end
|
65
65
|
|
66
|
-
it "can be built with underlying persistent data structure hashes
|
66
|
+
it "can be built with underlying persistent data structure hashes" do
|
67
67
|
struct = struct_class.new(Hamsterdam.hash(top: 10, bottom: "low"))
|
68
68
|
struct.should be
|
69
69
|
struct.top.should == 10
|
@@ -275,4 +275,28 @@ describe "Hamsterdam structures" do
|
|
275
275
|
val.internal_hash.should == Hamsterdam.hash(foo: 3, bar: 4)
|
276
276
|
end
|
277
277
|
end
|
278
|
+
|
279
|
+
describe "alternate constructor method that ignores invalid keys (vs raising an error)" do
|
280
|
+
let(:the_struct) { Hamsterdam::Struct.define(:foo, :bar) }
|
281
|
+
|
282
|
+
it "ignores unknown keys" do
|
283
|
+
val = the_struct.safe_create(:foo => "the foo", "bar" => "the bar", :baz => "the baz")
|
284
|
+
val.foo.should == "the foo"
|
285
|
+
val.bar.should == "the bar"
|
286
|
+
|
287
|
+
val.internal_hash.should == Hamsterdam.hash(foo: "the foo", bar: "the bar")
|
288
|
+
end
|
289
|
+
|
290
|
+
it "does not require anything be passed" do
|
291
|
+
val = the_struct.safe_create
|
292
|
+
val.foo.should be_nil
|
293
|
+
val.bar.should be_nil
|
294
|
+
end
|
295
|
+
|
296
|
+
it "can be built with underlying persistent data structure hashes" do
|
297
|
+
val = the_struct.safe_create(Hamsterdam.hash(no: "ye", foo: "the foo"))
|
298
|
+
val.foo.should == "the foo"
|
299
|
+
val.bar.should be_nil
|
300
|
+
end
|
301
|
+
end
|
278
302
|
end
|
metadata
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
name: hamsterdam
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.0.
|
5
|
+
version: 1.0.9
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- David Crosby
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: hamster
|