Class: VER::Mode

Inherits:
Object
  • Object
show all
Defined in:
lib/ver/mode.rb

Constant Summary collapse

MERGER =
proc{|key, v1, v2|
  if v1.respond_to?(:merge) && v2.respond_to?(:merge)
    v1.merge(v2, &MERGER)
  else
    v2
  end
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, callback) ⇒ Mode

Returns a new instance of Mode.



17
18
19
20
21
22
23
24
# File 'lib/ver/mode.rb', line 17

def initialize(name, callback)
  @name, @callback = name, callback
  @stack = []
  @map = {}
  @ancestors = []
  @missing = nil
  @arguments = true
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



11
12
13
# File 'lib/ver/mode.rb', line 11

def arguments
  @arguments
end

#callbackObject

Returns the value of attribute callback.



11
12
13
# File 'lib/ver/mode.rb', line 11

def callback
  @callback
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/ver/mode.rb', line 11

def name
  @name
end

Class Method Details

.split_stack(stack) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/ver/mode.rb', line 190

def self.split_stack(stack)
  first = stack[0]
  return stack, nil if first == '0' || first == '<KeyPress-0>'

  pivot = stack.index{|c| c !~ /^(<KeyPress-\d+>|\d+)$/ }

  if pivot == 0
    return stack, nil
  elsif pivot
    keys, args = stack[pivot..-1], stack[0..pivot]
    return keys, args.join.scan(/\d+/).join.to_i
  else
    return [], stack.join.to_i
  end
end

Instance Method Details

#ancestors(*done) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (VER::Mode)

    the object that the method was called on



42
43
44
45
46
47
48
49
50
# File 'lib/ver/mode.rb', line 42

def ancestors(*done, &block)
  yield self

  @ancestors.each do |ancestor|
    next if done.include?(ancestor)
    yield ancestor
    ancestor.ancestors(done + [self], &block)
  end
end

#attempt_execute(original_stack, lookup = false) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ver/mode.rb', line 123

def attempt_execute(original_stack, lookup = false)
  if arguments
    stack, arg = Mode.split_stack(original_stack)
  else
    stack, arg = original_stack, nil
  end

  if stack.empty?
    arg ? nil : false
  else
    executable = @map
    while key = stack.shift
      previous = executable
      executable = executable[key]

      case executable
      when nil
        # FIXME: this allows only one mode
        if found = previous.find{|prev_key, prev_value| prev_key.is_a?(Mode) }
          mode, action = found
          looked = mode.attempt_execute([key, *stack], true)

          case looked
          when false
            return false
          when nil
            return nil
          else
            cmd, cmd_arg = looked
            return nil if cmd.is_a?(Hash)
            return execute(action, cmd, arg)
          end
        else
          return false
        end
      end
    end

    if lookup
      return executable, arg
    else
      execute(executable, *arg)
    end
  end
end

#bind(keychain, action_name = nil, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ver/mode.rb', line 63

def bind(keychain, action_name = nil, &block)
  keychain = keychain.dup
  total = hash = {}

  while key = keychain.shift
    if key.is_a?(Symbol)
      canonical = callback.modes[key]
    else
      canonical = register(key)
    end

    if keychain.empty?
      hash[canonical] = block || action_name
    else
      hash = hash[canonical] = {}
    end
  end

  @map.replace @map.merge(total, &MERGER)
end

#enter_key(key) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ver/mode.rb', line 92

def enter_key(key)
  @stack << key

  ancestors do |ancestor|
    result = ancestor.attempt_execute(@stack.dup)

    case result
    when nil # nothing matched yet, but possible in future
      return nil
    when false # nothing possible
      # try next one
    when true # executed
      @stack.clear
      return true
    else
      raise "%p is not a valid result" % [result]
    end
  end

  # no ancestors or all failed
  @stack.clear
  enter_missing(key)
rescue => ex
  VER.error(ex)
  @stack.clear
end

#enter_keys(*keys) ⇒ Object



88
89
90
# File 'lib/ver/mode.rb', line 88

def enter_keys(*keys)
  keys.flatten.each{|key| enter_key(key) }
end

#enter_missing(key) ⇒ Object



119
120
121
# File 'lib/ver/mode.rb', line 119

def enter_missing(key)
  execute(@missing, key) if @missing
end

#execute(executable, *arg) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ver/mode.rb', line 169

def execute(executable, *arg)
  arg = [*arg].compact # doesn't allow nil
  case executable
  when Hash
    return nil
  when Symbol
    callback.send(executable, *arg)
  when Array
    callback.send(*executable, *arg)
  when Proc
    executable.call(*arg)
  else
    return false
  end

  true
rescue ArgumentError => ex
  callback.message("#{executable} : #{ex}")
  true
end

#find_ancestor(name) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/ver/mode.rb', line 34

def find_ancestor(name)
  if found = callback.modes[name.to_sym]
    return found
  else
    raise "Mode #{name} is not specified yet"
  end
end

#inherits(*others) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/ver/mode.rb', line 26

def inherits(*others)
  others.flatten.each do |other|
    ancestor = find_ancestor(other.to_sym)
    @ancestors.delete ancestor
    @ancestors.unshift ancestor
  end
end

#inspectObject



13
14
15
# File 'lib/ver/mode.rb', line 13

def inspect
  "#<Mode #@name>"
end

#map(sym, *keychains) ⇒ Object Also known as: to



56
57
58
59
60
# File 'lib/ver/mode.rb', line 56

def map(sym, *keychains)
  keychains.each do |keychain|
    bind(keychain.flatten, sym)
  end
end

#missing(sym) ⇒ Object



52
53
54
# File 'lib/ver/mode.rb', line 52

def missing(sym)
  @missing = sym
end

#register(key) ⇒ Object



84
85
86
# File 'lib/ver/mode.rb', line 84

def register(key)
  callback.register(key)
end