structure 0.10.0 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -0
- data/README.md +7 -7
- data/lib/structure.rb +18 -9
- data/lib/structure/version.rb +1 -1
- data/spec/models/book.rb +1 -1
- data/spec/models/person.rb +1 -1
- data/spec/structure_spec.rb +6 -6
- metadata +2 -2
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -37,14 +37,14 @@ Define a model:
|
|
37
37
|
require 'structure'
|
38
38
|
|
39
39
|
class Person < Structure
|
40
|
-
key
|
41
|
-
key
|
42
|
-
|
43
|
-
|
40
|
+
key :name, :default => "John Doe"
|
41
|
+
key :age, Integer
|
42
|
+
embeds_many :friends
|
43
|
+
embeds_one :partner
|
44
44
|
end
|
45
45
|
```
|
46
46
|
|
47
|
-
|
47
|
+
Create an object:
|
48
48
|
|
49
49
|
```ruby
|
50
50
|
p1 = Person.new :name => 'Gilles'
|
@@ -79,11 +79,11 @@ Talk JSON:
|
|
79
79
|
require 'structure/json'
|
80
80
|
|
81
81
|
json = p1.to_json
|
82
|
-
=> {"json_class":"Person","name":"John","age":28,"friends":[{"json_class":"Person","name":
|
82
|
+
=> {"json_class":"Person","name":"John","age":28,"friends":[{"json_class":"Person","name":"John Doe","age":null,"friends":[]}],"partner":null}
|
83
83
|
|
84
84
|
person = JSON.parse(json)
|
85
85
|
person.friends.first
|
86
|
-
=> #<Person:0x0000010107d030 @attributes={:name=>
|
86
|
+
=> #<Person:0x0000010107d030 @attributes={:name=>"John Doe", :age=>nil, :friends=>[], :partner=>nil}>
|
87
87
|
```
|
88
88
|
|
89
89
|
Quack Active Model:
|
data/lib/structure.rb
CHANGED
@@ -16,33 +16,42 @@ class Structure
|
|
16
16
|
# A shortcut to define an attribute that represents an array of other
|
17
17
|
# objects, possibly structures.
|
18
18
|
def embeds_many(name)
|
19
|
-
key name,
|
19
|
+
key name, Array, :default => []
|
20
20
|
end
|
21
21
|
|
22
22
|
# A shortcut to define an attribute that represents another structure.
|
23
23
|
def embeds_one(name)
|
24
|
-
key name,
|
24
|
+
key name, Structure
|
25
25
|
end
|
26
26
|
|
27
27
|
# Defines an attribute key.
|
28
28
|
#
|
29
|
-
# Takes a name and an optional hash of options.
|
29
|
+
# Takes a name, an optional type, and an optional hash of options.
|
30
|
+
#
|
31
|
+
# The type can be Array, Boolean, Float, Hash, Integer, String, or
|
32
|
+
# Structure. If none is specified, it defaults to String.
|
33
|
+
#
|
34
|
+
# Available options are:
|
30
35
|
#
|
31
|
-
# * :type, which can be Array, Boolean, Float, Hash, Integer, String,
|
32
|
-
# or Structure. If not specified, type defaults to String.
|
33
36
|
# * :default, which sets the default value for the attribute.
|
34
37
|
#
|
35
38
|
# class Book
|
36
|
-
# key :title
|
37
|
-
# key :authors,
|
39
|
+
# key :title
|
40
|
+
# key :authors, Array, :default => []
|
38
41
|
# end
|
39
|
-
def key(name,
|
42
|
+
def key(name, *args)
|
40
43
|
name = name.to_sym
|
44
|
+
options = if args.last.is_a? Hash
|
45
|
+
args.pop
|
46
|
+
else
|
47
|
+
{}
|
48
|
+
end
|
49
|
+
type = args.shift || String
|
50
|
+
|
41
51
|
if method_defined?(name)
|
42
52
|
raise NameError, "#{name} is already defined"
|
43
53
|
end
|
44
54
|
|
45
|
-
type = options[:type] || String
|
46
55
|
unless TYPES.include? type
|
47
56
|
raise TypeError, "#{type} is not a valid type"
|
48
57
|
end
|
data/lib/structure/version.rb
CHANGED
data/spec/models/book.rb
CHANGED
data/spec/models/person.rb
CHANGED
data/spec/structure_spec.rb
CHANGED
@@ -23,7 +23,7 @@ describe Structure do
|
|
23
23
|
context "when an invalid type is specified" do
|
24
24
|
it "raises an error" do
|
25
25
|
expect do
|
26
|
-
Person.key :location,
|
26
|
+
Person.key :location, Object
|
27
27
|
end.to raise_error TypeError
|
28
28
|
end
|
29
29
|
end
|
@@ -31,7 +31,7 @@ describe Structure do
|
|
31
31
|
context "when default value is not of the specified type" do
|
32
32
|
it "raises an error" do
|
33
33
|
expect do
|
34
|
-
Person.key :location,
|
34
|
+
Person.key :location, String, :default => 0
|
35
35
|
end.to raise_error TypeError
|
36
36
|
end
|
37
37
|
end
|
@@ -88,7 +88,7 @@ describe Structure do
|
|
88
88
|
context "when default value is true" do
|
89
89
|
it "does not raise an invalid type error" do
|
90
90
|
expect do
|
91
|
-
Person.key :single,
|
91
|
+
Person.key :single, Boolean, :default => true
|
92
92
|
end.not_to raise_error
|
93
93
|
end
|
94
94
|
end
|
@@ -96,14 +96,14 @@ describe Structure do
|
|
96
96
|
context "when default value is false" do
|
97
97
|
it "does not raise an invalid type error" do
|
98
98
|
expect do
|
99
|
-
Person.key :married,
|
99
|
+
Person.key :married, Boolean, :default => false
|
100
100
|
end.not_to raise_error
|
101
101
|
end
|
102
102
|
end
|
103
103
|
|
104
104
|
context "when typecasting a set value" do
|
105
105
|
before(:all) do
|
106
|
-
Person.key :vegetarian,
|
106
|
+
Person.key :vegetarian, Boolean
|
107
107
|
end
|
108
108
|
|
109
109
|
it "typecasts 'true' to true" do
|
@@ -140,7 +140,7 @@ describe Structure do
|
|
140
140
|
|
141
141
|
context "when type is Hash" do
|
142
142
|
before(:all) do
|
143
|
-
Person.key :education,
|
143
|
+
Person.key :education, Hash
|
144
144
|
end
|
145
145
|
|
146
146
|
context "when setting to a value that is not a Hash" do
|