Class: CollectionJson::Item
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- CollectionJson::Item
- Extended by:
- FunkyAccessor
- Defined in:
- lib/collection_json/item.rb
Instance Method Summary collapse
- #attributes ⇒ Object
- #blank_template ⇒ Object
-
#initialize(object) ⇒ Item
constructor
A new instance of Item.
- #representation ⇒ Object
- #singular_representation ⇒ Object
- #template ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(object) ⇒ Item
Returns a new instance of Item.
10 11 12 13 14 15 16 17 |
# File 'lib/collection_json/item.rb', line 10 def initialize object @object = object pre_check super object @links = [] @version = "1.0" end |
Instance Method Details
#attributes ⇒ Object
47 48 49 50 |
# File 'lib/collection_json/item.rb', line 47 def attributes @attributes = @object.attributes || {} @attributes.map{|k,v| {name: k.to_s, value: v}} end |
#blank_template ⇒ Object
23 24 25 |
# File 'lib/collection_json/item.rb', line 23 def blank_template template.map{|h| h[:value]=""; h } end |
#representation ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/collection_json/item.rb', line 37 def representation r = { version: version} r.merge!({href: href}) if href r.merge!({links: links}) if links r.merge!({items: [singular_representation]}) { collection: r } end |
#singular_representation ⇒ Object
31 32 33 34 35 |
# File 'lib/collection_json/item.rb', line 31 def singular_representation item = {} item.merge!({href: href}) if href item.merge!({data: attributes}) if attributes.any? end |
#template ⇒ Object
27 28 29 |
# File 'lib/collection_json/item.rb', line 27 def template attributes end |
#to_json ⇒ Object
19 20 21 |
# File 'lib/collection_json/item.rb', line 19 def to_json representation.to_json end |