Module: Representable::Cached

Defined in:
lib/representable/cached.rb

Overview

Using this module only makes sense with Decorator representers.

Defined Under Namespace

Modules: Binding, Serializer

Instance Method Summary collapse

Instance Method Details

#representable_mapper(format, options) ⇒ Object

The main point here is that the decorator instance simply saves its mapper. Since the mapper in turn stores the bindings, we have a straight-forward way of “caching” the bindings without having to mess around on the class level: this all happens in the decorator instance.

Every binding in turn stores its nested representer (if it has one), implementing a recursive caching.

Decorator -> Mapper -> [Binding->Decorator, Binding]



11
12
13
14
15
# File 'lib/representable/cached.rb', line 11

def representable_mapper(format, options)
  @mapper ||= super.tap do |mapper|
    mapper.bindings(represented, options).each { |binding| binding.extend(Binding) }
  end
end

#update!(represented) ⇒ Object

replace represented for each property in this representer.



18
19
20
21
# File 'lib/representable/cached.rb', line 18

def update!(represented)
  @represented = represented
  self
end