Class: Shift::ActionMap

Inherits:
Object
  • Object
show all
Defined in:
lib/shift/action_map.rb

Overview

A mapping of actions to interfaces. Handles validation, lookup, updates, link walking etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fallback, actions = {}) ⇒ ActionMap

Returns a new instance of ActionMap.



9
10
11
12
13
# File 'lib/shift/action_map.rb', line 9

def initialize(fallback, actions={})
  @actions  = {}
  @fallback = fallback
  map(actions)
end

Instance Attribute Details

#fallbackObject (readonly)

Returns the value of attribute fallback.



15
16
17
# File 'lib/shift/action_map.rb', line 15

def fallback
  @fallback
end

Instance Method Details

#[](action) ⇒ Object

Look up an action



56
57
58
59
60
# File 'lib/shift/action_map.rb', line 56

def [](action)
  action = action.to_sym
  item = @actions[action] || (@fallback[action] if @fallback)
  item.is_a?(Symbol) ? self[item] : item
end

#atoms(inherit = false) ⇒ Object

Return a hash of mappings that are not links



40
41
42
# File 'lib/shift/action_map.rb', line 40

def atoms(inherit=false)
  to_hash(inherit).delete_if {|k,v| v.is_a?(Symbol) }
end

#dupObject



66
67
68
# File 'lib/shift/action_map.rb', line 66

def dup
  self.class.new(fallback ? @fallback.dup : @fallback, @actions)
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/shift/action_map.rb', line 44

def eql?(other)
  (eql_id.eql? other.eql_id) &&
  (@fallback.eql? other.fallback)
end

#eql_idObject



50
51
52
# File 'lib/shift/action_map.rb', line 50

def eql_id
  [to_hash(false), @fallback]
end

#hashObject



53
# File 'lib/shift/action_map.rb', line 53

def hash; eql_id.hash; end

#inspectObject



62
63
64
# File 'lib/shift/action_map.rb', line 62

def inspect
  'ActionMap' + @actions.inspect
end

#localObject

Return a duplicate ActionHash without fallback, to be used for local queries.



19
20
21
# File 'lib/shift/action_map.rb', line 19

def local
  self.class.new(nil).map(@actions)
end

#map(actions) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/shift/action_map.rb', line 23

def map(actions)
  begin
    original = @actions.dup
    parse(actions)
    validate
  rescue Shift::Error
    @actions = original
    raise
  end; self
end

#to_hash(inherit = true) ⇒ Object



34
35
36
37
# File 'lib/shift/action_map.rb', line 34

def to_hash(inherit=true)
  (inherit && @fallback) ?
    @actions.merge(@fallback) : @actions.dup
end