Class: MagLoft::RemoteResource
- Inherits:
-
Object
- Object
- MagLoft::RemoteResource
show all
- Defined in:
- lib/magloft/remote_resource.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ RemoteResource
Returns a new instance of RemoteResource.
6
7
8
9
|
# File 'lib/magloft/remote_resource.rb', line 6
def initialize(attributes = {})
allowed_attributes = attributes.slice(*self.class.remote_attributes)
Dialers::AssignAttributes.call(self, allowed_attributes.except(:id))
end
|
Instance Attribute Details
#destroyed ⇒ Object
Returns the value of attribute destroyed.
4
5
6
|
# File 'lib/magloft/remote_resource.rb', line 4
def destroyed
@destroyed
end
|
#id ⇒ Object
Returns the value of attribute id.
3
4
5
|
# File 'lib/magloft/remote_resource.rb', line 3
def id
@id
end
|
Class Method Details
.all ⇒ Object
91
92
93
|
# File 'lib/magloft/remote_resource.rb', line 91
def all
api.get(endpoint).transform_to_many(self)
end
|
.create(attributes = {}) ⇒ Object
95
96
97
98
99
|
# File 'lib/magloft/remote_resource.rb', line 95
def create(attributes = {})
entity = new(attributes)
entity.save
entity
end
|
.endpoint(path = nil) ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/magloft/remote_resource.rb', line 71
def endpoint(path = nil)
if path.nil?
@endpoint
else
@endpoint = path
end
end
|
.find(id) ⇒ Object
79
80
81
|
# File 'lib/magloft/remote_resource.rb', line 79
def find(id)
api.get("#{endpoint}/#{id}").transform_to_one(self)
end
|
.find_one(params) ⇒ Object
83
84
85
|
# File 'lib/magloft/remote_resource.rb', line 83
def find_one(params)
where(params).first
end
|
.method_missing(name, *args, &block) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/magloft/remote_resource.rb', line 101
def method_missing(name, *args, &block)
if name[0..7] == "find_by_" and args.length == 1
attribute = name[8..-1].to_sym
if remote_attributes.include?(attribute)
params = {}
params[attribute] = args.first
return find_one(params)
end
end
super
end
|
.remote_attribute(*args) ⇒ Object
63
64
65
66
67
68
69
|
# File 'lib/magloft/remote_resource.rb', line 63
def remote_attribute(*args)
args.each do |arg|
remote_attributes.push(arg)
class_eval("attr_accessor :#{arg}", __FILE__, __LINE__)
class_eval("def #{arg}=(val);update_data(:#{arg}, val);end", __FILE__, __LINE__)
end
end
|
.remote_attributes ⇒ Object
59
60
61
|
# File 'lib/magloft/remote_resource.rb', line 59
def remote_attributes
@remote_attributes ||= []
end
|
.respond_to_missing?(method_name, include_private = false) ⇒ Boolean
113
114
115
|
# File 'lib/magloft/remote_resource.rb', line 113
def respond_to_missing?(method_name, include_private = false)
method_name.to_s.start_with?('find_by_') || super
end
|
.where(params) ⇒ Object
87
88
89
|
# File 'lib/magloft/remote_resource.rb', line 87
def where(params)
api.get(endpoint, params).transform_to_many(self)
end
|
Instance Method Details
#changed? ⇒ Boolean
11
12
13
|
# File 'lib/magloft/remote_resource.rb', line 11
def changed?
changed_data.keys.count > 0
end
|
#changed_data ⇒ Object
42
43
44
|
# File 'lib/magloft/remote_resource.rb', line 42
def changed_data
@changed_data ||= {}
end
|
#clear_changed_data! ⇒ Object
46
47
48
49
|
# File 'lib/magloft/remote_resource.rb', line 46
def clear_changed_data!
@changed_data = {}
self
end
|
#destroy ⇒ Object
33
34
35
36
37
38
39
40
|
# File 'lib/magloft/remote_resource.rb', line 33
def destroy
return false if id.nil? or destroyed?
transformable = Api.client.api_caller.delete("#{self.class.endpoint}/#{id}")
transformable.transform_to_existing(self)
@destroyed = true
clear_changed_data!
self
end
|
#destroyed? ⇒ Boolean
15
16
17
|
# File 'lib/magloft/remote_resource.rb', line 15
def destroyed?
destroyed == true
end
|
#save ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/magloft/remote_resource.rb', line 19
def save
return false if destroyed? or !changed?
if changed_data.keys.count > 0
if id.nil?
transformable = Api.client.api_caller.post(self.class.endpoint, changed_data)
else
transformable = Api.client.api_caller.put("#{self.class.endpoint}/#{id}", changed_data.except(:id))
end
transformable.transform_to_existing(self)
clear_changed_data!
end
self
end
|
#update_data(key, value) ⇒ Object
51
52
53
54
55
56
|
# File 'lib/magloft/remote_resource.rb', line 51
def update_data(key, value)
if send(key) != value
instance_variable_set("@#{key}", value)
changed_data[key] = value
end
end
|