class Shift::ActionMap
A mapping of actions to interfaces. Handles validation, lookup, updates, link walking etc.
Attributes
fallback[R]
Public Class Methods
new(fallback, actions={})
click to toggle source
# File lib/shift/action_map.rb, line 9 def initialize(fallback, actions={}) @actions = {} @fallback = fallback map(actions) end
Public Instance Methods
[](action)
click to toggle source
Look up an action
# 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)
click to toggle source
Return a hash of mappings that are not links
# File lib/shift/action_map.rb, line 40 def atoms(inherit=false) to_hash(inherit).delete_if {|k,v| v.is_a?(Symbol) } end
dup()
click to toggle source
# File lib/shift/action_map.rb, line 66 def dup self.class.new(fallback ? @fallback.dup : @fallback, @actions) end
eql?(other)
click to toggle source
# File lib/shift/action_map.rb, line 44 def eql?(other) (eql_id.eql? other.eql_id) && (@fallback.eql? other.fallback) end
Also aliased as: ==
eql_id()
click to toggle source
# File lib/shift/action_map.rb, line 50 def eql_id [to_hash(false), @fallback] end
hash()
click to toggle source
# File lib/shift/action_map.rb, line 53 def hash; eql_id.hash; end
inspect()
click to toggle source
# File lib/shift/action_map.rb, line 62 def inspect 'ActionMap' + @actions.inspect end
local()
click to toggle source
Return a duplicate ActionHash without fallback, to be used for local queries.
# File lib/shift/action_map.rb, line 19 def local self.class.new(nil).map(@actions) end
map(actions)
click to toggle source
# 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)
click to toggle source
# File lib/shift/action_map.rb, line 34 def to_hash(inherit=true) (inherit && @fallback) ? @actions.merge(@fallback) : @actions.dup end
Private Instance Methods
cycle_search(action, visited=[])
click to toggle source
# File lib/shift/action_map.rb, line 92 def cycle_search(action, visited=[]) visited << action item = @actions[action] raise(MappingError, "bad link #{action.inspect}") unless item if visited.include?(item) raise MappingError, 'cycle detected ' + visited.inspect end cycle_search(item, visited) if item.is_a?(Symbol) end
parse(hsh)
click to toggle source
# File lib/shift/action_map.rb, line 72 def parse(hsh) hsh = {:default => hsh} unless hsh.is_a?(Hash) hsh.each do |name, handler| h = case handler when Symbol, InterfaceList then handler when Array then handler.map {|cls| cls.to_s } else [handler.to_s] end @actions[name] = h.is_a?(Array) ? InterfaceList.new(h) : h end end
validate()
click to toggle source
# File lib/shift/action_map.rb, line 86 def validate @actions.each do |name, handler| cycle_search(handler) if handler.is_a?(Symbol) end end