Module: Repp::Handler

Defined in:
lib/repp/handler.rb,
lib/repp/handler/shell.rb,
lib/repp/handler/slack.rb

Defined Under Namespace

Classes: Shell, Slack

Class Method Summary collapse

Class Method Details

.defaultObject



34
35
36
37
38
39
40
41
# File 'lib/repp/handler.rb', line 34

def self.default
  # Guess.
  if  ENV.include?("REPP_HANDLER")
    self.get(ENV["REPP_HANDLER"])
  else
    pick ['shell']
  end
end

.get(service) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/repp/handler.rb', line 4

def get(service)
  return unless service
  service = service.to_s

  unless @handlers[service]
    load_error = try_require('repp/handler', service)
  end

  if klass = @handlers[service]
    klass.split('::').inject(Object) { |o, x| o.const_get(x) }
  else
    const_get(service, false)
  end

rescue => name_error
  raise load_error || name_error
end

.pick(service_names) ⇒ Object

Raises:

  • (LoadError)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/repp/handler.rb', line 22

def self.pick(service_names)
  service_names = Array(service_names)
  service_names.each do |service_name|
    begin
      return get(service_name.to_s)
    rescue LoadError, NameError
    end
  end

  raise LoadError, "Couldn't find handler for: #{service_names.join(', ')}."
end

.register(service, klass) ⇒ Object



53
54
55
56
# File 'lib/repp/handler.rb', line 53

def register(service, klass)
  @handlers ||= {}
  @handlers[service.to_s] = klass.to_s
end

.try_require(prefix, const_name) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/repp/handler.rb', line 43

def self.try_require(prefix, const_name)
  file = const_name.gsub(/^[A-Z]+/) { |pre| pre.downcase }.
    gsub(/[A-Z]+[^A-Z]/, '_\&').downcase

  require(::File.join(prefix, file))
  nil
rescue LoadError => error
  error
end