Class: RabbitJobs::Configuration

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/rabbit_jobs/configuration.rb

Constant Summary collapse

DEFAULT_QUEUE_PARAMS =
{
  auto_delete: false,
  durable: true,
  ack: true
}
DEFAULT_EXCHANGE_PARAMS =
{
  auto_delete: false,
  durable: true
}
DEFAULT_MESSAGE_PARAMS =
{
  persistent: true,
  nowait: false,
  immediate: false
}

Instance Method Summary collapse

Methods included from Helpers

#constantize, #symbolize_keys!

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



50
51
52
53
54
55
56
57
# File 'lib/rabbit_jobs/configuration.rb', line 50

def initialize
  @data = {
    host: 'localhost',
    exchange: 'rabbit_jobs',
    exchange_params: DEFAULT_EXCHANGE_PARAMS,
    queues: {}
  }
end

Instance Method Details

#[](name) ⇒ Object



59
60
61
# File 'lib/rabbit_jobs/configuration.rb', line 59

def [](name)
  @data[name]
end

#convert_yaml_config(yaml) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rabbit_jobs/configuration.rb', line 111

def convert_yaml_config(yaml)
  if yaml['rabbit_jobs']
    convert_yaml_config(yaml['rabbit_jobs'])
  else
    @data = {host: nil, exchange: nil, queues: {}}
    host yaml['host']
    exchange yaml['exchange'], symbolize_keys!(yaml['exchange_params'])
    yaml['queues'].each do |name, params|
      queue name, symbolize_keys!(params) || {}
    end
  end
end

#exchange(value = nil, params = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/rabbit_jobs/configuration.rb', line 72

def exchange(value = nil, params = {})
  if value
    raise ArgumentError unless value.is_a?(String) && value != ""
    @data[:exchange] = value.downcase
    @data[:exchange_params] = DEFAULT_EXCHANGE_PARAMS.merge(params)
  else
    @data[:exchange]
  end
end

#host(value = nil) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/rabbit_jobs/configuration.rb', line 63

def host(value = nil)
  if value
    raise ArgumentError unless value.is_a?(String) && value != ""
    @data[:host] = value.to_s
  else
    @data[:host]
  end
end

#load_file(filename) ⇒ Object



103
104
105
# File 'lib/rabbit_jobs/configuration.rb', line 103

def load_file(filename)
  load_yaml(File.read(filename))
end

#load_yaml(text) ⇒ Object



107
108
109
# File 'lib/rabbit_jobs/configuration.rb', line 107

def load_yaml(text)
  convert_yaml_config(YAML.load(text))
end

#queue(name, params = {}) ⇒ Object

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rabbit_jobs/configuration.rb', line 82

def queue(name, params = {})
  raise ArgumentError.new("name is #{name.inspect}") unless name && name.is_a?(String) && name != ""
  raise ArgumentError.new("params is #{params.inspect}") unless params && params.is_a?(Hash)

  name = name.downcase

  if @data[:queues][name]
    @data[:queues][name].merge!(params)
  else
    @data[:queues][name] = DEFAULT_QUEUE_PARAMS.merge(params)
  end
end

#queue_name(routing_key) ⇒ Object



99
100
101
# File 'lib/rabbit_jobs/configuration.rb', line 99

def queue_name(routing_key)
  [@data[:exchange], routing_key].join('#')
end

#routing_keysObject



95
96
97
# File 'lib/rabbit_jobs/configuration.rb', line 95

def routing_keys
  @data[:queues].keys
end

#to_hashObject



46
47
48
# File 'lib/rabbit_jobs/configuration.rb', line 46

def to_hash
  @data.dup
end