Class: Likeno::Entity
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
address, client, endpoint, entity_name, module_name, request
all_action, all_params, default_headers, exists_action, find_action, find_prefix, id_params
#convert_to_hash, #date_time_to_s, #field_to_hash
#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_errors ⇒ Object
Returns the value of attribute likeno_errors.
24
25
26
|
# File 'lib/likeno/entity.rb', line 24
def likeno_errors
@likeno_errors
end
|
#persisted ⇒ Object
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
.all ⇒ Object
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, )
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
94
95
96
|
# File 'lib/likeno/entity.rb', line 94
def self.exists?(id)
request(exists_action, id_params(id), :get, exists_prefix, )['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, )
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
|
#destroy ⇒ Object
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, )
@persisted = false
end
end
|
#instance_entity_name ⇒ Object
126
127
128
|
# File 'lib/likeno/entity.rb', line 126
def instance_entity_name
self.class.entity_name
end
|
#save ⇒ Object
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, )
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
|
#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, )
end
end
|