module StoreModel::Model

When included into class configures it to handle JSON column

Attributes

parent[RW]

Public Instance Methods

==(other) click to toggle source

Compares two StoreModel::Model instances

@param other [StoreModel::Model]

@return [Boolean]

Calls superclass method
# File lib/store_model/model.rb, line 40
def ==(other)
  return super unless other.is_a?(self.class)

  attributes.all? { |name, value| value == other.attributes[name] }
end
_has_attribute?(attr_name) click to toggle source

Legacy implementation of has_attribute?

@param attr_name [String] name of the attribute

@return [Boolean]

# File lib/store_model/model.rb, line 110
def _has_attribute?(attr_name)
  attribute_types.key?(attr_name)
end
as_json(options = {}) click to toggle source

Returns a hash representing the model. Some configuration can be passed through options.

@param options [Hash]

@return [Hash]

# File lib/store_model/model.rb, line 31
def as_json(options = {})
  attributes.with_indifferent_access.as_json(options)
end
blank?() click to toggle source

Allows to call :presence validation on the association itself.

@return [Boolean]

# File lib/store_model/model.rb, line 56
def blank?
  attributes.values.all?(&:blank?)
end
has_attribute?(attr_name) click to toggle source

Checks if the attribute with a given name is defined

@example

class Person
  include StoreModel::Model
  attribute :name, :string
  alias_attribute :new_name, :name
end

Person.has_attribute?('name')     # => true
Person.has_attribute?('new_name') # => true
Person.has_attribute?(:age)       # => true
Person.has_attribute?(:nothing)   # => false

@param attr_name [String] name of the attribute

@return [Boolean] rubocop:disable Naming/PredicateName

# File lib/store_model/model.rb, line 99
def has_attribute?(attr_name)
  attr_name = attr_name.to_s
  attr_name = self.class.attribute_aliases[attr_name] || attr_name
  attribute_types.key?(attr_name)
end
hash() click to toggle source

Returns hash for a StoreModel::Model instance based on attributes hash

@return [Integer]

# File lib/store_model/model.rb, line 49
def hash
  attributes.hash
end
inspect() click to toggle source

String representation of the object.

@return [String]

# File lib/store_model/model.rb, line 63
def inspect
  attribute_string = attributes.map { |name, value| "#{name}: #{value.nil? ? 'nil' : value}" }
                               .join(", ")
  "#<#{self.class.name} #{attribute_string}>"
end
type_for_attribute(attr_name) click to toggle source

Returns the type of the attribute with the given name

@param attr_name [String] name of the attribute

@return [ActiveModel::Type::Value]

# File lib/store_model/model.rb, line 76
def type_for_attribute(attr_name)
  attr_name = attr_name.to_s
  attribute_types[attr_name]
end
unknown_attributes() click to toggle source

Contains a hash of attributes which are not defined but exist in the underlying JSON data

@return [Hash]

# File lib/store_model/model.rb, line 120
def unknown_attributes
  @unknown_attributes ||= {}
end

Private Instance Methods

attribute?(attribute) click to toggle source
# File lib/store_model/model.rb, line 126
def attribute?(attribute)
  case value = attributes[attribute]
  when 0 then false
  else value.present?
  end
end