Module: Garage::Representer

Included in:
Docs::Document, HashRepresenter, Meta::RemoteService
Defined in:
lib/garage/representer.rb

Defined Under Namespace

Modules: ClassMethods Classes: Collection, Definition, Link, NonEncodableValue

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



4
5
6
# File 'lib/garage/representer.rb', line 4

def params
  @params
end

#partialObject

Returns the value of attribute partial.



4
5
6
# File 'lib/garage/representer.rb', line 4

def partial
  @partial
end

#representer_attrsObject

Returns the value of attribute representer_attrs.



4
5
6
# File 'lib/garage/representer.rb', line 4

def representer_attrs
  @representer_attrs
end

#selectorObject

Returns the value of attribute selector.



4
5
6
# File 'lib/garage/representer.rb', line 4

def selector
  @selector
end

Class Method Details

.included(base) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/garage/representer.rb', line 65

def self.included(base)
  self.representers << base

  base.class_eval do
    if Rails.application
      include Rails.application.routes.url_helpers
    end
    extend ClassMethods
  end
end

.representersObject



48
49
50
# File 'lib/garage/representer.rb', line 48

def self.representers
  @representers ||= []
end

Instance Method Details

#default_url_optionsObject



39
40
41
# File 'lib/garage/representer.rb', line 39

def default_url_options
  @default_url_options ||= {}
end

#handle_definition?(selector, definition, options) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
# File 'lib/garage/representer.rb', line 29

def handle_definition?(selector, definition, options)
  if definition.requires_select?
    # definition is not selected by default - it's opt-in
    selector.includes?(definition.name) && definition.selectable?(self, options[:responder])
  else
    # definition is selected by default - it's opt-out
    !selector.excludes?(definition.name)
  end
end


60
61
62
63
# File 'lib/garage/representer.rb', line 60

def link_path_for(rel)
  represent! unless representer_attrs
  representer_attrs.grep(Link).find { |link| link.rel === rel }.try(:pathify, self)
end

#partial?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/garage/representer.rb', line 6

def partial?
  @partial
end

#render_hash(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/garage/representer.rb', line 10

def render_hash(options={})
  obj = {}
  representer_attrs.each do |definition|
    if definition.options[:if]
      next unless definition.options[:if].call(self, options[:responder])
    end

    if definition.respond_to?(:encode)
      next unless handle_definition?(selector, definition, options)
      obj[definition.name] = definition.encode(self, options[:responder], selector[definition.name])
    else
      next if selector.excludes?('_links')
      obj['_links'] ||= {}
      obj['_links'][definition.rel.to_s] = { 'href' => definition.pathify(self) }
    end
  end
  obj
end

#represent!Object



43
44
45
46
# File 'lib/garage/representer.rb', line 43

def represent!
  self.representer_attrs ||= []
  self.representer_attrs += self.class.representer_attrs
end

#resource_classObject



52
53
54
# File 'lib/garage/representer.rb', line 52

def resource_class
  self.class
end

#to_resource(options = {}) ⇒ Object



56
57
58
# File 'lib/garage/representer.rb', line 56

def to_resource(options = {})
  self
end