Class: Oat::Adapters::JsonAPI
- Inherits:
-
Oat::Adapter
- Object
- Oat::Adapter
- Oat::Adapters::JsonAPI
- Defined in:
- lib/oat/adapters/json_api.rb
Instance Method Summary collapse
- #collection(name, collection, serializer_class = nil, context_options = {}, &block) ⇒ Object
- #entities(name, collection, serializer_class = nil, context_options = {}, &block) ⇒ Object
- #entity(name, obj, serializer_class = nil, context_options = {}, &block) ⇒ Object
-
#initialize(*args) ⇒ JsonAPI
constructor
A new instance of JsonAPI.
- #link(rel, opts = {}) ⇒ Object
- #meta(key, value) ⇒ Object
- #properties(&block) ⇒ Object
- #property(key, value) ⇒ Object
- #to_hash ⇒ Object
- #type(*types) ⇒ Object
Constructor Details
#initialize(*args) ⇒ JsonAPI
Returns a new instance of JsonAPI.
15 16 17 18 19 20 |
# File 'lib/oat/adapters/json_api.rb', line 15 def initialize(*args) super @entities = {} @link_templates = {} @meta = {} end |
Instance Method Details
#collection(name, collection, serializer_class = nil, context_options = {}, &block) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/oat/adapters/json_api.rb', line 93 def collection(name, collection, serializer_class = nil, = {}, &block) @treat_as_resource_collection = true data[:resource_collection] = [] unless data[:resource_collection].is_a?(Array) collection.each do |obj| ent = serializer_from_block_or_class(obj, serializer_class, , &block) data[:resource_collection] << ent.to_hash if ent end end |
#entities(name, collection, serializer_class = nil, context_options = {}, &block) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/oat/adapters/json_api.rb', line 77 def entities(name, collection, serializer_class = nil, = {}, &block) return if collection.nil? || collection.empty? link_name = name.to_s.pluralize.to_sym data[:links][link_name] = [] collection.each do |obj| entity_hash[link_name] ||= [] ent = serializer_from_block_or_class(obj, serializer_class, , &block) if ent ent_hash = ent.to_hash data[:links][link_name] << ent_hash[:id] entity_hash[link_name] << ent_hash end end end |
#entity(name, obj, serializer_class = nil, context_options = {}, &block) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/oat/adapters/json_api.rb', line 67 def entity(name, obj, serializer_class = nil, = {}, &block) ent = serializer_from_block_or_class(obj, serializer_class, , &block) if ent ent_hash = ent.to_hash entity_hash[name.to_s.pluralize.to_sym] ||= [] data[:links][name] = ent_hash[:id] entity_hash[name.to_s.pluralize.to_sym] << ent_hash end end |
#link(rel, opts = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/oat/adapters/json_api.rb', line 26 def link(rel, opts = {}) if opts.is_a?(Hash) templated = opts.delete(:templated) if templated link_template(rel, opts[:href]) else check_link_keys(opts) end end data[:links][rel] = opts end |
#meta(key, value) ⇒ Object
63 64 65 |
# File 'lib/oat/adapters/json_api.rb', line 63 def (key, value) @meta[key] = value end |
#properties(&block) ⇒ Object
55 56 57 |
# File 'lib/oat/adapters/json_api.rb', line 55 def properties(&block) data.merge! yield_props(&block) end |
#property(key, value) ⇒ Object
59 60 61 |
# File 'lib/oat/adapters/json_api.rb', line 59 def property(key, value) data[key] = value end |
#to_hash ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/oat/adapters/json_api.rb', line 103 def to_hash raise "JSON API entities MUST define a type. Use type 'user' in your serializers" unless root_name if serializer.top != serializer return data else h = {} if @treat_as_resource_collection h[root_name] = data[:resource_collection] else h[root_name] = [data] end h[:linked] = @entities if @entities.keys.any? h[:links] = @link_templates if @link_templates.keys.any? h[:meta] = @meta if @meta.keys.any? return h end end |
#type(*types) ⇒ Object
22 23 24 |
# File 'lib/oat/adapters/json_api.rb', line 22 def type(*types) @root_name = types.first.to_s.pluralize.to_sym end |