Class: AR2DTO::DTO

Inherits:
Object
  • Object
show all
Defined in:
lib/ar2dto/dto.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ DTO

Returns a new instance of DTO.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ar2dto/dto.rb', line 12

def initialize(data = {})
  attribute_names = self.class.original_model.attribute_names

  data.each do |key, value|
    if attribute_names.include?(key)
      instance_variable_set("@#{key}", value)
      singleton_class.attr_reader(key)
    else
      define_singleton_method(key) { value }
    end
  end

  super()
end

Class Method Details

.[](original_model) ⇒ Object



5
6
7
8
9
10
# File 'lib/ar2dto/dto.rb', line 5

def self.[](original_model)
  Class.new(self) do
    include ::AR2DTO::ActiveModel if original_model.ar2dto.active_model_compliance
    define_singleton_method(:original_model) { original_model }
  end
end

Instance Method Details

#==(other) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/ar2dto/dto.rb', line 27

def ==(other)
  if other.instance_of?(self.class)
    as_json == other.as_json
  else
    super
  end
end