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.



2
3
4
# File 'lib/garage/representer.rb', line 2

def params
  @params
end

#partialObject

Returns the value of attribute partial.



2
3
4
# File 'lib/garage/representer.rb', line 2

def partial
  @partial
end

#representer_attrsObject

Returns the value of attribute representer_attrs.



2
3
4
# File 'lib/garage/representer.rb', line 2

def representer_attrs
  @representer_attrs
end

#selectorObject

Returns the value of attribute selector.



2
3
4
# File 'lib/garage/representer.rb', line 2

def selector
  @selector
end

Class Method Details

.included(base) ⇒ Object



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

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



46
47
48
# File 'lib/garage/representer.rb', line 46

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

Instance Method Details

#default_url_optionsObject



37
38
39
# File 'lib/garage/representer.rb', line 37

def default_url_options
  @default_url_options ||= {}
end

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

Returns:

  • (Boolean)


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

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


58
59
60
61
# File 'lib/garage/representer.rb', line 58

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)


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

def partial?
  @partial
end

#render_hash(options = {}) ⇒ Object



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

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



41
42
43
44
# File 'lib/garage/representer.rb', line 41

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

#resource_classObject



50
51
52
# File 'lib/garage/representer.rb', line 50

def resource_class
  self.class
end

#to_resourceObject



54
55
56
# File 'lib/garage/representer.rb', line 54

def to_resource
  self
end