rrod 1.0.0.alpha.8 → 1.0.0.alpha.9
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.
- checksums.yaml +4 -4
- data/lib/rrod/model/attribute_methods.rb +21 -13
- data/lib/rrod/model/collection.rb +1 -1
- data/lib/rrod/model/schema.rb +3 -5
- data/lib/rrod/model/serialization.rb +1 -1
- data/lib/rrod/version.rb +1 -1
- data/spec/rrod/model/attribute_methods_spec.rb +8 -3
- data/spec/rrod/model/schema_spec.rb +6 -1
- data/spec/support/models/person.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84fe091ae38ffdc6b135ebe27a64b909d105c7cb
|
4
|
+
data.tar.gz: 3961bd9dc0a7a76f08b2c12254f7fc8b8528ba3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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
|
-
|
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
|
-
|
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
|
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
|
-
|
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
|
-
|
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
|
-
|
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.
|
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
|
data/lib/rrod/model/schema.rb
CHANGED
@@ -43,16 +43,14 @@ module Rrod
|
|
43
43
|
def rrod_cast(value, parent=nil)
|
44
44
|
return if value.nil?
|
45
45
|
|
46
|
-
|
46
|
+
case value
|
47
47
|
when Rrod::Model
|
48
|
-
value
|
48
|
+
value.tap { |m| m._parent = parent }
|
49
49
|
when Hash
|
50
|
-
new(
|
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
|
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.
|
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
|
105
|
-
instance.wheels
|
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
|
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.
|
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-
|
11
|
+
date: 2015-03-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: riak-client
|