Class: Leif::CollectionJson::Collection::Template

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Enumerable
Defined in:
lib/leif/collection_json.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, href, method) ⇒ Template

Returns a new instance of Template.



54
55
56
57
58
# File 'lib/leif/collection_json.rb', line 54

def initialize(template, href, method)
  @href   = href
  @method = method
  super template
end

Instance Attribute Details

#hrefObject

Returns the value of attribute href.



52
53
54
# File 'lib/leif/collection_json.rb', line 52

def href
  @href
end

#methodObject

Returns the value of attribute method.



52
53
54
# File 'lib/leif/collection_json.rb', line 52

def method
  @method
end

Instance Method Details

#convert_to_jsonObject



64
65
66
67
68
# File 'lib/leif/collection_json.rb', line 64

def convert_to_json
  fetch('data').each_with_object({}) do |datum, json|
    json[datum['name']] = datum['value']
  end
end

#each(&block) ⇒ Object



60
61
62
# File 'lib/leif/collection_json.rb', line 60

def each(&block)
  convert_to_json.each(&block)
end

#fill_field(name, value) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/leif/collection_json.rb', line 70

def fill_field(name, value)
  new_data = fetch('data').map {|datum|
    datum = datum.clone
    datum['value'] = value if datum['name'] == name
    datum
  }
  new_template = {'data' => new_data }
  Template.new(new_template, href, method)
end