structure 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +9 -4
- data/lib/structure.rb +4 -4
- data/lib/structure/version.rb +2 -2
- data/spec/structure_spec.rb +16 -2
- data/structure.gemspec +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -41,11 +41,12 @@ Define a model:
|
|
41
41
|
key :name
|
42
42
|
key :age, :type => Integer
|
43
43
|
key :friends, :type => Array, :default => []
|
44
|
+
key :partner, :type => Structure
|
44
45
|
end
|
45
46
|
|
46
47
|
Conjure an object:
|
47
48
|
|
48
|
-
p1 = Person.new :name => '
|
49
|
+
p1 = Person.new :name => 'Gilles'
|
49
50
|
|
50
51
|
Typecast values:
|
51
52
|
|
@@ -53,10 +54,13 @@ Typecast values:
|
|
53
54
|
p1.age
|
54
55
|
=> 28
|
55
56
|
|
56
|
-
|
57
|
+
Boast ORM-esque association idioms:
|
57
58
|
|
58
|
-
p2 = Person.new :name => '
|
59
|
-
p1.friends << p2
|
59
|
+
p2 = Person.new :name => 'Michel'
|
60
|
+
p1.friends << p2 # has many
|
61
|
+
|
62
|
+
p3 = Person.new :name => 'Félix'
|
63
|
+
p1.partner = p3 # has one
|
60
64
|
|
61
65
|
Dump good-looking JSON:
|
62
66
|
|
@@ -107,3 +111,4 @@ Structure supports the following types:
|
|
107
111
|
* Hash
|
108
112
|
* Integer
|
109
113
|
* String
|
114
|
+
* Structure
|
data/lib/structure.rb
CHANGED
@@ -9,7 +9,7 @@ class Structure
|
|
9
9
|
class ::FalseClass; include Boolean; end
|
10
10
|
end
|
11
11
|
|
12
|
-
TYPES = [Array, Boolean, Float, Hash, Integer, String]
|
12
|
+
TYPES = [Array, Boolean, Float, Hash, Integer, String, Structure]
|
13
13
|
|
14
14
|
# Defines an attribute key.
|
15
15
|
#
|
@@ -57,10 +57,10 @@ class Structure
|
|
57
57
|
!!value
|
58
58
|
end
|
59
59
|
end
|
60
|
-
elsif
|
60
|
+
elsif [Hash, Structure].include? type
|
61
61
|
lambda do |value|
|
62
|
-
unless value.is_a?
|
63
|
-
raise TypeError, "#{value} is not a
|
62
|
+
unless value.is_a? type
|
63
|
+
raise TypeError, "#{value} is not a #{type}"
|
64
64
|
end
|
65
65
|
value
|
66
66
|
end
|
data/lib/structure/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
class
|
2
|
-
VERSION = '0.
|
1
|
+
class Structure
|
2
|
+
VERSION = '0.5.0'
|
3
3
|
end
|
data/spec/structure_spec.rb
CHANGED
@@ -37,7 +37,7 @@ describe Structure do
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
describe "
|
40
|
+
describe ".default_attributes" do
|
41
41
|
it "returns the default attributes for the structure" do
|
42
42
|
Person.send(:default_attributes).should == { :name => nil,
|
43
43
|
:age => nil,
|
@@ -143,7 +143,7 @@ describe Structure do
|
|
143
143
|
Person.key :education, :type => Hash
|
144
144
|
end
|
145
145
|
|
146
|
-
context "when setting to a value is not a Hash" do
|
146
|
+
context "when setting to a value that is not a Hash" do
|
147
147
|
it "raises an error" do
|
148
148
|
expect do
|
149
149
|
person.education = 'foo'
|
@@ -152,6 +152,20 @@ describe Structure do
|
|
152
152
|
end
|
153
153
|
end
|
154
154
|
|
155
|
+
context "when type is Structure" do
|
156
|
+
before(:all) do
|
157
|
+
Person.key :father, :type => Structure
|
158
|
+
end
|
159
|
+
|
160
|
+
context "when setting to a value that is not a Structure" do
|
161
|
+
it "raises an error" do
|
162
|
+
expect do
|
163
|
+
person.father = 'foo'
|
164
|
+
end.to raise_error TypeError
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
155
169
|
context "when a default is specified" do
|
156
170
|
it "defaults to that value" do
|
157
171
|
Person.key :location, :default => 'New York'
|
data/structure.gemspec
CHANGED