Class: HCl::TimesheetResource
- Inherits:
-
Object
- Object
- HCl::TimesheetResource
show all
- Defined in:
- lib/hcl/timesheet_resource.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of TimesheetResource.
3
4
5
|
# File 'lib/hcl/timesheet_resource.rb', line 3
def initialize params
@data = params
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
11
12
13
|
# File 'lib/hcl/timesheet_resource.rb', line 11
def method_missing method, *args
@data.key?(method.to_sym) ? @data[method] : super
end
|
Class Method Details
._prepare_resource(name, *args, &url_cb) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/hcl/timesheet_resource.rb', line 20
def _prepare_resource name, *args, &url_cb
((@resources ||= {})[name] = {}).tap do |res|
opt_or_cb = args.pop
res[:url_cb] = url_cb
res[:opts] = {}
case opt_or_cb
when String
res[:url_cb] = ->() { opt_or_cb }
res[:opts] = args.pop || {}
when Hash
res[:opts] = opt_or_cb
url = args.pop
res[:url_cb] = ->() { url } if url
end
end
end
|
.collection_name(name = nil) ⇒ Object
63
64
65
|
# File 'lib/hcl/timesheet_resource.rb', line 63
def collection_name name=nil
name ? (@collection_name = name) : @collection_name
end
|
.resource(name, *args, &url_cb) ⇒ Object
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/hcl/timesheet_resource.rb', line 48
def resource name, *args, &url_cb
res = _prepare_resource name, *args, &url_cb
cls = res[:opts][:class_name] ? HCl.const_get(res[:opts][:class_name]) : self
method = cls == self ? :define_singleton_method : :define_method
send(method, name) do |http, *args|
url = instance_exec *args, &res[:url_cb]
cb = res[:opts][:load_cb]
cls.new http.get(url).tap{|e| cb.call(e) if cb }[cls.underscore_name]
end
end
|
.resources(name, *args, &url_cb) ⇒ Object
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/hcl/timesheet_resource.rb', line 37
def resources name, *args, &url_cb
res = _prepare_resource name, *args, &url_cb
cls = res[:opts][:class_name] ? HCl.const_get(res[:opts][:class_name]) : self
method = cls == self ? :define_singleton_method : :define_method
send(method, name) do |http, *args|
url = instance_exec *args, &res[:url_cb]
cb = res[:opts][:load_cb]
http.get(url).tap{|e| cb.call(e) if cb }[cls.collection_name].map{|e|new(e)}
end
end
|
.underscore_name ⇒ Object
59
60
61
|
# File 'lib/hcl/timesheet_resource.rb', line 59
def underscore_name
@underscore_name ||= name.split('::').last.split(/(?=[A-Z])/).map(&:downcase).join('_').to_sym
end
|
Instance Method Details
#id ⇒ Object
7
8
9
|
# File 'lib/hcl/timesheet_resource.rb', line 7
def id
@data[:id]
end
|
#respond_to?(method, include_all = false) ⇒ Boolean
15
16
17
|
# File 'lib/hcl/timesheet_resource.rb', line 15
def respond_to? method, include_all=false
(@data && @data.key?(method.to_sym)) || super
end
|