Class: CollectionJson::Item

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

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Item

Returns a new instance of Item.



10
11
12
13
14
15
16
17
# File 'lib/collection_json/item.rb', line 10

def initialize object
  @object  = object
  pre_check
  super object

  @links   = []
  @version = "1.0"
end

Instance Method Details

#attributesObject



47
48
49
50
# File 'lib/collection_json/item.rb', line 47

def attributes
  @attributes = @object.attributes || {}
  @attributes.map{|k,v|  {name: k.to_s, value: v}}
end

#blank_templateObject



23
24
25
# File 'lib/collection_json/item.rb', line 23

def blank_template
  template.map{|h| h[:value]=""; h }
end

#representationObject



37
38
39
40
41
42
43
44
45
# File 'lib/collection_json/item.rb', line 37

def representation
  r = { version: version}

  r.merge!({href:  href})         if href
  r.merge!({links: links})        if links
  r.merge!({items: [singular_representation]})

  { collection: r }
end

#singular_representationObject



31
32
33
34
35
# File 'lib/collection_json/item.rb', line 31

def singular_representation
  item = {}
  item.merge!({href: href})       if href
  item.merge!({data: attributes}) if attributes.any?
end

#templateObject



27
28
29
# File 'lib/collection_json/item.rb', line 27

def template
  attributes
end

#to_jsonObject



19
20
21
# File 'lib/collection_json/item.rb', line 19

def to_json
  representation.to_json
end