Module: StoreModel::Model

Defined in:
lib/store_model/model.rb

Overview

When included into class configures it to handle JSON column

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



24
25
26
# File 'lib/store_model/model.rb', line 24

def parent
  @parent
end

Class Method Details

.included(base) ⇒ Object

:nodoc:



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/store_model/model.rb', line 11

def self.included(base) # :nodoc:
  base.include ActiveModel::Model
  base.include ActiveModel::Attributes
  base.include ActiveRecord::AttributeMethods::BeforeTypeCast
  base.include ActiveModel::AttributeMethods
  base.include StoreModel::NestedAttributes

  base.extend StoreModel::Enum
  base.extend StoreModel::TypeBuilders

  base.attribute_method_suffix "?"
end

Instance Method Details

#==(other) ⇒ Boolean

Compares two StoreModel::Model instances

Parameters:

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/store_model/model.rb', line 41

def ==(other)
  return super unless other.is_a?(self.class)

  attributes.all? { |name, value| value == other.attributes[name] }
end

#_has_attribute?(attr_name) ⇒ Boolean

Legacy implementation of #has_attribute?

Parameters:

  • attr_name (String)

    name of the attribute

Returns:

  • (Boolean)


111
112
113
# File 'lib/store_model/model.rb', line 111

def _has_attribute?(attr_name)
  attribute_types.key?(attr_name)
end

#as_json(options = {}) ⇒ Hash

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

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (Hash)


32
33
34
# File 'lib/store_model/model.rb', line 32

def as_json(options = {})
  attributes.with_indifferent_access.merge(unknown_attributes).as_json(options)
end

#blank?Boolean

Allows to call :presence validation on the association itself.

Returns:

  • (Boolean)


57
58
59
# File 'lib/store_model/model.rb', line 57

def blank?
  attributes.values.all?(&:blank?)
end

#has_attribute?(attr_name) ⇒ Boolean

Checks if the attribute with a given name is defined

rubocop:disable Naming/PredicateName

Examples:

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

Parameters:

  • attr_name (String)

    name of the attribute

Returns:

  • (Boolean)


100
101
102
103
104
# File 'lib/store_model/model.rb', line 100

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

#hashInteger

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

Returns:

  • (Integer)


50
51
52
# File 'lib/store_model/model.rb', line 50

def hash
  attributes.hash
end

#inspectString

String representation of the object.

Returns:

  • (String)


64
65
66
67
68
# File 'lib/store_model/model.rb', line 64

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) ⇒ ActiveModel::Type::Value

Returns the type of the attribute with the given name

Parameters:

  • attr_name (String)

    name of the attribute

Returns:

  • (ActiveModel::Type::Value)


77
78
79
80
# File 'lib/store_model/model.rb', line 77

def type_for_attribute(attr_name)
  attr_name = attr_name.to_s
  attribute_types[attr_name]
end

#unknown_attributesHash

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

Returns:

  • (Hash)


121
122
123
# File 'lib/store_model/model.rb', line 121

def unknown_attributes
  @unknown_attributes ||= {}
end