Class: JsonApiServer::MetaBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_server/meta_builder.rb

Overview

Class for building meta element. jsonapi.org/format/#document-meta

Example

MetaBuilder.new
 .add('copyright', "Copyright 2015 Example Corp.")
 .add('authors', ["Yehuda Katz", "Steve Klabnik", "Dan Gebhardt", "Tyler Kellen"])
 .merge({a: 'something', b: 'something else'})
 .meta  # => { "copyright": "Copyright 2015 Example Corp.",
                "authors": ["Yehuda Katz", "Steve Klabnik", "Dan Gebhardt", "Tyler Kellen"],
                a: 'something',
                b: 'something else'
             }

Instance Method Summary collapse

Constructor Details

#initializeMetaBuilder

Returns a new instance of MetaBuilder.



18
19
20
# File 'lib/json_api_server/meta_builder.rb', line 18

def initialize
  @hash = {}
end

Instance Method Details

#add(key, value) ⇒ Object

Add key and value.



23
24
25
26
# File 'lib/json_api_server/meta_builder.rb', line 23

def add(key, value)
  @hash[key] = value
  self
end

#merge(hash) ⇒ Object

Push in multiple key/values with merge.



29
30
31
32
# File 'lib/json_api_server/meta_builder.rb', line 29

def merge(hash)
  @hash.merge!(hash) if hash.respond_to?(:keys) && hash.any?
  self
end

#metaObject

Returns a hash if it has values, nil otherwise.



35
36
37
# File 'lib/json_api_server/meta_builder.rb', line 35

def meta
  @hash.any? ? @hash : nil
end