class VirtusModel::Base

Public Class Methods

association?(name, *types) click to toggle source

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
associations(*types) click to toggle source

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
attribute?(name) click to toggle source

Is there an attribute with the provided name?

# File lib/virtus_model/base.rb, line 20
def self.attribute?(name)
  attributes.include?(name)
end
attributes() click to toggle source

Get an array of attribute names.

# File lib/virtus_model/base.rb, line 15
def self.attributes
  attribute_set.map(&:name)
end
new(model = nil) click to toggle source

Initialize attributes using the provided hash or object.

Calls superclass method
# File lib/virtus_model/base.rb, line 44
def initialize(model = nil)
  super(compact_hash(extract_attributes(model)))
end

Public Instance Methods

==(other) click to toggle source

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
as_json(options = nil) click to toggle source

Alias of export.

# File lib/virtus_model/base.rb, line 95
def as_json(options = nil)
  export(options).deep_stringify_keys
end
assign_attributes(model) click to toggle source

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
export(options = nil) click to toggle source

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
to_h(options = nil) click to toggle source

Alias of to_hash.

# File lib/virtus_model/base.rb, line 90
def to_h(options = nil)
  to_hash(options)
end
to_hash(options = nil) click to toggle source

Alias of export.

# File lib/virtus_model/base.rb, line 85
def to_hash(options = nil)
  export(options)
end
to_json(options = nil) click to toggle source

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(model = nil, options = {}) click to toggle source

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

compact_hash(hash) click to toggle source

Omit keys with nil values.

# File lib/virtus_model/base.rb, line 163
def compact_hash(hash)
  hash.select { |_, value| !value.nil? }
end
export_value(value, options = nil) click to toggle source

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_values(values, options = nil) click to toggle source

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_attributes(model) click to toggle source

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
import_errors(name, model) click to toggle source

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_associations() click to toggle source

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_associations_many() click to toggle source

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_associations_one() click to toggle source

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