Class: Facturama::Models::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serializers::JSON, ActiveModel::Validations
Defined in:
lib/facturama/models/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Model

Returns a new instance of Model.



13
14
15
16
17
18
# File 'lib/facturama/models/model.rb', line 13

def initialize(values)
  values.each_pair do |k, v|
    send("#{k}=", v)
  end
  after_initialize
end

Instance Attribute Details

#all_errorsObject

Returns the value of attribute all_errors.



11
12
13
# File 'lib/facturama/models/model.rb', line 11

def all_errors
  @all_errors
end

Class Method Details

.define_reader(association) ⇒ Object



62
63
64
# File 'lib/facturama/models/model.rb', line 62

def define_reader(association)
  attr_reader association
end

.define_writer(association, class_name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/facturama/models/model.rb', line 47

def define_writer(association, class_name)
  class_eval <<-CODE, __FILE__, __LINE__ + 1
  def #{association}=(value)
    @#{association} =
    if value.class.name == "Array"
      value.collect do |val|
        #{class_name.to_s.camelize}.new(val)
      end
    else
      #{class_name.to_s.camelize}.new(value)
    end
  end
  CODE
end

.has_many_objects(association, class_name) ⇒ Object



37
38
39
40
# File 'lib/facturama/models/model.rb', line 37

def has_many_objects(association, class_name)
  define_writer(association, class_name)
  define_reader(association)
end

.has_one_object(association) ⇒ Object



42
43
44
45
# File 'lib/facturama/models/model.rb', line 42

def has_one_object(association)
  define_writer(association, association)
  define_reader(association)
end

Instance Method Details

#after_initializeObject



20
# File 'lib/facturama/models/model.rb', line 20

def after_initialize; end

#attributesObject



22
23
24
# File 'lib/facturama/models/model.rb', line 22

def attributes
  instance_values
end

#get_instance_valuesObject



30
31
32
33
34
# File 'lib/facturama/models/model.rb', line 30

def get_instance_values
  instance_values.delete_if do |k, _v|
    %w[all_errors errors validation_context].include?(k)
  end
end

#prepare_dataObject



26
27
28
# File 'lib/facturama/models/model.rb', line 26

def prepare_data
  prepare_keys.to_json
end