lib/chozo/varia_model.rb in chozo-0.4.2 vs lib/chozo/varia_model.rb in chozo-0.5.0
- old
+ new
@@ -9,18 +9,18 @@
ASSIGNMENT_MODES = [
:whitelist,
:carefree
]
- # @return [HashWithIndifferentAccess]
+ # @return [Hashie::Mash]
def attributes
@attributes ||= Hashie::Mash.new
end
- # @return [HashWithIndifferentAccess]
+ # @return [Hashie::Mash]
def validations
- @validations ||= HashWithIndifferentAccess.new
+ @validations ||= Hashie::Mash.new
end
# @return [Symbol]
def assignment_mode
@assignment_mode ||= :whitelist
@@ -71,11 +71,11 @@
errors = Array.new
types = types.uniq
matches = false
types.each do |type|
- if model.attributes.dig(key).is_a?(type)
+ if model.get_attribute(key).is_a?(type)
matches = true
break
end
end
@@ -92,11 +92,11 @@
# @param [VariaModel] model
# @param [String] key
#
# @return [Array]
def validate_required(model, key)
- if model.attributes.dig(key).nil?
+ if model.get_attribute(key).nil?
[ :error, "A value is required for attribute: '#{key}'" ]
else
[ :ok, "" ]
end
end
@@ -136,21 +136,21 @@
def define_mimic_methods(name, options = {})
fun_name = name.split('.').first
class_eval do
define_method fun_name do
- self.attributes[fun_name]
+ _attributes_[fun_name]
end
define_method "#{fun_name}=" do |value|
value = if options[:coerce].is_a?(Proc)
options[:coerce].call(value)
else
value
end
- self.attributes[fun_name] = value
+ _attributes_[fun_name] = value
end
end
end
end
@@ -159,15 +159,10 @@
base.extend(ClassMethods)
end
end
# @return [HashWithIndifferentAccess]
- def attributes
- @attributes ||= self.class.attributes.dup
- end
-
- # @return [HashWithIndifferentAccess]
def validate
self.class.validations.each do |attr_path, validations|
validations.each do |validation|
status, messages = validation.call(self, attr_path)
@@ -194,32 +189,41 @@
# @return [HashWithIndifferentAccess]
def errors
@errors ||= HashWithIndifferentAccess.new
end
+ # Assigns the attributes of a model from a given hash of attributes.
+ #
+ # If the assignment mode is set to `:whitelist`, then only the values of keys which have a
+ # corresponding attribute definition on the model will be set. All other keys will have their
+ # values ignored.
+ #
+ # If the assignment mode is set to `:carefree`, then the attributes hash will be populated
+ # with any key/values that are provided.
+ #
+ # @param [Hash] new_attrs
def mass_assign(new_attrs = {})
case self.class.assignment_mode
when :whitelist
whitelist_assign(new_attrs)
when :carefree
carefree_assign(new_attrs)
end
end
- alias_method :attributes=, :mass_assign
# @param [#to_s] key
#
# @return [Object]
def get_attribute(key)
- self.attributes.dig(key.to_s)
+ _attributes_.dig(key.to_s)
end
alias_method :[], :get_attribute
# @param [#to_s] key
# @param [Object] value
def set_attribute(key, value)
- self.attributes.deep_merge!(attributes.class.from_dotted_path(key.to_s, value))
+ _attributes_.deep_merge!(Hashie::Mash.from_dotted_path(key.to_s, value))
end
alias_method :[]=, :set_attribute
# @param [#to_hash] hash
#
@@ -235,21 +239,24 @@
def from_json(data)
mass_assign(MultiJson.decode(data))
self
end
- # @return [Hash]
- def to_hash
- self.attributes
+ # The storage hash containing all of the key/values for this object's attributes
+ #
+ # @return [Hashie::Mash]
+ def _attributes_
+ @_attributes_ ||= self.class.attributes.dup
end
+ alias_method :to_hash, :_attributes_
# @option options [Boolean] :symbolize_keys
# @option options [Class, Symbol, String] :adapter
#
# @return [String]
def to_json(options = {})
- MultiJson.encode(self.attributes, options)
+ MultiJson.encode(_attributes_, options)
end
alias_method :as_json, :to_json
protected
@@ -261,10 +268,10 @@
end
private
def carefree_assign(new_attrs = {})
- attributes.deep_merge!(new_attrs)
+ _attributes_.deep_merge!(new_attrs)
end
def whitelist_assign(new_attrs = {})
self.class.attributes.dotted_paths.each do |dotted_path|
value = new_attrs.dig(dotted_path)