Class: Termtter::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/termtter/hook.rb

Direct Known Subclasses

StdOut

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Hook

Returns a new instance of Hook.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
# File 'lib/termtter/hook.rb', line 7

def initialize(args)
  raise ArgumentError, ":name is not given." unless args.has_key?(:name)
  @name = args[:name].to_sym
  @points = args[:points] || [args[:point]].compact
  @exec_proc = args[:exec_proc] || args[:exec] || lambda {}
end

Instance Attribute Details

#exec_procObject

Returns the value of attribute exec_proc.



5
6
7
# File 'lib/termtter/hook.rb', line 5

def exec_proc
  @exec_proc
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/termtter/hook.rb', line 5

def name
  @name
end

#pointsObject

Returns the value of attribute points.



5
6
7
# File 'lib/termtter/hook.rb', line 5

def points
  @points
end

Instance Method Details

#call(*args) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/termtter/hook.rb', line 27

def call(*args)
  begin
    self.exec_proc.call(*args)
  rescue ArgumentError
    args.pop
    retry
  end
end

#match?(point) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/termtter/hook.rb', line 14

def match?(point)
  points.any? {|pt|
    case pt
    when String, Symbol
      pt.to_s == point.to_s
    when Regexp
      (pt =~ point.to_s) ? true : false
    else
      false
    end
  }
end