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 CHANGED
@@ -11,3 +11,7 @@
11
11
 
12
12
  * Rename has_one and has_many to embeds_one and embeds_many to make room
13
13
  for associations.
14
+
15
+ # 0.11
16
+
17
+ * .key now emulates the DataMapper.property method.
data/README.md CHANGED
@@ -37,14 +37,14 @@ Define a model:
37
37
  require 'structure'
38
38
 
39
39
  class Person < Structure
40
- key :name
41
- key :age, :type => Integer
42
- has_many :friends
43
- has_one :partner
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
- Conjure an object:
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":null,"age":null,"friends":[]}],"partner":null}
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=>nil, :age=>nil, :friends=>[], :partner=>nil}>
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, :type => Array, :default => []
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, :type => Structure
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. Available options are:
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, :type => String
37
- # key :authors, :type => Array, :default => []
39
+ # key :title
40
+ # key :authors, Array, :default => []
38
41
  # end
39
- def key(name, options={})
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
@@ -1,3 +1,3 @@
1
1
  class Structure
2
- VERSION = '0.10.0'
2
+ VERSION = '0.11.0'
3
3
  end
data/spec/models/book.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  class Book < Structure
2
2
  key :title
3
- key :authors, :type => Array, :value => []
3
+ key :authors, Array, :value => []
4
4
  end
@@ -1,5 +1,5 @@
1
1
  class Person < Structure
2
2
  key :name
3
- key :age, :type => Integer
3
+ key :age, Integer
4
4
  embeds_many :friends
5
5
  end
@@ -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, :type => Object
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, :type => String, :default => 0
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, :type => Boolean, :default => true
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, :type => Boolean, :default => false
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, :type => Boolean
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, :type => Hash
143
+ Person.key :education, Hash
144
144
  end
145
145
 
146
146
  context "when setting to a value that is not a Hash" do
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 10
7
+ - 11
8
8
  - 0
9
- version: 0.10.0
9
+ version: 0.11.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Paper Cavalier