Class: Sawyer::Relation::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/sawyer/relation.rb

Instance Method Summary collapse

Constructor Details

#initializeMap

Tracks the available next actions for a resource, and issues requests for them.



6
7
8
# File 'lib/sawyer/relation.rb', line 6

def initialize
  @map = {}
end

Instance Method Details

#<<(rel) ⇒ Object

Adds a Relation to the map.

rel - A Relation.

Returns nothing.



15
16
17
# File 'lib/sawyer/relation.rb', line 15

def <<(rel)
  @map[rel.name] = rel if rel
end

#[](key) ⇒ Object

Gets the raw Relation by its name.

key - The Symbol name of the Relation.

Returns a Relation.



24
25
26
# File 'lib/sawyer/relation.rb', line 24

def [](key)
  @map[key.to_sym]
end

#inspectObject



50
51
52
53
# File 'lib/sawyer/relation.rb', line 50

def inspect
  hash = to_hash
  hash.respond_to?(:pretty_inspect) ? hash.pretty_inspect : hash.inspect
end

#keysObject

Gets a list of the Relation names.

Returns an Array of Symbols in no specific order.



38
39
40
# File 'lib/sawyer/relation.rb', line 38

def keys
  @map.keys
end

#sizeObject

Gets the number of mapped Relations.

Returns an Integer.



31
32
33
# File 'lib/sawyer/relation.rb', line 31

def size
  @map.size
end

#to_hashObject Also known as: to_h



42
43
44
45
46
47
# File 'lib/sawyer/relation.rb', line 42

def to_hash
  pairs = @map.map do |k, v|
    [(k.to_s + "_url").to_sym, v.href]
  end
  Hash[pairs]
end