module VariaModel
Constants
- VERSION
Public Class Methods
# File lib/varia_model.rb, line 158 def included(base) base.extend(ClassMethods) end
Public Instance Methods
The storage hash containing all of the key/values for this object's attributes
@return [Hashie::Mash]
# File lib/varia_model.rb, line 247 def _attributes_ @_attributes_ ||= self.class.attributes.dup end
@return [Hash]
# File lib/varia_model.rb, line 261 def as_json(*) opts = {} opts[JSON.create_id] = self.class.name if JSON.create_id to_hash.merge(opts) end
@return [Hashie::Mash]
# File lib/varia_model.rb, line 190 def errors @errors ||= Hashie::Mash.new end
@param [#to_hash] hash
@return [self]
# File lib/varia_model.rb, line 231 def from_hash(hash) mass_assign(hash.to_hash) self end
@param [String] data
@return [self]
# File lib/varia_model.rb, line 239 def from_json(data) mass_assign(JSON.parse(data)) self end
@param [#to_s] key
@return [Object]
# File lib/varia_model.rb, line 216 def get_attribute(key) eval_as_proc(_attributes_.berks_dig(key.to_s)) 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
# File lib/varia_model.rb, line 204 def mass_assign(new_attrs = {}) case self.class.assignment_mode when :whitelist whitelist_assign(new_attrs) when :carefree carefree_assign(new_attrs) end end
@param [#to_s] key @param [Object] value
# File lib/varia_model.rb, line 223 def set_attribute(key, value) _attributes_.deep_merge!(Attributes.from_dotted_path(key.to_s, value)) end
@option options [Buff::Boolean] :symbolize_keys @option options [Class, Symbol, String] :adapter
@return [String]
# File lib/varia_model.rb, line 256 def to_json(*options) as_json.to_json(*options) end
@return [Buff::Boolean]
# File lib/varia_model.rb, line 185 def valid? validate.empty? end
@return [Hashie::Mash]
# File lib/varia_model.rb, line 164 def validate self.class.validations.each do |attr_path, validations| validations.each do |validation| status, messages = validation.call(self, attr_path) if status == :error if messages.is_a?(Array) messages.each do |message| self.add_error(attr_path, message) end else self.add_error(attr_path, messages) end end end end self.errors end
Protected Instance Methods
@param [String] attribute @param [String] message
# File lib/varia_model.rb, line 276 def add_error(attribute, message) self.errors[attribute] ||= Array.new self.errors[attribute] << message end
Private Instance Methods
# File lib/varia_model.rb, line 291 def carefree_assign(new_attrs = {}) _attributes_.deep_merge!(new_attrs) end
Send call to the given object if it responds to it. If it doesn't, just return the object.
@param [#call]
# File lib/varia_model.rb, line 287 def eval_as_proc(obj) obj.respond_to?(:call) ? obj.call : obj end
# File lib/varia_model.rb, line 295 def whitelist_assign(new_attrs = {}) self.class.attributes.dotted_paths.each do |dotted_path| value = new_attrs.berks_dig(dotted_path) next if value.nil? set_attribute(dotted_path, value) end end