module StoreModel::Model
When included into class configures it to handle JSON column
Attributes
Public Instance Methods
Compares two StoreModel::Model
instances
@param other [StoreModel::Model]
@return [Boolean]
# 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
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
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
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
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
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
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
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
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
# File lib/store_model/model.rb, line 126 def attribute?(attribute) case value = attributes[attribute] when 0 then false else value.present? end end