class Representable::Definition
Created at class compile time. Keeps configuration options for one property.
Public Class Methods
Source
# File lib/representable/definition.rb, line 11 def initialize(sym, options={}, &block) options[:extend] = options[:nested] if options[:nested] super # defaults: options[:parse_filter] = Pipeline[*options[:parse_filter]] options[:render_filter] = Pipeline[*options[:render_filter]] setup!(options, &block) end
Calls superclass method
Public Instance Methods
Source
# File lib/representable/definition.rb, line 44 def [](name) # return nil if name==:extend && self[:nested].nil? # return Uber::Options::Value.new(self[:nested]) if name==:extend @runtime_options[name] end
Source
# File lib/representable/definition.rb, line 80 def create_binding(*args) self[:binding].call(self, *args) end
Source
# File lib/representable/definition.rb, line 38 def delete!(name) @runtime_options.delete(name) @options.delete(name) self end
Source
# File lib/representable/definition.rb, line 72 def has_default? @options.has_key?(:default) end
Source
# File lib/representable/definition.rb, line 84 def inspect state = (instance_variables-[:@runtime_options, :@name]).collect { |ivar| "#{ivar}=#{instance_variable_get(ivar)}" } "#<Representable::Definition ==>#{name} #{state.join(" ")}>" end
Source
# File lib/representable/definition.rb, line 28 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
Source
# File lib/representable/definition.rb, line 23 def name self[:name] end
Also aliased as: getter
Source
# File lib/representable/definition.rb, line 58 def representable? return if self[:representable] == false self[:representable] or typed? end
Source
# File lib/representable/definition.rb, line 76 def representer_module @options[:extend] end
Source
# File lib/representable/definition.rb, line 54 def typed? # TODO: remove. self[:class] or self[:extend] or self[:instance] end
Private Instance Methods
Source
# File lib/representable/definition.rb, line 114 def dynamic_options [:as, :getter, :setter, :class, :instance, :reader, :writer, :extend, :prepare, :if, :deserialize, :serialize, :skip_parse, :skip_render] end
Source
# File lib/representable/definition.rb, line 122 def handle_as!(options) options[:as] = options[:as].to_s if options[:as].is_a?(Symbol) # Allow symbols for as: end
Source
# File lib/representable/definition.rb, line 118 def handle_extend!(options) mod = options.delete(:extend) || options.delete(:decorator) and options[:extend] = mod end
Source
# File lib/representable/definition.rb, line 105 def runtime_options!(options) @runtime_options = {} for name, value in options value = ::Representable::Option(value) if dynamic_options.include?(name) @runtime_options[name] = value end end
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).
Source
# File lib/representable/definition.rb, line 90 def setup!(options, &block) handle_extend!(options) handle_as!(options) # DISCUSS: we could call more macros here (e.g. for :nested). Representable::Populator.apply!(options) yield options if block_given? @options.merge!(options) runtime_options!(@options) end