module VariaModel

Constants

VERSION

Public Class Methods

included(base) click to toggle source
# File lib/varia_model.rb, line 158
def included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

[](key)
Alias for: get_attribute
[]=(key, value)
Alias for: set_attribute
_attributes_() click to toggle source

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
Also aliased as: to_hash
as_json(*) click to toggle source

@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
errors() click to toggle source

@return [Hashie::Mash]

# File lib/varia_model.rb, line 190
def errors
  @errors ||= Hashie::Mash.new
end
from_hash(hash) click to toggle source

@param [#to_hash] hash

@return [self]

# File lib/varia_model.rb, line 231
def from_hash(hash)
  mass_assign(hash.to_hash)
  self
end
from_json(data) click to toggle source

@param [String] data

@return [self]

# File lib/varia_model.rb, line 239
def from_json(data)
  mass_assign(JSON.parse(data))
  self
end
get_attribute(key) click to toggle source

@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
Also aliased as: []
mass_assign(new_attrs = {}) click to toggle source

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
set_attribute(key, value) click to toggle source

@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
Also aliased as: []=
to_hash()
Alias for: _attributes_
to_json(*options) click to toggle source

@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
valid?() click to toggle source

@return [Buff::Boolean]

# File lib/varia_model.rb, line 185
def valid?
  validate.empty?
end
validate() click to toggle source

@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

add_error(attribute, message) click to toggle source

@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

carefree_assign(new_attrs = {}) click to toggle source
# File lib/varia_model.rb, line 291
def carefree_assign(new_attrs = {})
  _attributes_.deep_merge!(new_attrs)
end
eval_as_proc(obj) click to toggle source

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
whitelist_assign(new_attrs = {}) click to toggle source
# 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