Class: VirtusModel::Base
- Inherits:
-
Object
- Object
- VirtusModel::Base
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations, ActiveModel::Validations::Callbacks
- Defined in:
- lib/virtus_model/base.rb
Class Method Summary collapse
-
.association?(name, *types) ⇒ Boolean
Is there an association with the provided name and type (optional)?.
-
.associations(*types) ⇒ Object
Get an array of association names by type (optional).
-
.attribute?(name) ⇒ Boolean
Is there an attribute with the provided name?.
-
.attributes ⇒ Object
Get an array of attribute names.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two models are equal if their attributes are equal.
-
#as_json(options = nil) ⇒ Object
Alias of #export.
-
#assign_attributes(model) ⇒ Object
Recursively update attributes and return a self-reference.
-
#export(options = nil) ⇒ Object
Recursively convert all attributes to hash pairs.
-
#initialize(model = nil) ⇒ Base
constructor
Initialize attributes using the provided hash or object.
-
#to_h(options = nil) ⇒ Object
Alias of #to_hash.
-
#to_hash(options = nil) ⇒ Object
Alias of #export.
-
#to_json(options = nil) ⇒ Object
Convert the #as_json result to JSON.
-
#update(model = nil, options = {}) ⇒ Object
Update attributes and validate.
Constructor Details
#initialize(model = nil) ⇒ Base
Initialize attributes using the provided hash or object.
44 45 46 |
# File 'lib/virtus_model/base.rb', line 44 def initialize(model = nil) super(compact_hash(extract_attributes(model))) end |
Class Method Details
.association?(name, *types) ⇒ Boolean
Is there an association with the provided name and type (optional)?
25 26 27 |
# File 'lib/virtus_model/base.rb', line 25 def self.association?(name, *types) associations(*types).include?(name) end |
.associations(*types) ⇒ Object
Get an array of association names by type (optional).
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/virtus_model/base.rb', line 30 def self.associations(*types) classes = { one: Virtus::Attribute::EmbeddedValue, many: Virtus::Attribute::Collection }.select do |type, _| types.empty? || types.include?(type) end attribute_set.select do |field| classes.any? { |_, cls| field.class <= cls } end.map(&:name) end |
.attribute?(name) ⇒ Boolean
Is there an attribute with the provided name?
20 21 22 |
# File 'lib/virtus_model/base.rb', line 20 def self.attribute?(name) attributes.include?(name) end |
.attributes ⇒ Object
Get an array of attribute names.
15 16 17 |
# File 'lib/virtus_model/base.rb', line 15 def self.attributes attribute_set.map(&:name) end |
Instance Method Details
#==(other) ⇒ Object
Two models are equal if their attributes are equal.
61 62 63 64 65 66 67 |
# File 'lib/virtus_model/base.rb', line 61 def ==(other) if other.is_a?(VirtusModel::Base) self.attributes == other.attributes else self.attributes == other end end |
#as_json(options = nil) ⇒ Object
Alias of #export.
95 96 97 |
# File 'lib/virtus_model/base.rb', line 95 def as_json( = nil) export().deep_stringify_keys end |
#assign_attributes(model) ⇒ Object
Recursively update attributes and return a self-reference.
49 50 51 52 |
# File 'lib/virtus_model/base.rb', line 49 def assign_attributes(model) self.attributes = extract_attributes(model) self end |
#export(options = nil) ⇒ Object
Recursively convert all attributes to hash pairs.
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/virtus_model/base.rb', line 70 def export( = nil) self.class.attributes.reduce({}) do |result, name| value = attributes[name] if self.class.association?(name, :many) result[name] = export_values(value, ) elsif self.class.association?(name, :one) result[name] = export_value(value, ) else result[name] = value end result end end |
#to_h(options = nil) ⇒ Object
Alias of #to_hash.
90 91 92 |
# File 'lib/virtus_model/base.rb', line 90 def to_h( = nil) to_hash() end |
#to_hash(options = nil) ⇒ Object
Alias of #export.
85 86 87 |
# File 'lib/virtus_model/base.rb', line 85 def to_hash( = nil) export() end |
#to_json(options = nil) ⇒ Object
Convert the #as_json result to JSON.
100 101 102 |
# File 'lib/virtus_model/base.rb', line 100 def to_json( = nil) as_json().to_json end |
#update(model = nil, options = {}) ⇒ Object
Update attributes and validate.
55 56 57 58 |
# File 'lib/virtus_model/base.rb', line 55 def update(model = nil, = {}) assign_attributes(model) validate() end |