Class: CollectionJson::Collection

Inherits:
Object
  • Object
show all
Extended by:
FunkyAccessor
Defined in:
lib/collection_json/collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ Collection

Returns a new instance of Collection.



6
7
8
9
10
11
# File 'lib/collection_json/collection.rb', line 6

def initialize items
  @version    = "1.0"
  @items      = items.map { |i| Item.new(i) }
  @template = @items.first.blank_template if @items.any?
  @links = []
end

Instance Method Details

#item_representationsObject



30
31
32
# File 'lib/collection_json/collection.rb', line 30

def item_representations
  items.map &:singular_representation
end

#representationObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/collection_json/collection.rb', line 13

def representation
  collection = {
      version:  version,
      href:     href,
      links:    links,
      items:    item_representations
    }

  collection.merge!({template: {data: template}}) if template

  {collection: collection}
end

#to_jsonObject



26
27
28
# File 'lib/collection_json/collection.rb', line 26

def to_json
  representation.to_json
end