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
(also: #eql?)
Compares two StoreModel::Model instances.
-
#[](attr_name) ⇒ Object
Accessing attribute using brackets.
-
#[]=(attr_name, value) ⇒ Object
Setting attribute using brackets.
-
#_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.
-
#fetch(attr_name) ⇒ Object
Returns an Object, similar to Hash#fetch, raises a KeyError if attr_name doesn’t exist.
-
#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.
-
#values_for_database ⇒ Hash
Serialized values for storing in db.
Instance Attribute Details
#parent ⇒ Object
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 Also known as: eql?
Compares two StoreModel::Model instances
66 67 68 69 70 |
# File 'lib/store_model/model.rb', line 66 def ==(other) return super unless other.is_a?(self.class) attributes.all? { |name, value| value == other.attributes[name] } end |
#[](attr_name) ⇒ Object
Accessing attribute using brackets
78 79 80 |
# File 'lib/store_model/model.rb', line 78 def [](attr_name) @attributes.fetch_value(attr_name.to_s) end |
#[]=(attr_name, value) ⇒ Object
Setting attribute using brackets
88 89 90 |
# File 'lib/store_model/model.rb', line 88 def []=(attr_name, value) @attributes.write_from_user(attr_name.to_s, value) end |
#_has_attribute?(attr_name) ⇒ Boolean
Legacy implementation of #has_attribute?
156 157 158 |
# File 'lib/store_model/model.rb', line 156 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
.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/store_model/model.rb', line 34 def as_json( = {}) serialize_unknown_attributes = if .key?(:serialize_unknown_attributes) [:serialize_unknown_attributes] else StoreModel.config.serialize_unknown_attributes end result = attributes.with_indifferent_access result.merge!(unknown_attributes) if serialize_unknown_attributes result.as_json() end |
#blank? ⇒ Boolean
Allows to call :presence validation on the association itself.
102 103 104 |
# File 'lib/store_model/model.rb', line 102 def blank? attributes.values.all?(&:blank?) end |
#fetch(attr_name) ⇒ Object
Returns an Object, similar to Hash#fetch, raises a KeyError if attr_name doesn’t exist.
51 52 53 54 55 56 57 58 59 |
# File 'lib/store_model/model.rb', line 51 def fetch(attr_name) stringified_key = attr_name.to_s if attribute_names.include?(stringified_key) || attribute_aliases.key?(stringified_key) public_send(stringified_key) else = attr_name.is_a?(Symbol) ? "key not found: :#{attr_name}" : "key not found: #{attr_name}" raise KeyError, end end |
#has_attribute?(attr_name) ⇒ Boolean
Checks if the attribute with a given name is defined
rubocop:disable Naming/PredicateName
145 146 147 148 149 |
# File 'lib/store_model/model.rb', line 145 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
95 96 97 |
# File 'lib/store_model/model.rb', line 95 def hash attributes.hash end |
#inspect ⇒ String
String representation of the object.
109 110 111 112 113 |
# File 'lib/store_model/model.rb', line 109 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
122 123 124 125 |
# File 'lib/store_model/model.rb', line 122 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
166 167 168 |
# File 'lib/store_model/model.rb', line 166 def unknown_attributes @unknown_attributes ||= {} end |
#values_for_database ⇒ Hash
Serialized values for storing in db
173 174 175 176 177 178 179 |
# File 'lib/store_model/model.rb', line 173 def values_for_database result = @attributes.keys.each_with_object({}) do |key, values| values[key] = @attributes.fetch(key).value_for_database end result.merge!(unknown_attributes) result end |