Class: Remitmd::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/remitmd/models.rb

Overview

Immutable model base. Builds from a hash with string or symbol keys.

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Model

Returns a new instance of Model.



78
79
80
81
82
83
# File 'lib/remitmd/models.rb', line 78

def initialize(attrs)
  attrs.each do |k, v|
    instance_variable_set("@#{k}", v)
    self.class.attr_reader(k.to_sym) unless respond_to?(k.to_sym)
  end
end

Instance Method Details

#inspectObject



91
92
93
# File 'lib/remitmd/models.rb', line 91

def inspect
  "#<#{self.class.name} #{to_h}>"
end

#to_hObject



85
86
87
88
89
# File 'lib/remitmd/models.rb', line 85

def to_h
  instance_variables.each_with_object({}) do |var, h|
    h[var.to_s.delete("@").to_sym] = instance_variable_get(var)
  end
end