Class: VirtusModel::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Conversion, ActiveModel::Validations
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.



43
44
45
# File 'lib/virtus_model/base.rb', line 43

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)


24
25
26
# File 'lib/virtus_model/base.rb', line 24

def self.association?(name, *types)
  associations(*types).include?(name)
end

.associations(*types) ⇒ Object

Get an array of association names by type (optional).



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/virtus_model/base.rb', line 29

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)


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

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

.attributesObject

Get an array of attribute names.



14
15
16
# File 'lib/virtus_model/base.rb', line 14

def self.attributes
  attribute_set.map(&:name)
end

Instance Method Details

#==(other) ⇒ Object

Two models are equal if their attributes are equal.



60
61
62
63
64
65
66
# File 'lib/virtus_model/base.rb', line 60

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.



94
95
96
# File 'lib/virtus_model/base.rb', line 94

def as_json(options = nil)
  export(options).deep_stringify_keys
end

#assign_attributes(model) ⇒ Object

Recursively update attributes and return a self-reference.



48
49
50
51
# File 'lib/virtus_model/base.rb', line 48

def assign_attributes(model)
  self.attributes = extract_attributes(model)
  self
end

#export(options = nil) ⇒ Object

Recursively convert all attributes to hash pairs.



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/virtus_model/base.rb', line 69

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.



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

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

#to_hash(options = nil) ⇒ Object

Alias of #export.



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

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

#to_json(options = nil) ⇒ Object

Convert the #as_json result to JSON.



99
100
101
# File 'lib/virtus_model/base.rb', line 99

def to_json(options = nil)
  as_json(options).to_json
end

#update(model = nil, options = {}) ⇒ Object

Update attributes and validate.



54
55
56
57
# File 'lib/virtus_model/base.rb', line 54

def update(model = nil, options = {})
  assign_attributes(model)
  validate(options)
end