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
-
#parent ⇒ Object
Returns the value of attribute parent.
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares two StoreModel::Model instances.
-
#_has_attribute?(attr_name) ⇒ Boolean
Legacy implementation of #has_attribute?.
-
#as_json(options = {}) ⇒ Hash
Returns a hash representing the model.
-
#blank? ⇒ Boolean
Allows to call :presence validation on the association itself.
-
#has_attribute?(attr_name) ⇒ Boolean
Checks if the attribute with a given name is defined.
-
#hash ⇒ Integer
Returns hash for a StoreModel::Model instance based on attributes hash.
-
#inspect ⇒ String
String representation of the object.
-
#type_for_attribute(attr_name) ⇒ ActiveModel::Type::Value
Returns the type of the attribute with the given name.
-
#unknown_attributes ⇒ Hash
Contains a hash of attributes which are not defined but exist in the underlying JSON data.
Instance Attribute Details
#parent ⇒ Object
Returns the value of attribute parent.
23 24 25 |
# File 'lib/store_model/model.rb', line 23 def parent @parent end |
Class Method Details
.included(base) ⇒ Object
:nodoc:
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/store_model/model.rb', line 11 def self.included(base) # :nodoc: base.include ActiveModel::Model base.include ActiveModel::Attributes 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
40 41 42 43 44 |
# 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) ⇒ Boolean
Legacy implementation of #has_attribute?
110 111 112 |
# File 'lib/store_model/model.rb', line 110 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.
31 32 33 |
# File 'lib/store_model/model.rb', line 31 def as_json( = {}) attributes.with_indifferent_access.as_json() end |
#blank? ⇒ Boolean
Allows to call :presence validation on the association itself.
56 57 58 |
# File 'lib/store_model/model.rb', line 56 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
99 100 101 102 103 |
# 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 ⇒ Integer
Returns hash for a StoreModel::Model instance based on attributes hash
49 50 51 |
# File 'lib/store_model/model.rb', line 49 def hash attributes.hash end |
#inspect ⇒ String
String representation of the object.
63 64 65 66 67 |
# 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) ⇒ ActiveModel::Type::Value
Returns the type of the attribute with the given name
76 77 78 79 |
# 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 ⇒ Hash
Contains a hash of attributes which are not defined but exist in the underlying JSON data
120 121 122 |
# File 'lib/store_model/model.rb', line 120 def unknown_attributes @unknown_attributes ||= {} end |