class Representable::Definition

Created at class compile time. Keeps configuration options for one property.

Attributes

getter[R]
name[R]

Public Class Methods

new(sym, options={}, &block) click to toggle source
# File lib/representable/definition.rb, line 12
def initialize(sym, options={}, &block)
  @options = Cloneable::Hash.new # allows deep cloning. we then had to set Pipeline cloneable.
  @name    = sym.to_s
  options  = options.clone

  # defaults:
  options[:parse_filter]  = Pipeline[*options[:parse_filter]]
  options[:render_filter] = Pipeline[*options[:render_filter]]
  options[:as]          ||= @name

  setup!(options, &block)
end

Public Instance Methods

[](name) click to toggle source
# File lib/representable/definition.rb, line 41
def [](name)
  @runtime_options[name]
end
array?() click to toggle source
# File lib/representable/definition.rb, line 62
def array?
  self[:collection]
end
clone() click to toggle source
# File lib/representable/definition.rb, line 45
def clone
  self.class.new(name, @options.clone)
end
create_binding(*args) click to toggle source
# File lib/representable/definition.rb, line 78
def create_binding(*args)
  self[:binding].call(self, *args)
end
delete!(name) click to toggle source
# File lib/representable/definition.rb, line 35
def delete!(name)
  @runtime_options.delete(name)
  @options.delete(name)
  self
end
has_default?() click to toggle source
# File lib/representable/definition.rb, line 70
def has_default?
  @options.has_key?(:default)
end
hash?() click to toggle source
# File lib/representable/definition.rb, line 66
def hash?
  self[:hash]
end
inspect() click to toggle source
# File lib/representable/definition.rb, line 82
def inspect
  state = (instance_variables-[:@runtime_options, :@name]).collect { |ivar| "#{ivar}=#{instance_variable_get(ivar)}" }
  "#<Representable::Definition ==>#{name} #{state.join(" ")}>"
end
merge!(options, &block) click to toggle source
# File lib/representable/definition.rb, line 25
def merge!(options, &block)
  options = options.clone

  options[:parse_filter]  = @options[:parse_filter].push(*options[:parse_filter])
  options[:render_filter] = @options[:render_filter].push(*options[:render_filter])

  setup!(options, &block) # FIXME: this doesn't yield :as etc.
  self
end
representable?() click to toggle source
# File lib/representable/definition.rb, line 57
def representable?
  return if self[:representable] == false
  self[:representable] or typed?
end
representer_module() click to toggle source
# File lib/representable/definition.rb, line 74
def representer_module
  @options[:extend]
end
setter() click to toggle source
# File lib/representable/definition.rb, line 49
def setter
  :"#{name}="
end
typed?() click to toggle source
# File lib/representable/definition.rb, line 53
def typed?
  self[:class] or self[:extend] or self[:instance]
end

Private Instance Methods

dynamic_options() click to toggle source
# File lib/representable/definition.rb, line 112
def dynamic_options
  [:as, :getter, :setter, :class, :instance, :reader, :writer, :extend, :prepare, :if, :deserialize, :serialize, :render_filter, :parse_filter, :skip_parse, :skip_render]
end
handle_as!(options) click to toggle source
# File lib/representable/definition.rb, line 120
def handle_as!(options)
  options[:as] = options[:as].to_s if options[:as].is_a?(Symbol) # Allow symbols for as:
end
handle_extend!(options) click to toggle source
# File lib/representable/definition.rb, line 116
def handle_extend!(options)
  mod = options.delete(:extend) || options.delete(:decorator) and options[:extend] = mod
end
runtime_options!(options) click to toggle source

wrapping dynamic options in Value does save runtime, as this is used very frequently (and totally unnecessary to wrap an option at runtime, its value never changes).

# File lib/representable/definition.rb, line 103
def runtime_options!(options)
  @runtime_options = {}

  for name, value in options
    value = Uber::Options::Value.new(value) if dynamic_options.include?(name)
    @runtime_options[name] = value
  end
end
setup!(options) { |options| ... } click to toggle source
# File lib/representable/definition.rb, line 88
def setup!(options, &block)
  handle_extend!(options)
  handle_as!(options)

  # DISCUSS: we could call more macros here (e.g. for :nested).
  Representable::ParseStrategy.apply!(options)

  yield options if block_given?
  @options.merge!(options)

  runtime_options!(@options)
end