Module: Hsdq::Setting
- Included in:
- Hsdq
- Defined in:
- lib/hsdq/setting.rb
Overview
This module provide the original setting for the hsdq class as well as some utility methods
Instance Method Summary collapse
-
#channel(name = nil) ⇒ String
The channel based on the class name.
-
#channel=(name) ⇒ String
Force the channel to be set to any name.
-
#config_file_path(config_file_path = nil) ⇒ String
Cached path to the config file.
-
#config_filename(filename = nil) ⇒ String
The name for the config file (cached).
-
#config_path(path = nil) ⇒ String
The path for the config folder, default to config relative the the actual path for a script.
- #cx_opts ⇒ Object
-
#default_opts ⇒ Hash
The default options.
-
#environment(environment = nil) ⇒ String
cached value for the environment based on Rails.env or command line parameter (scripts do not have Rails.env).
- #environment_from_app(environment) ⇒ Object
-
#hsdq_add_options(opts) ⇒ Object
allow to add opions before listening.
-
#hsdq_get_name ⇒ String
Class name.
-
#hsdq_opts(opts = {}) ⇒ Hash
Cached hash of the options thus avoiding to pass options all over the place.
- #initial_setup(opts) ⇒ Object
-
#read_opts(file_path = nil) ⇒ Hash
Read the config file.
-
#set_abort_on_exception(options) ⇒ Object
sets abort_on_exception for debugging based on environment or parameter.
Instance Method Details
#channel(name = nil) ⇒ String
Returns the channel based on the class name.
86 87 88 |
# File 'lib/hsdq/setting.rb', line 86 def channel(name=nil) @channel ||= name || snakify(hsdq_get_name.gsub(/^hsdq/i, "")) end |
#channel=(name) ⇒ String
Force the channel to be set to any name
98 99 100 |
# File 'lib/hsdq/setting.rb', line 98 def channel=(name) @channel = name end |
#config_file_path(config_file_path = nil) ⇒ String
Cached path to the config file
116 117 118 |
# File 'lib/hsdq/setting.rb', line 116 def config_file_path(config_file_path=nil) @config_file_path ||= config_file_path || File.join(config_path, config_filename) end |
#config_filename(filename = nil) ⇒ String
Returns The name for the config file (cached).
103 104 105 |
# File 'lib/hsdq/setting.rb', line 103 def config_filename(filename=nil) @config_filename ||= filename || "#{snakify(hsdq_get_name.gsub(/^hsdq/i, ""))}.yml" end |
#config_path(path = nil) ⇒ String
Returns the path for the config folder, default to config relative the the actual path for a script.
109 110 111 |
# File 'lib/hsdq/setting.rb', line 109 def config_path(path=nil) @config_file_path ||= path || "#{(defined?(Rails) ? Rails.root : '.')}/config/" end |
#cx_opts ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/hsdq/setting.rb', line 39 def cx_opts ||= { message: { host: '127.0.0.1', port: 6379, db: 2 }, admin: { host: '127.0.0.1', port: 6379, db: 2 }, session: { host: '127.0.0.1', port: 6379, db: 2 } }.merge hsdq_opts[:redis] || {} end |
#default_opts ⇒ Hash
Returns the default options.
32 33 34 35 36 37 |
# File 'lib/hsdq/setting.rb', line 32 def default_opts @default_opts ||= { threaded: false, timeout: 10 } end |
#environment(environment = nil) ⇒ String
cached value for the environment based on Rails.env or command line parameter (scripts do not have Rails.env)
76 77 78 |
# File 'lib/hsdq/setting.rb', line 76 def environment(environment=nil) @environment ||= environment_from_app(environment) || 'development' end |
#environment_from_app(environment) ⇒ Object
80 81 82 |
# File 'lib/hsdq/setting.rb', line 80 def environment_from_app(environment) environment || (defined?(Rails) ? Rails.env : nil) || (RAILS_ENV if defined? RAILS_ENV) end |
#hsdq_add_options(opts) ⇒ Object
allow to add opions before listening
17 18 19 20 21 22 23 |
# File 'lib/hsdq/setting.rb', line 17 def (opts) if @hsdq_opts @hsdq_opts.merge!(opts) else hsdq_opts(opts) end end |
#hsdq_get_name ⇒ String
Returns class name.
91 92 93 |
# File 'lib/hsdq/setting.rb', line 91 def hsdq_get_name self.respond_to?(:name) ? self.name : self.class.name end |
#hsdq_opts(opts = {}) ⇒ Hash
Cached hash of the options thus avoiding to pass options all over the place. Initial state read the options from the config file if any provided and merge in it the opts parameter
11 12 13 |
# File 'lib/hsdq/setting.rb', line 11 def hsdq_opts(opts={}) @hsdq_opts ||= initial_setup opts end |
#initial_setup(opts) ⇒ Object
25 26 27 28 29 |
# File 'lib/hsdq/setting.rb', line 25 def initial_setup(opts) = read_opts.merge opts set_abort_on_exception() end |
#read_opts(file_path = nil) ⇒ Hash
Read the config file
62 63 64 65 66 67 68 69 70 |
# File 'lib/hsdq/setting.rb', line 62 def read_opts(file_path=nil) begin default_opts.merge!( deep_symbolize(YAML.load_file(file_path || config_file_path))[environment.to_sym]) rescue Errno::ENOENT => e p "[warning] config file not read, using default options" default_opts end end |
#set_abort_on_exception(options) ⇒ Object
sets abort_on_exception for debugging based on environment or parameter
the main thread will break if a child thread break which is what we want in development/test but we do not want that for production
125 126 127 |
# File 'lib/hsdq/setting.rb', line 125 def set_abort_on_exception() [:exceptions] ? Thread.abort_on_exception = true : Thread.abort_on_exception = false end |