module Representable::ForCollection

Gives us Representer::for_collection and its configuration directive ::collection_representer.

Public Instance Methods

for_collection() click to toggle source
# File lib/representable/for_collection.rb, line 5
def for_collection
  # this is done at run-time, not a big fan of this. however, it saves us from inheritance/self problems.
  @collection_representer ||= collection_representer!({}) # DON'T make it inheritable as it would inherit the wrong singular.
end

Private Instance Methods

collection_representer(options={}) click to toggle source
# File lib/representable/for_collection.rb, line 21
def collection_representer(options={})
  @collection_representer = collection_representer!(options)
end
collection_representer!(options) click to toggle source
# File lib/representable/for_collection.rb, line 11
def collection_representer!(options)
  singular = self

  # what happens here is basically
  # Module.new { include Representable::JSON::Collection; ... }
  build_inline(nil, [singular.collection_representer_class], "", {}) {
    items options.merge(:extend => singular)
  }
end