module Garage::Representer

Attributes

params[RW]
partial[RW]
representer_attrs[RW]
selector[RW]

Public Class Methods

included(base) click to toggle source
# 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
representers() click to toggle source
# File lib/garage/representer.rb, line 48
def self.representers
  @representers ||= []
end

Public Instance Methods

default_url_options() click to toggle source
# File lib/garage/representer.rb, line 39
def default_url_options
  @default_url_options ||= {}
end
handle_definition?(selector, definition, options) click to toggle source
# 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
partial?() click to toggle source
# File lib/garage/representer.rb, line 6
def partial?
  @partial
end
render_hash(options={}) click to toggle source
# 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!() click to toggle source
# File lib/garage/representer.rb, line 43
def represent!
  self.representer_attrs ||= []
  self.representer_attrs += self.class.representer_attrs
end
resource_class() click to toggle source
# File lib/garage/representer.rb, line 52
def resource_class
  self.class
end
to_resource(options = {}) click to toggle source
# File lib/garage/representer.rb, line 56
def to_resource(options = {})
  self
end