Class: Vident::Internals::ActionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/vident/internals/action_builder.rb

Overview

Fluent builder returned by ‘action(…)`. If no chain methods are called, raw args pass through untouched so bare `action :click` still works.

Constant Summary collapse

KWARG_KEYS =
%i[on call_method modifier keyboard window on_controller when].freeze

Instance Method Summary collapse

Constructor Details

#initialize(*args, **meta) ⇒ ActionBuilder

Returns a new instance of ActionBuilder.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vident/internals/action_builder.rb', line 12

def initialize(*args, **meta)
  @args = args
  unknown = meta.keys - KWARG_KEYS
  raise ArgumentError, "action: unknown option(s) #{unknown.inspect}. Valid: #{KWARG_KEYS.inspect}" unless unknown.empty?

  @event = meta[:on]
  @method_name = meta[:call_method]
  @modifiers = meta.key?(:modifier) ? Array(meta[:modifier]) : nil
  @keyboard = meta[:keyboard]
  @window = meta.fetch(:window, false)
  @controller_ref = meta[:on_controller]
  @when_proc = meta[:when]
  @touched = !meta.empty?
end

Instance Method Details

#call_method(name) ⇒ Object



33
34
35
36
37
# File 'lib/vident/internals/action_builder.rb', line 33

def call_method(name)
  @method_name = name
  @touched = true
  self
end

#keyboard(str) ⇒ Object



45
46
47
48
49
# File 'lib/vident/internals/action_builder.rb', line 45

def keyboard(str)
  @keyboard = str
  @touched = true
  self
end

#modifier(*mods) ⇒ Object



39
40
41
42
43
# File 'lib/vident/internals/action_builder.rb', line 39

def modifier(*mods)
  (@modifiers ||= []).concat(mods)
  @touched = true
  self
end

#on(event) ⇒ Object



27
28
29
30
31
# File 'lib/vident/internals/action_builder.rb', line 27

def on(event)
  @event = event
  @touched = true
  self
end

#on_controller(ref) ⇒ Object



57
58
59
60
61
# File 'lib/vident/internals/action_builder.rb', line 57

def on_controller(ref)
  @controller_ref = ref
  @touched = true
  self
end

#to_declarationObject



68
69
70
71
# File 'lib/vident/internals/action_builder.rb', line 68

def to_declaration
  return Declaration.new(args: @args.freeze, when_proc: @when_proc, meta: {}.freeze) unless @touched
  Declaration.new(args: [build_descriptor].freeze, when_proc: @when_proc, meta: {}.freeze)
end

#when(callable = nil, &block) ⇒ Object



63
64
65
66
# File 'lib/vident/internals/action_builder.rb', line 63

def when(callable = nil, &block)
  @when_proc = block || callable
  self
end

#windowObject



51
52
53
54
55
# File 'lib/vident/internals/action_builder.rb', line 51

def window
  @window = true
  @touched = true
  self
end