rrod 1.0.0.alpha.8 → 1.0.0.alpha.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21bcd59f68557a964682ed550e6f066b4b62d8c7
4
- data.tar.gz: cb7eb976f82092d7013d48418a2de2dbcb1d4a7d
3
+ metadata.gz: 84fe091ae38ffdc6b135ebe27a64b909d105c7cb
4
+ data.tar.gz: 3961bd9dc0a7a76f08b2c12254f7fc8b8528ba3a
5
5
  SHA512:
6
- metadata.gz: ebced7c3a04e6efe2a2b31bae0ee25eadebad2f2120dae03cc6ff646f98f4c7ca4e1a575398ae5a8655d4d69b79757121b4d9b2d3f473386602f414b087e2101
7
- data.tar.gz: 084cdd4c9358b4c0ef399667bb5126b59594cec7f806490911865b1ecc37ebd16fb81feedcaeba8509f50821b4e244d1240cc65554fd6eda3aefd9531c451cec
6
+ metadata.gz: 734a1f634fd1da6d7ec3f199ecce30599cd443fcabdff3be19be5c7be11682aa34d52de755afa929fc0526526c9504f93fae8a03fdb2ed75504af9cb5faff94a
7
+ data.tar.gz: 613516afd9e7612c8ffa6113548a8f91c2385ccf97348a9697524346e979ae3ad11e770c1a40e5b1b976411ff1f65e1154e1b2cec7b93bf929ece0e8f01670f2
@@ -11,9 +11,10 @@ module Rrod
11
11
  end
12
12
 
13
13
  def initialize(attributes = {})
14
- @attributes = {}
14
+ @attributes = {}.with_indifferent_access
15
+ self._parent = attributes.delete(:_parent)
15
16
  self.magic_methods = attributes.keys
16
- self.attributes = attributes
17
+ self._attributes = attributes
17
18
  changes_applied
18
19
  end
19
20
 
@@ -22,15 +23,13 @@ module Rrod
22
23
  end
23
24
 
24
25
  def id=(value)
25
- id_will_change! unless id == value
26
- robject.key = value
26
+ write_attribute(:id, robject.key = value)
27
27
  end
28
28
 
29
29
  # Returns a new hash with all of the object's attributes.
30
30
  # @return [Hash] the object's attributes
31
31
  def attributes
32
32
  self.class.attributes.keys.inject({}) { |acc, key|
33
- # @attributes.keys.inject({}) { |acc, key|
34
33
  acc.tap { |hash|
35
34
  next hash unless self.class.attributes.keys.include?(key.to_s)
36
35
  hash[key] = public_send(key)
@@ -41,16 +40,14 @@ module Rrod
41
40
  # Mass assign the attributes of the object.
42
41
  # @param [Hash] the attributes to mass assign
43
42
  def attributes=(attrs)
44
- attrs.each do |key, value|
45
- public_send "#{key}=", value
46
- end
43
+ self._attributes = attrs
47
44
  end
48
45
 
49
46
  # Read a single attribute of the object.
50
47
  # @param [Symbol, String] the key of the attribute to read
51
48
  # @return the attribute at the given key
52
49
  def read_attribute(key)
53
- @attributes[key.to_s] || read_default(key)
50
+ @attributes[key] || read_default(key)
54
51
  end
55
52
  alias :[] :read_attribute
56
53
 
@@ -58,18 +55,23 @@ module Rrod
58
55
  # @param [Symbol, String] the key of the attribute to write
59
56
  # @param the value to write to attributes
60
57
  def write_attribute(key, value)
61
- public_send("#{key}_will_change!") unless read_attribute(key) == value
62
- cast_attribute(key, value)
58
+ send("#{key}_will_change!") if changing_attribute?(key, value)
59
+ @attributes[key] = cast_attribute(key, value)
63
60
  end
64
61
  alias :[]= :write_attribute
65
62
 
66
63
  def cast_attribute(key, value)
67
- @attributes[key.to_s] = self.class.cast_attribute(key, value, self)
64
+ self.class.cast_attribute(key, value, self)
65
+ end
66
+
67
+ def changing_attribute?(key, value)
68
+ @attributes[key] != cast_attribute(key, value)
68
69
  end
69
70
 
70
71
  def read_default(key)
71
72
  method = "#{key}_default"
72
- cast_attribute(key, public_send(method)) if respond_to?(method)
73
+ return unless respond_to?(method)
74
+ @attributes[key] = cast_attribute(key, send(method))
73
75
  end
74
76
 
75
77
  def nested_model?
@@ -107,6 +109,12 @@ module Rrod
107
109
  magic_methods.include? args.first.to_s
108
110
  end
109
111
 
112
+ # to be run without the callback
113
+ def _attributes=(attrs)
114
+ attrs.each do |key, value|
115
+ public_send "#{key}=", value
116
+ end
117
+ end
110
118
  end
111
119
  end
112
120
  end
@@ -20,7 +20,7 @@ module Rrod
20
20
  end
21
21
 
22
22
  def collection=(collection)
23
- message = "#{collection.inspect} does not respond to :each"
23
+ message = "Object type #{collection.class} does not respond to :each"
24
24
  raise InvalidCollectionTypeError.new(message) unless collection.respond_to?(:each)
25
25
  collection.map { |member| push member }
26
26
  rescue InvalidMemberTypeError => e
@@ -43,16 +43,14 @@ module Rrod
43
43
  def rrod_cast(value, parent=nil)
44
44
  return if value.nil?
45
45
 
46
- cast_model = case value
46
+ case value
47
47
  when Rrod::Model
48
- value
48
+ value.tap { |m| m._parent = parent }
49
49
  when Hash
50
- new(value)
50
+ new value.merge(_parent: parent)
51
51
  else
52
52
  raise UncastableObjectError.new("#{value.inspect} cannot be rrod_cast") unless Hash === value
53
53
  end
54
-
55
- cast_model.tap { |m| m._parent = parent }
56
54
  end
57
55
 
58
56
  end
@@ -15,7 +15,7 @@ module Rrod
15
15
  def to_json(options={})
16
16
  MultiJson.dump as_json(options)
17
17
  end
18
-
18
+
19
19
  end
20
20
  end
21
21
  end
data/lib/rrod/version.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  Rrod = Module.new unless defined? Rrod
2
- Rrod::VERSION = '1.0.0.alpha.8'
2
+ Rrod::VERSION = '1.0.0.alpha.9'
@@ -94,15 +94,20 @@ describe Rrod::Model do
94
94
 
95
95
  describe "dirty tracking" do
96
96
  let(:model) { Class.new(Car) { attribute :wheels, Integer, default: 4 } }
97
- let(:instance) { model.new }
97
+ let(:instance) { model.new(make: 'Jeep') }
98
98
 
99
99
  it "will track if an attribute is changing" do
100
100
  instance.wheels = 5
101
101
  expect(instance.wheels_changed?).to be true
102
102
  end
103
103
 
104
- it "will not track if an attribute is not changing" do
105
- instance.wheels = 4
104
+ it "will not track if an attribute is set from a default" do
105
+ expect(instance.wheels).to eq 4
106
+ expect(instance.wheels_changed?).to be false
107
+ end
108
+
109
+ it "will not track if an attribute has not changed" do
110
+ instance.make = 'Jeep'
106
111
  expect(instance.wheels_changed?).to be false
107
112
  end
108
113
  end
@@ -19,7 +19,7 @@ describe Rrod::Model::Schema do
19
19
  end
20
20
 
21
21
  it "defines a default for the attribute if provided as a proc" do
22
- expect(instance.comments).to eq([instance.build_comment])
22
+ expect(instance.comments).to eq([instance.build_comment.stringify_keys])
23
23
  end
24
24
 
25
25
  context "subclassing" do
@@ -118,4 +118,9 @@ describe Rrod::Model::Schema do
118
118
  expect(instance.pets.first.attributes).not_to have_key('id')
119
119
  end
120
120
 
121
+ it "will properly nest grandparents" do
122
+ instance.pets = [pet = Pet.new]
123
+ expect(instance.pets.first.vaccinations.first.pet).to eq pet
124
+ end
125
+
121
126
  end
@@ -10,8 +10,9 @@ end
10
10
 
11
11
  class Vaccination
12
12
  include Rrod::Model
13
+ nested_in :pet
13
14
 
14
- attribute :type, String
15
+ attribute :type, String, default: -> { 'Temper' unless pet.friendly? }
15
16
  attribute :when, Date
16
17
  end
17
18
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rrod
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha.8
4
+ version: 1.0.0.alpha.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hunter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-04 00:00:00.000000000 Z
11
+ date: 2015-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: riak-client