Class: CollectionJSON::ItemBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/collection-json/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, links) ⇒ ItemBuilder

Returns a new instance of ItemBuilder.



57
58
59
60
# File 'lib/collection-json/builder.rb', line 57

def initialize(data, links)
  @data = data
  @links = links
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



55
56
57
# File 'lib/collection-json/builder.rb', line 55

def data
  @data
end

Returns the value of attribute links.



55
56
57
# File 'lib/collection-json/builder.rb', line 55

def links
  @links
end

Instance Method Details

#add_data(name, value = '', prompt = '') ⇒ Object



62
63
64
65
66
67
# File 'lib/collection-json/builder.rb', line 62

def add_data(name, value = '', prompt = '')
  data << {name: name}.tap do |data|
    data.merge!({'value' => value}) if value != ''
    data.merge!({'prompt' => prompt}) if prompt != ''
  end
end


69
70
71
72
73
74
75
76
# File 'lib/collection-json/builder.rb', line 69

def add_link(href, rel, name = '', prompt = '', render = '')
  href = CollectionJSON.add_host(href)
  links << Link.from_hash({'href' => href, 'rel' => rel}).tap do |link|
    link.merge!({'name' => name}) if name != ''
    link.merge!({'prompt' => prompt}) if prompt != ''
    link.merge!({'render' => render}) if render != ''
  end
end