structure 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 => 'John'
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
- Use ORM-esque association idioms:
57
+ Boast ORM-esque association idioms:
57
58
 
58
- p2 = Person.new :name => 'Jane'
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 type == Hash
60
+ elsif [Hash, Structure].include? type
61
61
  lambda do |value|
62
- unless value.is_a? Hash
63
- raise TypeError, "#{value} is not a Hash"
62
+ unless value.is_a? type
63
+ raise TypeError, "#{value} is not a #{type}"
64
64
  end
65
65
  value
66
66
  end
@@ -1,3 +1,3 @@
1
- class Sucker
2
- VERSION = '0.4.0'
1
+ class Structure
2
+ VERSION = '0.5.0'
3
3
  end
@@ -37,7 +37,7 @@ describe Structure do
37
37
  end
38
38
  end
39
39
 
40
- describe "#default_attributes" do
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
@@ -4,7 +4,7 @@ require 'structure/version'
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "structure"
7
- s.version = Sucker::VERSION
7
+ s.version = Structure::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Paper Cavalier"]
10
10
  s.email = ["code@papercavalier.com"]
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 4
7
+ - 5
8
8
  - 0
9
- version: 0.4.0
9
+ version: 0.5.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Paper Cavalier