class Garage::Representer::Definition

Constants

PRIMITIVE_CLASSES

Attributes

options[R]

Public Class Methods

new(name, options={}) click to toggle source
# File lib/garage/representer.rb, line 135
def initialize(name, options={})
  @name = name
  @options = options
end

Public Instance Methods

encode(object, responder, selector = nil) click to toggle source
# File lib/garage/representer.rb, line 156
def encode(object, responder, selector = nil)
  value = object.send(@name)
  encode_value(value, responder, selector)
end
encode_value(value, responder, selector) click to toggle source
# File lib/garage/representer.rb, line 161
def encode_value(value, responder, selector)
  if value.is_a?(Garage::Representer)
    responder.encode_to_hash(value, partial: true, selector: selector)
  elsif primitive?(value)
    value
  else
    raise NonEncodableValue, "#{value.class} can not be encoded directly. Forgot to include Garage::Representer?"
  end
end
name() click to toggle source
# File lib/garage/representer.rb, line 152
def name
  (@options[:as] || @name).to_s
end
primitive?(value) click to toggle source
# File lib/garage/representer.rb, line 186
def primitive?(value)
  PRIMITIVE_CLASSES.any? {|k| value.is_a?(k) }
end
requires_select?() click to toggle source
# File lib/garage/representer.rb, line 140
def requires_select?
  @options[:selectable]
end
selectable?(*args) click to toggle source
# File lib/garage/representer.rb, line 144
def selectable?(*args)
  if boolean?(@options[:selectable])
    @options[:selectable]
  else
    @options[:selectable].call(*args)
  end
end

Private Instance Methods

boolean?(value) click to toggle source
# File lib/garage/representer.rb, line 192
def boolean?(value)
  value.is_a?(TrueClass) || value.is_a?(FalseClass)
end