Method: StoreModel::Model#as_json

Defined in:
lib/store_model/model.rb

#as_json(options = {}) ⇒ Hash

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

Parameters:

  • (defaults to: {})

Returns:



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

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|
    values[key] = serialized_attribute(key)
  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