Class: VirtusModel::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Conversion, ActiveModel::Validations, ActiveModel::Validations::Callbacks
Defined in:
lib/virtus_model/base.rb

Class Method Summary collapse

Instance Method Summary collapse

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)?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


20
21
22
# File 'lib/virtus_model/base.rb', line 20

def self.attribute?(name)
  attributes.include?(name)
end

.attributesObject

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(options = nil)
  export(options).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(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) ⇒ Object

Alias of #to_hash.



90
91
92
# File 'lib/virtus_model/base.rb', line 90

def to_h(options = nil)
  to_hash(options)
end

#to_hash(options = nil) ⇒ Object

Alias of #export.



85
86
87
# File 'lib/virtus_model/base.rb', line 85

def to_hash(options = nil)
  export(options)
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(options = nil)
  as_json(options).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, options = {})
  assign_attributes(model)
  validate(options)
end