structure 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -39,7 +39,7 @@ Define a model:
39
39
  class Person < Structure
40
40
  key :name
41
41
  key :age, :type => Integer
42
- key :friends, :type => Array, :value => []
42
+ key :friends, :type => Array, :default => []
43
43
  end
44
44
 
45
45
  Typecast values:
@@ -59,14 +59,14 @@ Dump good-looking JSON:
59
59
  require 'structure/json'
60
60
 
61
61
  json = p1.to_json
62
- => {"json_class":"Person","name":"John","age":28,"friends":[{"json_class":"Person","name": null,"age":null,"friends":[]}]}
62
+ => {"json_class":"Person","name":"John","age":28,"friends":[{"json_class":"Person","name": "Jane","age":null,"friends":[]}]}
63
63
 
64
- Load the JSON in a different app back into Ruby seamlessly, provided you
65
- have the same models defined there:
64
+ Load the JSON elsewhere back into Ruby seamlessly, provided you have the same
65
+ models set up:
66
66
 
67
67
  person = JSON.parse(json)
68
- person.friends.first.class
69
- => Person
68
+ person.friends.first
69
+ => #<Person:0x0000010107d030 @attributes={:name=>"Jane", :age=>nil, :friends=>[#<Person:0x0000010107d030 ...>]}, @modifiable=true>
70
70
 
71
71
  Types
72
72
  -----
@@ -1,3 +1,3 @@
1
1
  class Sucker
2
- VERSION = '0.3.0'
2
+ VERSION = '0.3.1'
3
3
  end
data/lib/structure.rb CHANGED
@@ -11,8 +11,6 @@ class Structure
11
11
 
12
12
  TYPES = [Array, Boolean, Float, Hash, Integer, String]
13
13
 
14
- @@default_attributes = {}
15
-
16
14
  # Defines an attribute key.
17
15
  #
18
16
  # Takes a name and an optional hash of options. Available options are:
@@ -42,7 +40,7 @@ class Structure
42
40
  raise TypeError, "#{default} is not #{%w{AEIOU}.include?(type.to_s[0]) ? 'an' : 'a'} #{type}"
43
41
  end
44
42
 
45
- @@default_attributes[name] = default
43
+ default_attributes[name] = default
46
44
 
47
45
  module_eval do
48
46
 
@@ -112,10 +110,14 @@ class Structure
112
110
 
113
111
  private
114
112
 
113
+ def self.default_attributes
114
+ @default_attributes ||= {}
115
+ end
116
+
115
117
  def initialize_attributes
116
118
  @attributes =
117
- @@default_attributes.inject({}) do |attributes, (key, value)|
118
- attributes[key] = value
119
+ self.class.default_attributes.inject({}) do |attributes, (key, value)|
120
+ attributes[key] = value.is_a?(Array) ? value.dup : value
119
121
  attributes
120
122
  end
121
123
  end
@@ -0,0 +1,4 @@
1
+ class Book < Structure
2
+ key :title
3
+ key :authors, :type => Array, :value => []
4
+ end
@@ -49,6 +49,16 @@ describe Structure do
49
49
  end
50
50
  end
51
51
 
52
+ describe "#default_attributes" do
53
+ it "returns the default attributes for the structure" do
54
+ Person.send(:default_attributes).should == { :name => nil,
55
+ :age => nil,
56
+ :friends => [] }
57
+ Book.send(:default_attributes).should == { :title => nil,
58
+ :authors => nil }
59
+ end
60
+ end
61
+
52
62
  describe "attribute getter" do
53
63
  it "returns the value of the attribute" do
54
64
  person.instance_variable_get(:@attributes)[:name] = 'Joe'
@@ -56,9 +66,12 @@ describe Structure do
56
66
  end
57
67
 
58
68
  context "when type is Array and default value is []" do
69
+ let(:friend) { Person.new }
70
+
59
71
  it "supports the `<<' idiom" do
60
- person.friends << Person.new
72
+ person.friends << friend
61
73
  person.friends.count.should eql 1
74
+ friend.friends.count.should eql 0
62
75
  end
63
76
  end
64
77
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 0
9
- version: 0.3.0
8
+ - 1
9
+ version: 0.3.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Paper Cavalier
@@ -68,6 +68,7 @@ files:
68
68
  - lib/structure.rb
69
69
  - lib/structure/json.rb
70
70
  - lib/structure/version.rb
71
+ - spec/models/book.rb
71
72
  - spec/models/person.rb
72
73
  - spec/spec_helper.rb
73
74
  - spec/structure/json_spec.rb
@@ -106,6 +107,7 @@ signing_key:
106
107
  specification_version: 3
107
108
  summary: A better Struct
108
109
  test_files:
110
+ - spec/models/book.rb
109
111
  - spec/models/person.rb
110
112
  - spec/spec_helper.rb
111
113
  - spec/structure/json_spec.rb