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



65
66
67
# File 'lib/facturama/models/model.rb', line 65

def define_reader(association)
  attr_reader association
end

.define_writer(association, class_name) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/facturama/models/model.rb', line 50

def define_writer(association, class_name)
  class_eval <<-CODE
  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



40
41
42
43
# File 'lib/facturama/models/model.rb', line 40

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

.has_one_object(association) ⇒ Object



45
46
47
48
# File 'lib/facturama/models/model.rb', line 45

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

Instance Method Details

#after_initializeObject



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

def after_initialize
end

#attributesObject



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

def attributes
  instance_values
end

#get_instance_valuesObject



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

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

#prepare_dataObject



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

def prepare_data
  prepare_keys.to_json
end