Class: Clubhouse::BaseResource
- Inherits:
-
Object
- Object
- Clubhouse::BaseResource
show all
- Defined in:
- lib/clubhouse/base_resource.rb
Direct Known Subclasses
Comment, Epic, Epic::Comment, File, Label, LinkedFile, Project, Story, StoryLink, Task, User, Workflow
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attr = {}) ⇒ BaseResource
Returns a new instance of BaseResource.
33
34
35
36
37
38
39
40
41
|
# File 'lib/clubhouse/base_resource.rb', line 33
def initialize(attr = {})
attr.each do |key, value|
begin
send("#{key}=", value)
rescue NoMethodError
raise InvalidKeyAssignment, "You can't assign value to #{key}"
end
end
end
|
Class Attribute Details
.endpoint ⇒ Object
Returns the value of attribute endpoint.
9
10
11
|
# File 'lib/clubhouse/base_resource.rb', line 9
def endpoint
@endpoint
end
|
Instance Attribute Details
Class Method Details
.all ⇒ Object
27
28
29
30
|
# File 'lib/clubhouse/base_resource.rb', line 27
def all
payload = client.get(self.endpoint)
payload.collect{|h| self.new.update_object_from_payload(h) }
end
|
.attributes(*keys, **opts) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/clubhouse/base_resource.rb', line 43
def self.attributes(*keys, **opts)
readonly = Array(opts[:readonly])
class_eval do
define_method(:attribute_keys) { (keys + readonly) }
(keys + readonly).each do |key|
define_method(:"#{key}") { instance_variable_get("@#{key}") }
if !readonly.include?(key)
define_method(:"#{key}=") {|v| instance_variable_set("@#{key}", v) }
end
end
end
end
|
.attributes_for_create(*keys) ⇒ Object
69
70
71
72
73
74
75
76
77
|
# File 'lib/clubhouse/base_resource.rb', line 69
def self.attributes_for_create(*keys)
class_eval do
define_method :create_attributes do
keys.reduce({}) do |h, k|
h.merge(k => instance_variable_get("@#{k}"))
end.reject{|k,v| v.nil?}
end
end
end
|
.attributes_for_update(*keys) ⇒ Object
59
60
61
62
63
64
65
66
67
|
# File 'lib/clubhouse/base_resource.rb', line 59
def self.attributes_for_update(*keys)
class_eval do
define_method :update_attributes do
keys.reduce({}) do |h, k|
h.merge(k => instance_variable_get("@#{k}"))
end.reject{|k,v| v.nil?}
end
end
end
|
.delete(id) ⇒ Object
23
24
25
|
# File 'lib/clubhouse/base_resource.rb', line 23
def delete(id)
client.delete("#{self.endpoint}/#{id}")
end
|
.find(id) ⇒ Object
19
20
21
|
# File 'lib/clubhouse/base_resource.rb', line 19
def find(id)
client.find(self, self.endpoint, id)
end
|
.resource(name) ⇒ Object
11
12
13
|
# File 'lib/clubhouse/base_resource.rb', line 11
def resource(name)
@endpoint = name
end
|
Instance Method Details
#reload ⇒ Object
91
92
93
94
|
# File 'lib/clubhouse/base_resource.rb', line 91
def reload
payload = client.get("#{self.class.endpoint}/#{id}")
update_object_from_payload(payload)
end
|
#save ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/clubhouse/base_resource.rb', line 96
def save
raise ClientNotSetup, "A default client or instance client is not setup" unless client
payload = if id
client.put("#{self.class.endpoint}/#{id}", update_attributes)
else
client.post(self.class.endpoint, create_attributes)
end
update_object_from_payload(payload)
end
|
#update_object_from_payload(payload) ⇒ Object
83
84
85
86
87
88
89
|
# File 'lib/clubhouse/base_resource.rb', line 83
def update_object_from_payload(payload)
attribute_keys.each do |k|
self.instance_variable_set("@#{k}", payload[k.to_s])
end
self
end
|