Module: CollectionJson::Decorator

Defined in:
lib/collection_json/decorator.rb

Instance Method Summary collapse

Instance Method Details

#decorate(object) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/collection_json/decorator.rb', line 2

def decorate object
  klass = object.respond_to?(:each) ? CollectionJson::Collection : CollectionJson::Item

  klass.new(object).tap do |o|
    if object.respond_to? :each
      o.items.each { |i| yield o, i } if block_given?
    else
      yield o if block_given?
    end
  end
end