Class: Api::Presenter::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/api/presenter/resource.rb

Direct Known Subclasses

CollectionResource

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

.hostObject



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


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


23
24
25
# File 'lib/api/presenter/resource.rb', line 23

def links
  @links ||= {}
end

.prefixObject



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

.propertiesObject



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



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

    # retrieve stubs to replace looking for {{method_to_call}}
    stubs = link_actual_value.scan(/\{\{(\w+)\}\}/).flatten

    # now we replace them
    stubs.each{ |stub| link_actual_value.gsub!(/\{\{#{stub}\}\}/, self.send(stub.to_sym).to_s) }

    # and finish the url
    links[link_name.to_s] = { "href" => "#{self.class.host}#{self.class.prefix}#{link_actual_value}" } if link_value[:condition].call(options)
  end

  links
end

#presentObject



79
80
81
# File 'lib/api/presenter/resource.rb', line 79

def present
  Api::Presenter::Hypermedia.present self
end

#self_link?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/api/presenter/resource.rb', line 75

def self_link?(options = {})
  true
end