Class: ProcessBot::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/process_bot/options.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Options

Returns a new instance of Options.



14
15
16
# File 'lib/process_bot/options.rb', line 14

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/process_bot/options.rb', line 2

def options
  @options
end

Class Method Details

.from_args(args) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/process_bot/options.rb', line 4

def self.from_args(args)
  options = ProcessBot::Options.new

  args.each do |key, value|
    options.set(key, value)
  end

  options
end

Instance Method Details

#[](key) ⇒ Object



18
19
20
# File 'lib/process_bot/options.rb', line 18

def [](key)
  options[key]
end

#application_basenameObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/process_bot/options.rb', line 37

def application_basename
  @application_basename ||= begin
    app_path_parts = release_path.split("/")

    if release_path.include?("/releases/")
      app_path_parts.pop(2)
    elsif release_path.end_with?("/current")
      app_path_parts.pop
    end

    app_path_parts.last
  end
end

#eventsObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/process_bot/options.rb', line 22

def events
  @events ||= begin
    require "knjrbfw"

    event_handler = ::Knj::Event_handler.new
    event_handler.add_event(name: :on_process_started)
    event_handler.add_event(name: :on_socket_opened)
    event_handler
  end
end

#fetchObject



33
34
35
# File 'lib/process_bot/options.rb', line 33

def fetch(...)
  options.fetch(...)
end

#possible_process_titlesObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/process_bot/options.rb', line 51

def possible_process_titles
  possible_names = []

  # Sidekiq name can by current Rails root base name
  possible_names << application_basename

  # Sidekiq name can be set tag name (but we wrongly read application for some reason?)
  possible_names << options.fetch(:application)

  possible_names
end

#possible_process_titles_joined_regexObject



63
64
65
66
67
68
69
70
71
# File 'lib/process_bot/options.rb', line 63

def possible_process_titles_joined_regex
  possible_process_titles_joined_regex = ""
  possible_process_titles.each_with_index do |possible_name, index|
    possible_process_titles_joined_regex << "|" if index >= 1
    possible_process_titles_joined_regex << Regexp.escape(possible_name)
  end

  possible_process_titles_joined_regex
end

#present?(key) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/process_bot/options.rb', line 73

def present?(key)
  return true if options.key?(key) && options[key]

  false
end

#release_pathObject



79
80
81
# File 'lib/process_bot/options.rb', line 79

def release_path
  @release_path ||= fetch(:release_path)
end

#set(key, value) ⇒ Object



83
84
85
# File 'lib/process_bot/options.rb', line 83

def set(key, value)
  options[key] = value
end