Class: CollectionJSON::Builder
- Inherits:
-
Object
- Object
- CollectionJSON::Builder
- Defined in:
- lib/collection-json/builder.rb
Instance Method Summary collapse
- #add_item(href, params = {}, &block) ⇒ Object
- #add_link(href, rel, params = {}) ⇒ Object
- #add_query(href, rel, params = {}, &block) ⇒ Object
-
#initialize(collection) ⇒ Builder
constructor
A new instance of Builder.
- #set_error(params = {}) ⇒ Object
- #set_template(params = {}, &block) ⇒ Object
Constructor Details
#initialize(collection) ⇒ Builder
Returns a new instance of Builder.
3 4 5 |
# File 'lib/collection-json/builder.rb', line 3 def initialize(collection) @collection = collection end |
Instance Method Details
#add_item(href, params = {}, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/collection-json/builder.rb', line 16 def add_item(href, params = {}, &block) params.merge!({'href' => href}) @collection.items << Item.from_hash(params).tap do |item| if block_given? data = [] links = [] item_builder = ItemBuilder.new(data, links) yield(item_builder) item.data data item.links links end end end |
#add_link(href, rel, params = {}) ⇒ Object
11 12 13 14 |
# File 'lib/collection-json/builder.rb', line 11 def add_link(href, rel, params = {}) params.merge!({'rel' => rel, 'href' => href}) @collection.links << Link.from_hash(params) end |
#add_query(href, rel, params = {}, &block) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/collection-json/builder.rb', line 30 def add_query(href, rel, params = {}, &block) params.merge!({'href' => href, 'rel' => rel}) @collection.queries << Query.from_hash(params).tap do |query| data = [] query_builder = QueryBuilder.new(data) yield(query_builder) if block_given? query.data data end end |
#set_error(params = {}) ⇒ Object
7 8 9 |
# File 'lib/collection-json/builder.rb', line 7 def set_error(params = {}) @collection.error params end |
#set_template(params = {}, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/collection-json/builder.rb', line 40 def set_template(params = {}, &block) if block_given? data = params['data'] || [] template_builder = TemplateBuilder.new(data) yield(template_builder) params.merge!({'data' => data}) end @collection.template params end |