plain_old_model 0.1.3 → 0.1.4
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.
@@ -30,7 +30,8 @@ module PlainOldModel
|
|
30
30
|
associations.each do |association|
|
31
31
|
attr_name = association.attr_name
|
32
32
|
if attributes.include?(attr_name)
|
33
|
-
|
33
|
+
merged_hash = merge_association_instance_variables_with_attributes(association, attr_name, attributes)
|
34
|
+
value = association.create_value_from_attributes(merged_hash)
|
34
35
|
set_attribute(attr_name, value)
|
35
36
|
attributes = attributes.delete_if { |key, value| key.to_s == attr_name.to_s }
|
36
37
|
end
|
@@ -86,6 +87,7 @@ module PlainOldModel
|
|
86
87
|
end
|
87
88
|
|
88
89
|
private
|
90
|
+
|
89
91
|
def sanitize_attributes(attributes)
|
90
92
|
sanitized_attributes = {}
|
91
93
|
attributes.each do |k, v|
|
@@ -111,5 +113,38 @@ module PlainOldModel
|
|
111
113
|
instance_variable_set("@#{attr_name}".to_sym, value)
|
112
114
|
end
|
113
115
|
end
|
116
|
+
|
117
|
+
def merge_association_instance_variables_with_attributes(association, attr_name, attributes)
|
118
|
+
association_instance = send(attr_name)
|
119
|
+
if association.class == HasOneAssociation
|
120
|
+
instance_hash = create_association_hash(association_instance,{})
|
121
|
+
merged_result = instance_hash.deep_merge(attributes[attr_name])
|
122
|
+
elsif association.class == HasManyAssociation
|
123
|
+
association_instance_array = []
|
124
|
+
if association_instance.nil?
|
125
|
+
merged_result = attributes[attr_name]
|
126
|
+
else
|
127
|
+
for i in 0..association_instance.length-1
|
128
|
+
instance_hash = create_association_hash(association_instance[i],{})
|
129
|
+
association_instance_array << instance_hash.deep_merge(attributes[attr_name][i])
|
130
|
+
end
|
131
|
+
merged_result = association_instance_array
|
132
|
+
end
|
133
|
+
end
|
134
|
+
merged_result
|
135
|
+
end
|
136
|
+
|
137
|
+
def create_association_hash(association_instance,association_instance_hash)
|
138
|
+
unless association_instance.nil?
|
139
|
+
association_instance.instance_variables.each do |var|
|
140
|
+
if association_instance.instance_variable_get(var).instance_variables.length > 0
|
141
|
+
association_instance_hash[var.to_s.delete("@").to_sym] = create_association_hash(association_instance.instance_variable_get(var),{})
|
142
|
+
else
|
143
|
+
association_instance_hash[var.to_s.delete("@").to_sym] = association_instance.instance_variable_get(var)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
association_instance_hash
|
148
|
+
end
|
114
149
|
end
|
115
150
|
end
|
data/spec/lib/base_spec.rb
CHANGED
@@ -11,7 +11,9 @@ describe PlainOldModel::Base do
|
|
11
11
|
end
|
12
12
|
it "should assign value to the attr_reader attributes/ read only attribute" do
|
13
13
|
@address = Address.new
|
14
|
-
@address.
|
14
|
+
@address.fname = "first value"
|
15
|
+
@address.assign_attributes({:lname => "second value", :read_test => 'This should be assigned'})
|
16
|
+
@address.fname.should == "first value"
|
15
17
|
@address.read_test.should == "This should be assigned"
|
16
18
|
end
|
17
19
|
it "should assign value to the attr_writer attributes" do
|
@@ -66,7 +68,41 @@ describe PlainOldModel::Base do
|
|
66
68
|
@address = Address.new({:fname => "first value", :lname => "second value", :country => {:code => "In", :name => "India"}, :read_test => 'This should be assigned',:write_test => "this shd be available"})
|
67
69
|
@address.read_test.should == "This should be assigned"
|
68
70
|
end
|
71
|
+
it "should override assigned attributes" do
|
72
|
+
@address = Address.new({:fname => "first value", :lname => "second value", :country => {:code => "In", :name => "India"}, :read_test => 'This should be assigned',:write_test => "this shd be available"})
|
73
|
+
@address.assign_attributes({:fname => "replaced first value", :lname => "replaced second value", :country => {:code => "USA", :name => "United States"}})
|
74
|
+
@address.fname.should == "replaced first value"
|
75
|
+
@address.country.code.should == "USA"
|
76
|
+
@address.country.name.should == "United States"
|
77
|
+
@address.read_test.should == "This should be assigned"
|
78
|
+
end
|
79
|
+
it "should not override unassigned nested attributes" do
|
80
|
+
@address = Address.new({:fname => "first value", :lname => "second value", :country => {:code => "In", :name => "India"}, :read_test => 'This should be assigned',:write_test => "this shd be available"})
|
81
|
+
@address.assign_attributes({:fname => "replaced first value", :lname => "replaced second value"})
|
82
|
+
@address.fname.should == "replaced first value"
|
83
|
+
@address.country.code.should == "In"
|
84
|
+
@address.country.name.should == "India"
|
85
|
+
@address.read_test.should == "This should be assigned"
|
86
|
+
end
|
87
|
+
it "should not override unassigned nested attributes' values" do
|
88
|
+
@address = Address.new({:fname => "first value", :lname => "second value", :country => {:code => "In", :name => "India", :continent => {:name => "asia", :desc => {:this => "is a test", :actual_desc => "is another test"}}}, :read_test => 'This should be assigned',:write_test => "this shd be available"})
|
89
|
+
@address.assign_attributes({:fname => "replaced first value", :lname => "replaced second value", :country => {:name => "United States", :continent => {:desc => {:this => "is a replacement", :actual_desc => "is another replacement"}}}})
|
90
|
+
@address.fname.should == "replaced first value"
|
91
|
+
@address.country.code.should == "In"
|
92
|
+
@address.country.name.should == "United States"
|
93
|
+
@address.country.continent.name.should == "asia"
|
94
|
+
@address.read_test.should == "This should be assigned"
|
95
|
+
end
|
96
|
+
it "should create assigned nested attributes" do
|
97
|
+
@address = Address.new({:fname => "first value", :lname => "second value", :read_test => 'This should be assigned',:write_test => "this shd be available"})
|
98
|
+
@address.assign_attributes({:fname => "replaced first value", :lname => "replaced second value", :country => {:code => "In", :name => "India"} })
|
99
|
+
@address.fname.should == "replaced first value"
|
100
|
+
@address.country.code.should == "In"
|
101
|
+
@address.country.name.should == "India"
|
102
|
+
@address.read_test.should == "This should be assigned"
|
103
|
+
end
|
69
104
|
end
|
105
|
+
|
70
106
|
describe "has_many" do
|
71
107
|
it "should create a new instance and assign attributes for each value in array" do
|
72
108
|
@person = Person.new({ addresses: [{ fname: "first name 1", lname: "last name 1"}, { fname: "first name 2", lname: "last name 2"}]})
|
@@ -78,7 +114,6 @@ describe PlainOldModel::Base do
|
|
78
114
|
@person.addresses[1].fname.should == "first name 2"
|
79
115
|
@person.addresses[1].lname.should == "last name 2"
|
80
116
|
end
|
81
|
-
|
82
117
|
it "should not alter the params passed in" do
|
83
118
|
passed_params = { addresses: [{ fname: "first name 1", lname: "last name 1"}, { fname: "first name 2", lname: "last name 2"}]}
|
84
119
|
@person = Person.new(passed_params)
|
@@ -87,7 +122,6 @@ describe PlainOldModel::Base do
|
|
87
122
|
@person.addresses.first.fname.should == "first name 1"
|
88
123
|
passed_params.should == { addresses: [{ fname: "first name 1", lname: "last name 1"}, { fname: "first name 2", lname: "last name 2"}]}
|
89
124
|
end
|
90
|
-
|
91
125
|
it "should create each class via factory_method if one is specified" do
|
92
126
|
@person = Person.new({ phones: [{ number: '423-5841'}, {number: '383-9139'}]})
|
93
127
|
@person.phones.length.should == 2
|
@@ -95,11 +129,18 @@ describe PlainOldModel::Base do
|
|
95
129
|
@person.phones[0].extension.should == 'set_via_factory'
|
96
130
|
@person.phones[1].extension.should == 'set_via_factory'
|
97
131
|
end
|
132
|
+
it "should not override unassigned nested attributes' values" do
|
133
|
+
@person = Person.new({ addresses: [{ fname: "first name 1", lname: "last name 1", :country => {:code => "In", :name => "India", :continent => {:name => "asia", :desc => {:this => "is a test", :actual_desc => "is another test"}}}}, { fname: "first name 2", lname: "last name 2"}]})
|
134
|
+
@person.assign_attributes({ addresses: [{ fname: "first name 1", :country => {:name => "United States", :continent => {:desc => {:this => "is a replacement", :actual_desc => "is another replacement"}}}}, { fname: "first name 2", lname: "last name 2"}]})
|
135
|
+
@person.addresses.first.lname.should == "last name 1"
|
136
|
+
@person.addresses.last.lname.should == "last name 2"
|
137
|
+
@person.addresses.first.country.name.should == "United States"
|
138
|
+
@person.addresses.first.country.continent.name.should == "asia"
|
139
|
+
end
|
98
140
|
end
|
99
141
|
end
|
100
142
|
end
|
101
143
|
|
102
|
-
|
103
144
|
class Person < PlainOldModel::Base
|
104
145
|
attr_accessor :fname, :lname, :address
|
105
146
|
has_many :addresses
|
@@ -122,7 +163,6 @@ class Address < PlainOldModel::Base
|
|
122
163
|
attr_writer :write_test
|
123
164
|
|
124
165
|
has_one :country
|
125
|
-
|
126
166
|
end
|
127
167
|
|
128
168
|
class Country < PlainOldModel::Base
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: plain_old_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-04-01 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
@@ -130,7 +130,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
130
130
|
version: '0'
|
131
131
|
segments:
|
132
132
|
- 0
|
133
|
-
hash:
|
133
|
+
hash: 3516179471459458128
|
134
134
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
135
|
none: false
|
136
136
|
requirements:
|
@@ -139,10 +139,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
139
139
|
version: '0'
|
140
140
|
segments:
|
141
141
|
- 0
|
142
|
-
hash:
|
142
|
+
hash: 3516179471459458128
|
143
143
|
requirements: []
|
144
144
|
rubyforge_project:
|
145
|
-
rubygems_version: 1.8.
|
145
|
+
rubygems_version: 1.8.25
|
146
146
|
signing_key:
|
147
147
|
specification_version: 3
|
148
148
|
summary: This gem is created to cater the projects which do not require a backend/database,
|