Class: JsonApiServer::MetaBuilder
- Inherits:
-
Object
- Object
- JsonApiServer::MetaBuilder
- 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
-
#add(key, value) ⇒ Object
Add key and value.
-
#initialize ⇒ MetaBuilder
constructor
A new instance of MetaBuilder.
-
#merge(hash) ⇒ Object
Push in multiple key/values with merge.
-
#meta ⇒ Object
Returns a hash if it has values, nil otherwise.
Constructor Details
#initialize ⇒ MetaBuilder
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 |
#meta ⇒ Object
Returns a hash if it has values, nil otherwise.
35 36 37 |
# File 'lib/json_api_server/meta_builder.rb', line 35 def @hash.any? ? @hash : nil end |