Class: Likeno::Entity

Inherits:
Object
  • Object
show all
Extended by:
CRUDRequestParameters::ClassMethods, RequestMethods
Includes:
CRUDRequestParameters, HashConverters
Defined in:
lib/likeno/entity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RequestMethods

address, client, endpoint, entity_name, module_name, request

Methods included from CRUDRequestParameters::ClassMethods

all_action, all_params, default_headers, exists_action, find_action, find_prefix, id_params

Methods included from HashConverters

#convert_to_hash, #date_time_to_s, #field_to_hash

Methods included from CRUDRequestParameters

#default_headers, #destroy_action, #destroy_params, #destroy_prefix, #save_action, #save_params, #save_prefix, #update_params, #update_prefix

Constructor Details

#initialize(attributes = {}, persisted = false) ⇒ Entity

Returns a new instance of Entity.



26
27
28
29
30
# File 'lib/likeno/entity.rb', line 26

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

Instance Attribute Details

#likeno_errorsObject

Returns the value of attribute likeno_errors.



24
25
26
# File 'lib/likeno/entity.rb', line 24

def likeno_errors
  @likeno_errors
end

#persistedObject Also known as: persisted?

Returns the value of attribute persisted.



24
25
26
# File 'lib/likeno/entity.rb', line 24

def persisted
  @persisted
end

Class Method Details

.allObject



103
104
105
# File 'lib/likeno/entity.rb', line 103

def self.all
  create_objects_array_from_hash request(all_action, all_params, :get, all_prefix, all_headers)
end

.create(attributes = {}) ⇒ Object



72
73
74
75
76
# File 'lib/likeno/entity.rb', line 72

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

.create_array_from_hash(response) ⇒ Object



118
119
120
121
122
# File 'lib/likeno/entity.rb', line 118

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



114
115
116
# File 'lib/likeno/entity.rb', line 114

def self.create_objects_array_from_hash (response)
  create_array_from_hash(response[entity_name.pluralize]).map { |hash| new(hash, true) }
end

.exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/likeno/entity.rb', line 94

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

.find(id) ⇒ Object



98
99
100
101
# File 'lib/likeno/entity.rb', line 98

def self.find(id)
  response = request(find_action, id_params(id), :get, find_prefix, find_headers)
  new(response[entity_name], true)
end

.to_object(value) ⇒ Object



43
44
45
# File 'lib/likeno/entity.rb', line 43

def self.to_object(value)
  value.is_a?(Hash) ? new(value, true) : value
end

.to_objects_array(value) ⇒ Object



47
48
49
50
# File 'lib/likeno/entity.rb', line 47

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

Instance Method Details

#==(other) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/likeno/entity.rb', line 85

def ==(other)
  return false unless self.class == other.class
  variable_names.each do |name|
    next if %w(created_at updated_at persisted).include? name
    return false unless send("#{name}") == other.send("#{name}")
  end
  true
end

#destroyObject



107
108
109
110
111
112
# File 'lib/likeno/entity.rb', line 107

def destroy
  without_request_error? do
    response = self.class.request(destroy_action, destroy_params, :delete, destroy_prefix, destroy_headers)
    @persisted = false
  end
end

#instance_entity_nameObject



126
127
128
# File 'lib/likeno/entity.rb', line 126

def instance_entity_name
  self.class.entity_name
end

#saveObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/likeno/entity.rb', line 52

def save
  if persisted?
    update
  else
    without_request_error? do
      response = self.class.request(save_action, save_params, :post, save_prefix, save_headers)

      self.id = response[instance_entity_name]["id"]
      self.created_at = response[instance_entity_name]["created_at"] unless response[instance_entity_name]["created_at"].nil?
      self.updated_at = response[instance_entity_name]["updated_at"] unless response[instance_entity_name]["updated_at"].nil?
      @persisted = true
    end
  end
end

#save!Object



67
68
69
70
# File 'lib/likeno/entity.rb', line 67

def save!
  return true if save
  raise Likeno::Errors::RecordInvalid.new(self)
end

#to_hash(options = {}) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/likeno/entity.rb', line 32

def to_hash(options = {})
  hash = {}
  excepts = options[:except].nil? ? [] : options[:except]
  excepts << 'likeno_errors'
  excepts << 'persisted'
  fields.each { |field| hash.merge!(field_to_hash(field)) unless excepts.include? field }
  hash
end

#update(attributes = {}) ⇒ Object



78
79
80
81
82
83
# File 'lib/likeno/entity.rb', line 78

def update(attributes = {})
  attributes.each { |field, value| send("#{field}=", value) if self.class.valid?(field) }
  without_request_error? do
    self.class.request(update_action, update_params, :put, update_prefix, update_headers)
  end
end