simple_model 1.2.1 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +2 -1
- data/lib/simple_model/attributes.rb +13 -2
- data/lib/simple_model/version.rb +1 -1
- data/spec/attributes_spec.rb +1 -1
- metadata +1 -1
data/.rspec
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
--color
|
1
|
+
--color
|
2
|
+
--debug
|
@@ -45,14 +45,21 @@ module SimpleModel
|
|
45
45
|
end
|
46
46
|
|
47
47
|
# Returns attribute that have defaults in a hash: {:attrbute => "default value"}
|
48
|
+
# Checks for alias attributes to ensure they are not overwritten
|
48
49
|
def attributes_with_for_init(attrs)
|
49
50
|
d = attrs.with_indifferent_access
|
50
51
|
self.class.defined_attributes.each do |k,v|
|
51
|
-
|
52
|
+
if allow_set_default?(d,k,v)
|
53
|
+
d[k] = fetch_default_value(v[:default])
|
54
|
+
end
|
52
55
|
end
|
53
56
|
d
|
54
57
|
end
|
55
58
|
|
59
|
+
def allow_set_default?(d,k,v)
|
60
|
+
(v[:default] && v[:initialize] && (d[k].blank? && (self.class.alias_attributes[k].blank? || d.key?(self.class.alias_attributes[k]) && d[self.class.alias_attributes[k]].blank?)))
|
61
|
+
end
|
62
|
+
|
56
63
|
module ClassMethods
|
57
64
|
# Creates a new instance where the attributes store is set to object
|
58
65
|
# provided, which allows one to pass a session store hash or any other
|
@@ -74,9 +81,12 @@ module SimpleModel
|
|
74
81
|
new
|
75
82
|
end
|
76
83
|
|
84
|
+
def alias_attributes
|
85
|
+
@alias_attributes ||= {}.with_indifferent_access
|
86
|
+
end
|
77
87
|
|
78
88
|
def defined_attributes
|
79
|
-
@defined_attributes ||= {}
|
89
|
+
@defined_attributes ||= {}.with_indifferent_access
|
80
90
|
end
|
81
91
|
|
82
92
|
def defined_attributes=defined_attributes
|
@@ -179,6 +189,7 @@ module SimpleModel
|
|
179
189
|
# Creates alias setter and getter for the supplied attribute using the supplied alias
|
180
190
|
# See spec for example.
|
181
191
|
def alias_attribute(new_alias,attribute)
|
192
|
+
alias_attributes[attribute] = new_alias
|
182
193
|
define_method(new_alias) do
|
183
194
|
self.send(attribute)
|
184
195
|
end
|
data/lib/simple_model/version.rb
CHANGED
data/spec/attributes_spec.rb
CHANGED