Module: StoreModel::Model
- Defined in:
- lib/store_model/model.rb
Overview
When included into class configures it to handle JSON column
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#serialize_empty_attributes ⇒ Object
writeonly
Sets the attribute serialize_empty_attributes.
-
#serialize_enums_using_as_json ⇒ Object
writeonly
Sets the attribute serialize_enums_using_as_json.
-
#serialize_unknown_attributes ⇒ Object
writeonly
Sets the attribute serialize_unknown_attributes.
Class Method Summary collapse
-
.included(base) ⇒ Object
rubocop:disable Metrics/MethodLength.
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.
-
#serialize_empty_attributes? ⇒ Boolean
Returns the value of the ‘@serialize_empty_attributes` instance variable.
-
#serialize_enums_using_as_json? ⇒ Boolean
Returns the value of the ‘@serialize_enums_using_as_json` instance variable.
-
#serialize_unknown_attributes? ⇒ Boolean
Returns the value of the ‘@serialize_unknown_attributes` instance variable.
-
#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.
52 53 54 |
# File 'lib/store_model/model.rb', line 52 def parent @parent end |
#serialize_empty_attributes=(value) ⇒ Object (writeonly)
Sets the attribute serialize_empty_attributes
53 54 55 |
# File 'lib/store_model/model.rb', line 53 def serialize_empty_attributes=(value) @serialize_empty_attributes = value end |
#serialize_enums_using_as_json=(value) ⇒ Object (writeonly)
Sets the attribute serialize_enums_using_as_json
53 54 55 |
# File 'lib/store_model/model.rb', line 53 def serialize_enums_using_as_json=(value) @serialize_enums_using_as_json = value end |
#serialize_unknown_attributes=(value) ⇒ Object (writeonly)
Sets the attribute serialize_unknown_attributes
53 54 55 |
# File 'lib/store_model/model.rb', line 53 def serialize_unknown_attributes=(value) @serialize_unknown_attributes = value end |
Class Method Details
.included(base) ⇒ Object
rubocop:disable Metrics/MethodLength
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/store_model/model.rb', line 12 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.include ActiveModel::Validations::Callbacks if ActiveModel::VERSION::MAJOR >= 8 && ActiveModel::VERSION::MINOR >= 1 base.include ActiveModel::Attributes::Normalization end base.extend StoreModel::Enum base.extend StoreModel::TypeBuilders base.attribute_method_suffix "?" base.extend(ClassMethods) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Compares two StoreModel::Model instances
118 119 120 121 122 |
# File 'lib/store_model/model.rb', line 118 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
130 131 132 |
# File 'lib/store_model/model.rb', line 130 def [](attr_name) @attributes.fetch_value(attr_name.to_s) end |
#[]=(attr_name, value) ⇒ Object
Setting attribute using brackets
140 141 142 |
# File 'lib/store_model/model.rb', line 140 def []=(attr_name, value) @attributes.write_from_user(attr_name.to_s, value) end |
#_has_attribute?(attr_name) ⇒ Boolean
Legacy implementation of #has_attribute?
208 209 210 |
# File 'lib/store_model/model.rb', line 208 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.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/store_model/model.rb', line 63 def as_json( = {}) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity serialize_unknown_attributes = if .key?(:serialize_unknown_attributes) [:serialize_unknown_attributes] else StoreModel.config.serialize_unknown_attributes end serialize_enums_using_as_json = if .key?(:serialize_enums_using_as_json) [:serialize_enums_using_as_json] else StoreModel.config.serialize_enums_using_as_json end serialize_empty_attributes = if .key?(:serialize_empty_attributes) [:serialize_empty_attributes] else StoreModel.config.serialize_empty_attributes end # If the model is nested, we need to ensure that the serialization result = @attributes.keys.each_with_object({}) do |key, values| attr = @attributes.fetch(key) (attr, serialize_unknown_attributes, serialize_enums_using_as_json, serialize_empty_attributes) serialized = serialized_attribute(attr) values[key] = serialized if serialize_empty_attributes || !serialized.nil? end.with_indifferent_access result.merge!(unknown_attributes) if serialize_unknown_attributes result.as_json().tap do |json| serialize_enums!(json) if serialize_enums_using_as_json end end |
#blank? ⇒ Boolean
Allows to call :presence validation on the association itself.
154 155 156 |
# File 'lib/store_model/model.rb', line 154 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.
103 104 105 106 107 108 109 110 111 |
# File 'lib/store_model/model.rb', line 103 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
197 198 199 200 201 |
# File 'lib/store_model/model.rb', line 197 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
147 148 149 |
# File 'lib/store_model/model.rb', line 147 def hash attributes.hash end |
#inspect ⇒ String
String representation of the object.
161 162 163 164 165 |
# File 'lib/store_model/model.rb', line 161 def inspect attribute_string = attributes.map { |name, value| "#{name}: #{value.inspect}" } .join(", ") "#<#{self.class.name} #{attribute_string}>" end |
#serialize_empty_attributes? ⇒ Boolean
Returns the value of the ‘@serialize_empty_attributes` instance variable. The default value is the value of the globally configured serialize_empty_attributes option.
This method is used to determine whether empty values should be serialized when the as_json method is called in nested StoreModel::Model objects.
263 264 265 266 267 268 269 |
# File 'lib/store_model/model.rb', line 263 def serialize_empty_attributes? if @serialize_empty_attributes.nil? StoreModel.config.serialize_empty_attributes || true else @serialize_empty_attributes end end |
#serialize_enums_using_as_json? ⇒ Boolean
Returns the value of the ‘@serialize_enums_using_as_json` instance variable. The default value is the value of the globally configured serialize_enums_using_as_json option.
This method is used to determine whether enums should be serialized when the as_json method is called in nested StoreModel::Model objects.
246 247 248 249 250 251 252 |
# File 'lib/store_model/model.rb', line 246 def serialize_enums_using_as_json? if @serialize_enums_using_as_json.nil? StoreModel.config.serialize_enums_using_as_json || false else @serialize_enums_using_as_json end end |
#serialize_unknown_attributes? ⇒ Boolean
Returns the value of the ‘@serialize_unknown_attributes` instance variable. In the current specification, unknown attributes must be persisted in the database regardless of the globally configured serialize_unknown_attributes option. Therefore, it returns the default value true if the instance variable is nil.
This method is used to ensure that the serialize_unknown_attributes option is correctly applied to nested StoreModel::Model objects when the as_json method is called.
233 234 235 |
# File 'lib/store_model/model.rb', line 233 def serialize_unknown_attributes? @serialize_unknown_attributes.nil? ? true : @serialize_unknown_attributes end |
#type_for_attribute(attr_name) ⇒ ActiveModel::Type::Value
Returns the type of the attribute with the given name
174 175 176 177 |
# File 'lib/store_model/model.rb', line 174 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
218 219 220 |
# File 'lib/store_model/model.rb', line 218 def unknown_attributes @unknown_attributes ||= {} end |