Class: KalibroGem::Entities::Model

Inherits:
Object
  • Object
show all
Extended by:
RequestMethods::ClassMethods
Includes:
HashConverters, RequestMethods
Defined in:
lib/kalibro_gem/entities/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RequestMethods::ClassMethods

exists_action, find_action, id_params

Methods included from HashConverters

#convert_to_hash, #date_with_milliseconds, #field_to_hash

Methods included from XMLConverters

#get_xml, #xml_instance_class_name

Methods included from RequestMethods

#destroy_action, #destroy_params, #save_action, #save_params

Constructor Details

#initialize(attributes = {}) ⇒ Model

Returns a new instance of Model.



26
27
28
29
# File 'lib/kalibro_gem/entities/model.rb', line 26

def initialize(attributes={})
  attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) }
  @kalibro_errors = []
end

Instance Attribute Details

#kalibro_errorsObject

Returns the value of attribute kalibro_errors.



24
25
26
# File 'lib/kalibro_gem/entities/model.rb', line 24

def kalibro_errors
  @kalibro_errors
end

Class Method Details

.create(attributes = {}) ⇒ Object



68
69
70
71
72
# File 'lib/kalibro_gem/entities/model.rb', line 68

def self.create(attributes={})
  new_model = new attributes
  new_model.save
  new_model
end

.create_array_from_hash(response) ⇒ Object



111
112
113
114
115
# File 'lib/kalibro_gem/entities/model.rb', line 111

def self.create_array_from_hash (response)
  response = [] if response.nil?
  response = [response] if response.is_a?(Hash)
  response
end

.create_objects_array_from_hash(response) ⇒ Object



107
108
109
# File 'lib/kalibro_gem/entities/model.rb', line 107

def self.create_objects_array_from_hash (response)
  create_array_from_hash(response).map { |hash| new hash }
end

.exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/kalibro_gem/entities/model.rb', line 87

def self.exists?(id)
  request(exists_action, id_params(id))[:exists]
end

.find(id) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/kalibro_gem/entities/model.rb', line 91

def self.find(id)
  if(exists?(id))
    new request(find_action, id_params(id))["#{class_name.underscore}".to_sym]
  else
    raise KalibroGem::Errors::RecordNotFound
  end
end

.request(action, request_body = nil) ⇒ Object



40
41
42
43
# File 'lib/kalibro_gem/entities/model.rb', line 40

def self.request(action, request_body = nil)
  response = client(endpoint).call(action, message: request_body )
  response.to_hash["#{action}_response".to_sym] # response is a Savon::SOAP::Response, and to_hash is a Savon::SOAP::Response method
end

.to_object(value) ⇒ Object



45
46
47
# File 'lib/kalibro_gem/entities/model.rb', line 45

def self.to_object value
  value.kind_of?(Hash) ? new(value) : value
end

.to_objects_array(value) ⇒ Object



49
50
51
52
# File 'lib/kalibro_gem/entities/model.rb', line 49

def self.to_objects_array value
  array = value.kind_of?(Array) ? value : [value]
  array.each.map { |element| to_object(element) }
end

Instance Method Details

#==(another) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/kalibro_gem/entities/model.rb', line 74

def ==(another)
  unless self.class == another.class then
    return false
  end
  self.variable_names.each {
    |name|
    unless self.send("#{name}") == another.send("#{name}") then
      return false
    end
  }
  true
end

#destroyObject



99
100
101
102
103
104
105
# File 'lib/kalibro_gem/entities/model.rb', line 99

def destroy
  begin
    self.class.request(destroy_action, destroy_params)
  rescue Exception => exception
    add_error exception
  end
end

#errors=(errors) ⇒ Object



117
118
119
# File 'lib/kalibro_gem/entities/model.rb', line 117

def errors=(errors)
  self.kalibro_errors = errors
end

#saveObject



54
55
56
57
58
59
60
61
62
# File 'lib/kalibro_gem/entities/model.rb', line 54

def save
  begin
    self.id = self.class.request(save_action, save_params)["#{instance_class_name.underscore}_id".to_sym]
    true
  rescue Exception => exception
    add_error exception
    false
  end
end

#save!Object



64
65
66
# File 'lib/kalibro_gem/entities/model.rb', line 64

def save!
  save
end

#to_hash(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/kalibro_gem/entities/model.rb', line 31

def to_hash(options={})
  hash = Hash.new
  excepts = options[:except].nil? ? [] : options[:except]
  fields.each do |field|
    hash = field_to_hash(field).merge(hash) if !excepts.include?(field)
  end
  hash
end