Class: Quiver::Serialization::JsonApi::ItemTypeHandler
- Inherits:
-
Object
- Object
- Quiver::Serialization::JsonApi::ItemTypeHandler
- Defined in:
- lib/quiver/serialization/json_api/item_type_handler.rb
Instance Attribute Summary collapse
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #attribute(name, opts = {}) ⇒ Object
- #attributes(*names) ⇒ Object
- #calculated_attribute(name, &block) ⇒ Object
-
#initialize(type, config_block, no_id = false) ⇒ ItemTypeHandler
constructor
A new instance of ItemTypeHandler.
- #link(name, opts = {}) ⇒ Object
- #serialize(item, opts = {}) ⇒ Object
Constructor Details
#initialize(type, config_block, no_id = false) ⇒ ItemTypeHandler
Returns a new instance of ItemTypeHandler.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/quiver/serialization/json_api/item_type_handler.rb', line 8 def initialize(type, config_block, no_id=false) self.attributes_storage = {} self.links = {} self.type = type instance_exec(&config_block) raise NoIdError, "no :id in handler for type #{type}" unless no_id || attributes_storage[:id] end |
Instance Attribute Details
#type ⇒ Object
Returns the value of attribute type.
6 7 8 |
# File 'lib/quiver/serialization/json_api/item_type_handler.rb', line 6 def type @type end |
Instance Method Details
#attribute(name, opts = {}) ⇒ Object
23 24 25 |
# File 'lib/quiver/serialization/json_api/item_type_handler.rb', line 23 def attribute(name, opts={}) attributes_storage[name] = opts end |
#attributes(*names) ⇒ Object
27 28 29 30 31 |
# File 'lib/quiver/serialization/json_api/item_type_handler.rb', line 27 def attributes(*names) names.each do |name| attribute(name) end end |
#calculated_attribute(name, &block) ⇒ Object
33 34 35 36 37 |
# File 'lib/quiver/serialization/json_api/item_type_handler.rb', line 33 def calculated_attribute(name, &block) attributes_storage[name] = { proc: block } end |
#link(name, opts = {}) ⇒ Object
19 20 21 |
# File 'lib/quiver/serialization/json_api/item_type_handler.rb', line 19 def link(name, opts={}) links[name] = opts end |
#serialize(item, opts = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/quiver/serialization/json_api/item_type_handler.rb', line 39 def serialize(item, opts={}) context = opts[:context] serialized_links = links.each_with_object({}) do |(name, opts), h| h[name] = {} if href_proc = opts[:resource] h[name][:resource] = context.instance_exec(item, &href_proc) end if self_proc = opts[:self] h[name][:self] = context.instance_exec(item, &self_proc) end if value_proc = opts[:value] value = value_from_proc(value_proc, item) key = value.is_a?(Array) ? :ids : :id h[name][key] = value end if type = opts[:type] h[name][:type] = type end h[name].merge!(scope: context.instance_exec(item, &opts[:scope])) if opts[:scope] end serialized_attributes = attributes_storage.each_with_object({}) do |(name, opts), h| if proc = opts[:proc] h[name] = context.instance_exec(item, &proc) elsif attr_alias = opts[:alias] h[name] = item.send(attr_alias) else h[name] = item.send(name) end end if serialized_links.count > 0 serialized_attributes.merge(links: serialized_links) else serialized_attributes end end |