Class: Basher::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/basher/handler.rb

Constant Summary collapse

ALPHABET =
('a'..'z').to_a.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_bindings = {}) ⇒ Handler

Returns a new instance of Handler.



19
20
21
# File 'lib/basher/handler.rb', line 19

def initialize(custom_bindings = {})
  @bindings = self.class.bindings.merge(custom_bindings)
end

Class Attribute Details

.bindingsObject (readonly)

Returns the value of attribute bindings.



6
7
8
# File 'lib/basher/handler.rb', line 6

def bindings
  @bindings
end

Instance Attribute Details

#bindingsObject (readonly)

Returns the value of attribute bindings.



17
18
19
# File 'lib/basher/handler.rb', line 17

def bindings
  @bindings
end

Class Method Details

.bind(*keys, &action) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/basher/handler.rb', line 8

def bind(*keys, &action)
  @bindings ||= {}
  keys.map(&:to_sym).each do |input|
    @bindings[input] ||= []
    @bindings[input] << action
  end
end

Instance Method Details

#invoke(input) ⇒ Object



23
24
25
26
27
# File 'lib/basher/handler.rb', line 23

def invoke(input)
  bindings.fetch(input.to_sym, []).map do |b|
    b.call(input)
  end.last
end

#letter?(input) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/basher/handler.rb', line 29

def letter?(input)
  return false if input.size != 1
  input =~ /[[:alpha:]]/
end