Class: Api::Presenter::Resource
- Inherits:
-
Object
- Object
- Api::Presenter::Resource
show all
- Defined in:
- lib/api/presenter/resource.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(resource) ⇒ Resource
Returns a new instance of Resource.
4
5
6
|
# File 'lib/api/presenter/resource.rb', line 4
def initialize(resource)
@resource = resource
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/api/presenter/resource.rb', line 48
def method_missing(method, *args, &block)
if self.class.properties.include? method
@resource.send(method, *args, &block)
else
super
end
end
|
Class Method Details
.host ⇒ Object
27
28
29
|
# File 'lib/api/presenter/resource.rb', line 27
def host
@@host ||= ''
end
|
.host=(v) ⇒ Object
31
32
33
|
# File 'lib/api/presenter/resource.rb', line 31
def host=(v)
@@host = v
end
|
.inherited(subclass) ⇒ Object
43
44
45
|
# File 'lib/api/presenter/resource.rb', line 43
def inherited(subclass)
(subclass.properties << properties).flatten!
end
|
.link(name, value, &condition) ⇒ Object
17
18
19
20
21
|
# File 'lib/api/presenter/resource.rb', line 17
def link(name, value, &condition)
condition = Proc.new { true } unless block_given?
links[name] = { value: value, condition: condition }
end
|
.links ⇒ Object
23
24
25
|
# File 'lib/api/presenter/resource.rb', line 23
def links
@links ||= {}
end
|
.prefix ⇒ Object
35
36
37
|
# File 'lib/api/presenter/resource.rb', line 35
def prefix
@@prefix ||= ''
end
|
.prefix=(v) ⇒ Object
39
40
41
|
# File 'lib/api/presenter/resource.rb', line 39
def prefix=(v)
@@prefix = v
end
|
.properties ⇒ Object
13
14
15
|
# File 'lib/api/presenter/resource.rb', line 13
def properties
@properties ||= []
end
|
.property(value) ⇒ Object
9
10
11
|
# File 'lib/api/presenter/resource.rb', line 9
def property(value)
properties << value unless properties.include? value
end
|
Instance Method Details
#build_links(options = {}) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/api/presenter/resource.rb', line 56
def build_links(options = {})
links = {}
self.class.links.each do |link_name, link_value|
link_actual_value = link_value[:value].dup
stubs = link_actual_value.scan(/\{\{(\w+)\}\}/).flatten
stubs.each{ |stub| link_actual_value.gsub!(/\{\{#{stub}\}\}/, self.send(stub.to_sym).to_s) }
links[link_name.to_s] = { "href" => "#{self.class.host}#{self.class.prefix}#{link_actual_value}" } if link_value[:condition].call(options)
end
links
end
|
#self_link?(options = {}) ⇒ Boolean
75
76
77
|
# File 'lib/api/presenter/resource.rb', line 75
def self_link?(options = {})
true
end
|