Class: Skein::Config

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/skein/config.rb

Constant Summary collapse

CONFIG_PATH_DEFAULT =

Constants ============================================================

'config/skein.yml'.freeze
ENV_DEFAULT =
'development'.freeze
DRIVERS =
{
  bunny: 'Bunny',
  march_hare: 'MarchHare'
}.freeze
DRIVER_PLATFORM_DEFAULT =
Hash.new(:bunny).merge(
  "java" => :march_hare
).freeze
DRIVER_DEFAULT =
(
  DRIVERS.find do |name, const|
    const_defined?(const)
  end || [ ]
)[0]
DEFAULTS =
{
  host: '127.0.0.1',
  port: 5672,
  username: 'guest',
  password: 'guest',
  driver: DRIVER_DEFAULT,
  namespace: nil
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Config

Instance Methods =====================================================



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/skein/config.rb', line 63

def initialize(options = nil)
  config_path = nil

  case (options)
  when String
    if (File.exist?(options))
      config_path = options
    end
  when Hash
    super(
      DEFAULTS.merge(
        Hash[
          options.map do |k, v|
            [ k.nil? ? nil : k.to_sym, v ]
          end
        ]
      )
    )

    return
  when false, :default
    # Ignore configuration file, use defaults
  else
    config_path = File.expand_path('config/skein.yml', self.class.root)
  end

  if (config_path and File.exist?(config_path))
    super(DEFAULTS.merge(
      YAML.load_file(config_path, aliases: true)[self.class.env] || { }
    ))
  else
    super(DEFAULTS)
  end
end

Class Method Details

.envObject



45
46
47
48
49
50
51
# File 'lib/skein/config.rb', line 45

def self.env
  if (defined?(Rails))
    Rails.env.to_s
  else
    ENV['RAILS_ENV'] || ENV_DEFAULT
  end
end

.exist?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/skein/config.rb', line 57

def self.exist?
  File.exist?(self.path)
end

.pathObject



53
54
55
# File 'lib/skein/config.rb', line 53

def self.path
  File.expand_path(CONFIG_PATH_DEFAULT, self.root)
end

.rootObject

Class Methods ========================================================



37
38
39
40
41
42
43
# File 'lib/skein/config.rb', line 37

def self.root
  if (defined?(Rails))
    Rails.root
  else
    Dir.pwd
  end
end