structure 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -1
- data/benchmark.rb +24 -0
- data/lib/structure.rb +3 -0
- data/lib/structure/version.rb +1 -1
- data/spec/structure_spec.rb +22 -0
- metadata +4 -3
data/README.md
CHANGED
@@ -57,6 +57,10 @@ Typecast:
|
|
57
57
|
p1.age
|
58
58
|
=> 28
|
59
59
|
|
60
|
+
Check for presence:
|
61
|
+
p1.age?
|
62
|
+
=> true
|
63
|
+
|
60
64
|
Embed other structures:
|
61
65
|
|
62
66
|
p2 = Person.new
|
@@ -73,7 +77,7 @@ Load the JSON seamlessly back into Ruby:
|
|
73
77
|
|
74
78
|
person = JSON.parse(json)
|
75
79
|
person.friends.first
|
76
|
-
=> #<Person:0x0000010107d030 @attributes={:name=>nil, :age=>nil, :friends=>[], :partner=>nil}
|
80
|
+
=> #<Person:0x0000010107d030 @attributes={:name=>nil, :age=>nil, :friends=>[], :partner=>nil}>
|
77
81
|
|
78
82
|
Throw in some Active Model modules:
|
79
83
|
|
data/benchmark.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'benchmark'
|
2
|
+
require 'ostruct'
|
3
|
+
require_relative 'lib/structure'
|
4
|
+
|
5
|
+
class Person1 < Structure
|
6
|
+
key :name
|
7
|
+
end
|
8
|
+
|
9
|
+
class Person2 < Struct.new(:name)
|
10
|
+
end
|
11
|
+
|
12
|
+
class Person3
|
13
|
+
attr_accessor :name
|
14
|
+
end
|
15
|
+
|
16
|
+
n = 100000
|
17
|
+
Benchmark.bm do |x|
|
18
|
+
x.report('Structure') { n.times { Person1.new(:name => 'John') } }
|
19
|
+
x.report('Structure') { n.times { Person1.new.name = 'John' } }
|
20
|
+
x.report('OpenStruct') { n.times { OpenStruct.new(:name => 'John') } }
|
21
|
+
x.report('Struct') { n.times { Person2.new('John') } }
|
22
|
+
x.report('Class') { n.times { Person3.new.name = 'John' } }
|
23
|
+
x.report('Hash') { n.times { { name: 'John' } } }
|
24
|
+
end
|
data/lib/structure.rb
CHANGED
@@ -90,6 +90,9 @@ class Structure
|
|
90
90
|
define_method("#{name}=") do |value|
|
91
91
|
@attributes[name] = value.nil? ? nil : typecast.call(value)
|
92
92
|
end
|
93
|
+
|
94
|
+
# Define a "presence" (for lack of a better term) method
|
95
|
+
define_method("#{name}?") { !!@attributes[name] }
|
93
96
|
end
|
94
97
|
end
|
95
98
|
|
data/lib/structure/version.rb
CHANGED
data/spec/structure_spec.rb
CHANGED
@@ -187,6 +187,28 @@ describe Structure do
|
|
187
187
|
end
|
188
188
|
end
|
189
189
|
|
190
|
+
describe "attribute presence" do
|
191
|
+
context "when the value of an attribute is nil" do
|
192
|
+
it "returns false" do
|
193
|
+
person.name?.should be_false
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
context "when the value of an attribute is false" do
|
198
|
+
it "returns false" do
|
199
|
+
person.instance_variable_get(:@attributes)[:name] = false
|
200
|
+
person.name?.should be_false
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
context "when the value of an attribute is set to a value other than false or nil" do
|
205
|
+
it "returns true" do
|
206
|
+
person.instance_variable_get(:@attributes)[:name] = "John"
|
207
|
+
person.name?.should be_true
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
190
212
|
describe ".new" do
|
191
213
|
context "when attributes are specified" do
|
192
214
|
it "initializes the object with those attributes" do
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 9
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.9.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Paper Cavalier
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-06-
|
17
|
+
date: 2011-06-29 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -79,6 +79,7 @@ files:
|
|
79
79
|
- LICENSE
|
80
80
|
- README.md
|
81
81
|
- Rakefile
|
82
|
+
- benchmark.rb
|
82
83
|
- lib/structure.rb
|
83
84
|
- lib/structure/json.rb
|
84
85
|
- lib/structure/version.rb
|