Class: Oat::Adapters::Siren

Inherits:
Oat::Adapter show all
Defined in:
lib/oat/adapters/siren.rb

Defined Under Namespace

Classes: Action

Instance Method Summary collapse

Methods inherited from Oat::Adapter

#to_hash

Constructor Details

#initialize(*args) ⇒ Siren


6
7
8
9
10
11
# File 'lib/oat/adapters/siren.rb', line 6

def initialize(*args)
  super
  data[:links] = []
  data[:entities] = []
  data[:actions] = []
end

Instance Method Details

#action(name, &block) ⇒ Object


59
60
61
62
63
64
# File 'lib/oat/adapters/siren.rb', line 59

def action(name, &block)
  action = Action.new(name)
  block.call(action)

  data[:actions] << action.data
end

#entities(name, collection, serializer_class = nil, context_options = {}, &block) ⇒ Object Also known as: collection


51
52
53
54
55
# File 'lib/oat/adapters/siren.rb', line 51

def entities(name, collection, serializer_class = nil, context_options = {}, &block)
  collection.each do |obj|
    entity name, obj, serializer_class, context_options, &block
  end
end

#entity(name, obj, serializer_class = nil, context_options = {}, &block) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/oat/adapters/siren.rb', line 38

def entity(name, obj, serializer_class = nil, context_options = {}, &block)
  ent = serializer_from_block_or_class(obj, serializer_class, context_options, &block)
  if ent
    # use the name as the sub-entities rel to the parent resource.
    ent.rel(name)
    ent_hash = ent.to_hash

    unless data[:entities].include? ent_hash
      data[:entities] << ent_hash
    end
  end
end

24
25
26
# File 'lib/oat/adapters/siren.rb', line 24

def link(rel, opts = {})
  data[:links] << {:rel => [rel].flatten}.merge(opts)
end

#properties(&block) ⇒ Object


28
29
30
# File 'lib/oat/adapters/siren.rb', line 28

def properties(&block)
  data[:properties].merge! yield_props(&block)
end

#property(key, value) ⇒ Object Also known as: meta


32
33
34
# File 'lib/oat/adapters/siren.rb', line 32

def property(key, value)
  data[:properties][key] = value
end

#rel(rels) ⇒ Object

Sub-Entities have a required rel attribute github.com/kevinswiber/siren#rel


15
16
17
18
# File 'lib/oat/adapters/siren.rb', line 15

def rel(rels)
  # rel must be an array.
  data[:rel] = Array(rels)
end

#type(*types) ⇒ Object


20
21
22
# File 'lib/oat/adapters/siren.rb', line 20

def type(*types)
  data[:class] = types
end