Class: RestRedmine::Resources::Base
- Inherits:
-
Object
- Object
- RestRedmine::Resources::Base
show all
- Defined in:
- lib/rest_redmine/resources/base.rb
Direct Known Subclasses
Issue
Constant Summary
collapse
- KEYS =
[]
- DEFAULT_MODEL =
{}
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(params = {}) ⇒ Base
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/rest_redmine/resources/base.rb', line 9
def initialize(params={})
@model = {}
@name = self.class.to_s.split("::").last.downcase.to_sym
self.class::KEYS.each do |key|
@model[key] = nil
end
@model.merge!(self.class::DEFAULT_MODEL)
params.merge!(RestRedmine.configuration.resources[@name] || {})
@model.merge!(params)
self.id = params[:id] if params[:id]
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
60
61
62
63
64
65
66
67
68
|
# File 'lib/rest_redmine/resources/base.rb', line 60
def method_missing(method, *args, &block)
if self.class::KEYS.include? method
@model[method.to_sym]
elsif method.to_s =~ /=$/ and self.class::KEYS.include? method.to_s.sub("=", "").to_sym
@model[method.to_s.sub("=", "").to_sym] = args.first
else
super
end
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
4
5
6
|
# File 'lib/rest_redmine/resources/base.rb', line 4
def id
@id
end
|
#model ⇒ Object
Returns the value of attribute model.
4
5
6
|
# File 'lib/rest_redmine/resources/base.rb', line 4
def model
@model
end
|
#name ⇒ Object
Returns the value of attribute name.
4
5
6
|
# File 'lib/rest_redmine/resources/base.rb', line 4
def name
@name
end
|
Instance Method Details
#get_data ⇒ Object
54
55
56
57
58
|
# File 'lib/rest_redmine/resources/base.rb', line 54
def get_data
{
"#{name}" => model
}
end
|
#load ⇒ Object
24
25
26
27
|
# File 'lib/rest_redmine/resources/base.rb', line 24
def load
raise RestRedmine::Exception.new("Id is required") unless self.id
response = request(method: :get)
end
|
#request(method: :get) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/rest_redmine/resources/base.rb', line 35
def request(method: :get)
response = RestRedmine::API.request(self.class::RESOURCE, id: self.id, data: get_data, method: method)
self.id = response[name.to_s]["id"] if response.respond_to? :has_key?
response
end
|
#save ⇒ Object
29
30
31
32
33
|
# File 'lib/rest_redmine/resources/base.rb', line 29
def save
method = self.id ? :put : :post
request(method: method)
end
|