class VirtusModel::Base
Public Class Methods
Is there an association with the provided name and type (optional)?
# File lib/virtus_model/base.rb, line 25 def self.association?(name, *types) associations(*types).include?(name) end
Get an array of association names by type (optional).
# 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
Is there an attribute with the provided name?
# File lib/virtus_model/base.rb, line 20 def self.attribute?(name) attributes.include?(name) end
Get an array of attribute names.
# File lib/virtus_model/base.rb, line 15 def self.attributes attribute_set.map(&:name) end
Initialize attributes using the provided hash or object.
# File lib/virtus_model/base.rb, line 44 def initialize(model = nil) super(compact_hash(extract_attributes(model))) end
Public Instance Methods
Two models are equal if their attributes are equal.
# 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
Alias of export
.
# File lib/virtus_model/base.rb, line 95 def as_json(options = nil) export(options).deep_stringify_keys end
Recursively update attributes and return a self-reference.
# File lib/virtus_model/base.rb, line 49 def assign_attributes(model) self.attributes = extract_attributes(model) self end
Recursively convert all attributes to hash pairs.
# File lib/virtus_model/base.rb, line 70 def export(options = nil) self.class.attributes.reduce({}) do |result, name| value = attributes[name] if self.class.association?(name, :many) result[name] = export_values(value, options) elsif self.class.association?(name, :one) result[name] = export_value(value, options) else result[name] = value end result end end
Alias of to_hash
.
# File lib/virtus_model/base.rb, line 90 def to_h(options = nil) to_hash(options) end
Alias of export
.
# File lib/virtus_model/base.rb, line 85 def to_hash(options = nil) export(options) end
Convert the as_json
result to JSON.
# File lib/virtus_model/base.rb, line 100 def to_json(options = nil) as_json(options).to_json end
Update attributes and validate.
# File lib/virtus_model/base.rb, line 55 def update(model = nil, options = {}) assign_attributes(model) validate(options) end
Protected Instance Methods
Omit keys with nil values.
# File lib/virtus_model/base.rb, line 163 def compact_hash(hash) hash.select { |_, value| !value.nil? } end
Export the value with the provided options.
# File lib/virtus_model/base.rb, line 157 def export_value(value, options = nil) return if value.nil? value.respond_to?(:export) ? value.export(options) : value end
Export each value with the provided options.
# File lib/virtus_model/base.rb, line 151 def export_values(values, options = nil) return if values.nil? values.map { |v| export_value(v, options) } end
Extract model attributes into a hash.
# File lib/virtus_model/base.rb, line 107 def extract_attributes(model) self.class.attributes.reduce({}) do |result, name| if model.respond_to?(name) result[name] = model.public_send(name) elsif model.respond_to?(:[]) && model.respond_to?(:key?) && model.key?(name) result[name] = model[name] end result end end
Merge associated errors using the current validation context.
# File lib/virtus_model/base.rb, line 142 def import_errors(name, model) return unless model.respond_to?(:validate) return if model.validate(validation_context) model.errors.each do |field, error| errors.add("#{name}[#{field}]", error) end end
Validate all associations by type and import resulting errors.
# File lib/virtus_model/base.rb, line 119 def validate_associations validate_associations_one validate_associations_many end
Validate “many” associations and import errors.
# File lib/virtus_model/base.rb, line 132 def validate_associations_many self.class.associations(:many).each do |name| values = attributes[name] || [] values.each.with_index do |value, index| import_errors("#{name}[#{index}]", value) end end end
Validate “one” associations and import errors.
# File lib/virtus_model/base.rb, line 125 def validate_associations_one self.class.associations(:one).each do |name| import_errors(name, attributes[name]) end end