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

Instance Method Details

#channel(name = nil) ⇒ String

Returns the channel based on the class name.

Parameters:

  • name (String) (defaults to: nil)

    the HsdqClassName

Returns:

  • (String)

    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

Parameters:

  • name (String)

Returns:

  • (String)

    the new channel 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

Parameters:

  • config_file_path (String) (defaults to: nil)

    if passed force the value

Returns:

  • (String)

    The value for the 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).

Returns:

  • (String)

    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.

Parameters:

  • path (String) (defaults to: nil)

    or nil or nothing. Force the path of a value is passed (cahed)

Returns:

  • (String)

    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_optsObject



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
  @cx_options ||= {
    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_optsHash

Returns the default options.

Returns:

  • (Hash)

    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)

Parameters:

  • environment (String) (defaults to: nil)

    the environment to be force set or nil or nothing

Returns:

  • (String)

    The environment string



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 hsdq_add_options(opts)
  if @hsdq_opts
    @hsdq_opts.merge!(opts)
  else
    hsdq_opts(opts)
  end
end

#hsdq_get_nameString

Returns class name.

Returns:

  • (String)

    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

Parameters:

  • opts (Hash) (defaults to: {})

    The Options to be added/merged into the options from the config file

Returns:

  • (Hash)

    of the options



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)
  options = read_opts.merge opts
  set_abort_on_exception(options)
  options
end

#read_opts(file_path = nil) ⇒ Hash

Read the config file

Parameters:

  • file_path (String) (defaults to: nil)

Returns:

  • (Hash)

    options from defult and config



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

Parameters:

  • options (Hash)

    If options[exception] true,



125
126
127
# File 'lib/hsdq/setting.rb', line 125

def set_abort_on_exception(options)
  options[:exceptions] ? Thread.abort_on_exception = true : Thread.abort_on_exception = false
end