module Representable

Caching of Bindings in Decorators, this could be an instance class var. when inherited, it is automatically busted. however, in modules we can't do that. we never know when a module is “finalised”, so we don't really know when to bust the cache.

Constants

VERSION

Attributes

representable_attrs[W]

Public Class Methods

included(base) click to toggle source
# File lib/representable.rb, line 23
def self.included(base)
  base.class_eval do
    extend Declarative
    extend ClassInclusions, ModuleExtensions
    extend ClassMethods
    extend Feature
    extend ForCollection
    extend Represent
    extend Apply
    # register_feature Representable # Representable gets included automatically when creating inline representer.
  end
end

Private Instance Methods

create_representation_with(doc, options, format) click to toggle source

Compiles the document going through all properties.

# File lib/representable.rb, line 45
def create_representation_with(doc, options, format)
  propagated_options, private_options = normalize_options!(options)

  representable_mapper(format, propagated_options).serialize(represented, doc, propagated_options, private_options)
end
normalize_options!(options) click to toggle source

Make sure we do not change original options. However, private options like :include or :wrap are not passed on to child representers.

# File lib/representable.rb, line 61
def normalize_options!(options)
  # here, we could also filter out local options e.g. like options[:band].
  private_options = {}
  return [options, private_options] if options.size == 0

  propagated_options = options.dup

  private_options[:include] = propagated_options.delete(:include) if options[:include]
  private_options[:exclude] = propagated_options.delete(:exclude) if options[:exclude]
  propagated_options.delete(:wrap) # FIXME.

  [propagated_options, private_options]
end
representable_attrs() click to toggle source
# File lib/representable.rb, line 75
def representable_attrs
  @representable_attrs ||= self.class.representable_attrs # DISCUSS: copy, or better not? what about "freezing"?
end
representable_binding_for(definition, format, options) click to toggle source
# File lib/representable.rb, line 55
def representable_binding_for(definition, format, options)
  format.build(definition, self)
end
representable_bindings_for(format, options) click to toggle source
# File lib/representable.rb, line 51
def representable_bindings_for(format, options)
  representable_attrs.collect {|definition| representable_binding_for(definition, format, options) }
end
representable_mapper(format, options) click to toggle source
# File lib/representable.rb, line 79
def representable_mapper(format, options)
  bindings = representable_bindings_for(format, options)
  Mapper.new(bindings)
end
representation_wrap(*args) click to toggle source
# File lib/representable.rb, line 84
def representation_wrap(*args)
  representable_attrs.wrap_for(nil, represented, *args) { self.class.name }
end
represented() click to toggle source
# File lib/representable.rb, line 88
def represented
  self
end
update_properties_from(doc, options, format) click to toggle source

Reads values from doc and sets properties accordingly.

# File lib/representable.rb, line 38
def update_properties_from(doc, options, format)
  propagated_options, private_options = normalize_options!(options)

  representable_mapper(format, propagated_options).deserialize(represented, doc, propagated_options, private_options)
end