/
model.rb
87 lines (75 loc) · 2.25 KB
/
model.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
# frozen_string_literal: true
require "store_model/types"
require "store_model/enum"
require "store_model/type_builders"
require "store_model/nested_attributes"
module StoreModel
# When included into class configures it to handle JSON column
module Model
def self.included(base) # :nodoc:
base.include ActiveModel::Model
base.include ActiveModel::Attributes
base.include StoreModel::NestedAttributes
base.extend StoreModel::Enum
base.extend StoreModel::TypeBuilders
end
# Returns a hash representing the model. Some configuration can be
# passed through +options+.
#
# @param options [Hash]
#
# @return [Hash]
def as_json(options = {})
attributes.with_indifferent_access.as_json(options)
end
# Compares two StoreModel::Model instances
#
# @param other [StoreModel::Model]
#
# @return [Boolean]
def ==(other)
return super unless other.is_a?(self.class)
attributes.all? { |name, value| value == other.send(name) }
end
# Allows to call :presence validation on the association itself.
#
# @return [Boolean]
def blank?
attributes.values.all?(&:blank?)
end
# String representation of the object.
#
# @return [String]
def inspect
attribute_string = attributes.map { |name, value| "#{name}: #{value || 'nil'}" }.join(", ")
"#<#{self.class.name} #{attribute_string}>"
end
delegate :attribute_types, to: :class
# Returns the type of the attribute with the given name
#
# @param attr_name [String] name of the attribute
#
# @return [ActiveModel::Type::Value]
def type_for_attribute(attr_name)
attr_name = attr_name.to_s
attribute_types[attr_name]
end
# Checks if the attribute with a given name is defined
#
# @param attr_name [String] name of the attribute
#
# @return [Boolean]
# rubocop:disable Naming/PredicateName
def has_attribute?(attr_name)
attribute_types.key?(attr_name.to_s)
end
# rubocop:enable Naming/PredicateName
# Contains a hash of attributes which are not defined but exist in the
# underlying JSON data
#
# @return [Hash]
def unknown_attributes
@unknown_attributes ||= {}
end
end
end