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


20
21
22
# File 'lib/store_model/model.rb', line 20

def parent
  @parent
end

Class Method Details

.included(base) ⇒ Object

:nodoc:


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

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

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

Instance Method Details

#==(other) ⇒ Boolean

Compares two StoreModel::Model instances


37
38
39
40
41
# File 'lib/store_model/model.rb', line 37

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

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

#as_json(options = {}) ⇒ Hash

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


28
29
30
# File 'lib/store_model/model.rb', line 28

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

#blank?Boolean

Allows to call :presence validation on the association itself.


46
47
48
# File 'lib/store_model/model.rb', line 46

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


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

def has_attribute?(attr_name)
  attribute_types.key?(attr_name.to_s)
end

#inspectString

String representation of the object.


53
54
55
56
57
# File 'lib/store_model/model.rb', line 53

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


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

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


86
87
88
# File 'lib/store_model/model.rb', line 86

def unknown_attributes
  @unknown_attributes ||= {}
end