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

#serialize_enums_using_as_json=(value) ⇒ Object (writeonly)

Sets the attribute serialize_enums_using_as_json

Parameters:

  • value

    the value to set the attribute serialize_enums_using_as_json to.



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

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

Parameters:

  • value

    the value to set the attribute serialize_unknown_attributes to.



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

def serialize_unknown_attributes=(value)
  @serialize_unknown_attributes = value
end

Class Method Details

.included(base) ⇒ Object

rubocop:disable Metrics/ModuleLength



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

Parameters:

Returns:

  • (Boolean)


80
81
82
83
84
# File 'lib/store_model/model.rb', line 80

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

Parameters:

  • attr_name (String, Symbol)

Returns:

  • (Object)


92
93
94
# File 'lib/store_model/model.rb', line 92

def [](attr_name)
  @attributes.fetch_value(attr_name.to_s)
end

#[]=(attr_name, value) ⇒ Object

Setting attribute using brackets

Parameters:

  • name (String, Symbol)
  • value (Object)

Returns:

  • (Object)


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

def []=(attr_name, value)
  @attributes.write_from_user(attr_name.to_s, value)
end

#_has_attribute?(attr_name) ⇒ Boolean

Legacy implementation of #has_attribute?

Parameters:

  • attr_name (String)

    name of the attribute

Returns:

  • (Boolean)


170
171
172
# File 'lib/store_model/model.rb', line 170

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)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/store_model/model.rb', line 35

def as_json(options = {}) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  serialize_unknown_attributes = if options.key?(:serialize_unknown_attributes)
                                   options[:serialize_unknown_attributes]
                                 else
                                   StoreModel.config.serialize_unknown_attributes
                                 end

  serialize_enums_using_as_json = if options.key?(:serialize_enums_using_as_json)
                                    options[:serialize_enums_using_as_json]
                                  else
                                    StoreModel.config.serialize_enums_using_as_json
                                  end

  result = @attributes.keys.each_with_object({}) do |key, values|
    attr = @attributes.fetch(key)
    assign_serialization_options(attr, serialize_unknown_attributes, serialize_enums_using_as_json)
    values[key] = serialized_attribute(attr)
  end.with_indifferent_access

  result.merge!(unknown_attributes) if serialize_unknown_attributes
  result.as_json(options).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.

Returns:

  • (Boolean)


116
117
118
# File 'lib/store_model/model.rb', line 116

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.

Parameters:

  • attr_name (String, Symbol)

Returns:

  • Object



65
66
67
68
69
70
71
72
73
# File 'lib/store_model/model.rb', line 65

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
    message = attr_name.is_a?(Symbol) ? "key not found: :#{attr_name}" : "key not found: #{attr_name}"
    raise KeyError, message
  end
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)


159
160
161
162
163
# File 'lib/store_model/model.rb', line 159

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)


109
110
111
# File 'lib/store_model/model.rb', line 109

def hash
  attributes.hash
end

#inspectString

String representation of the object.

Returns:

  • (String)


123
124
125
126
127
# File 'lib/store_model/model.rb', line 123

def inspect
  attribute_string = attributes.map { |name, value| "#{name}: #{value.inspect}" }
                               .join(", ")
  "#<#{self.class.name} #{attribute_string}>"
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.

Returns:

  • (Boolean)


208
209
210
211
212
213
214
# File 'lib/store_model/model.rb', line 208

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.

Returns:

  • (Boolean)


195
196
197
# File 'lib/store_model/model.rb', line 195

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

Parameters:

  • attr_name (String)

    name of the attribute

Returns:

  • (ActiveModel::Type::Value)


136
137
138
139
# File 'lib/store_model/model.rb', line 136

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)


180
181
182
# File 'lib/store_model/model.rb', line 180

def unknown_attributes
  @unknown_attributes ||= {}
end