Module: Fatty::Actionable::ClassMethods

Defined in:
lib/fatty/actionable.rb

Instance Method Summary collapse

Instance Method Details

#action(name, on: default_action_target, to: name, doc: nil, &block) ⇒ Object

Define instance method AND register as bindable action.

Examples: action :bol { @cursor = 0 } action :insert { |str| ... } action :backward_char, to: :move_left # alias action name to a method



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fatty/actionable.rb', line 36

def action(name, on: default_action_target, to: name, doc: nil, &block)
  name = name.to_sym
  on   = on.to_sym
  to   = to.to_sym

  # Prefer desc() if present, else doc: kwarg
  doc ||= next_action_doc

  if block
    # define the underlying method (usually same as name)
    define_method(to, &block)
  elsif name != to && !method_defined?(name)
    # If this is an alias (action name differs from method), define the alias method

    # Prefer a real alias if the target method already exists; otherwise define a delegator.
    if method_defined?(to)
      alias_method name, to
    else
      define_method(name) { |*args, &blk| public_send(to, *args, &blk) }
    end
  end
  Fatty::Actions.register(name, owner: self, on: on, method_name: to, doc: doc)
end

#action_on(sym = nil) ⇒ Object

Optional override; by default infer slot from class name (InputBuffer -> :buffer)



11
12
13
# File 'lib/fatty/actionable.rb', line 11

def action_on(sym = nil)
  @default_action_target = sym&.to_sym
end

#default_action_targetObject



15
16
17
# File 'lib/fatty/actionable.rb', line 15

def default_action_target
  @default_action_target || infer_action_target
end

#desc(text) ⇒ Object



19
20
21
# File 'lib/fatty/actionable.rb', line 19

def desc(text)
  @__next_action_doc = text.to_s
end

#next_action_docObject



23
24
25
26
27
# File 'lib/fatty/actionable.rb', line 23

def next_action_doc
  doc = @__next_action_doc
  @__next_action_doc = nil
  doc
end