Class: Carnivore::Config

Inherits:
Object
  • Object
show all
Extended by:
Mixlib::Config
Defined in:
lib/carnivore/config.rb

Overview

Configuration helper

Class Method Summary (collapse)

Class Method Details

+ (TrueClass, FalseClass) auto_symbolize(v = nil)

Set/get automatic symbolization of hash keys

v

Boolean value

Parameters:

  • v (Object) (defaults to: nil)

    truthy or falsey value

Returns:

  • (TrueClass, FalseClass)


18
19
20
21
22
23
# File 'lib/carnivore/config.rb', line 18

def auto_symbolize(v=nil)
  unless(v.nil?)
    @hash_symbolizer = !!v
  end
  @hash_symbolizer.nil? ? false : @hash_symbolizer
end

+ (self) build(path_or_hash)

Populates the configuration

Parameters:

  • path_or_hash (String, Hash)

    Path to JSON file or configuration hash

Returns:

  • (self)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/carnivore/config.rb', line 39

def build(path_or_hash)
  if(path_or_hash.is_a?(Hash))
    conf = path_or_hash
  else
    if(File.exists?(path_or_hash.to_s))
      conf = JSON.load(File.read(path_or_hash))
      self.config_path = path_or_hash
    else
      raise "Failed to load configuration file: #{path_or_hash}"
    end
  end
  conf.each do |k,v|
    self.send(k, v)
  end
  self
end

+ (self) configure(args)

Merge provided args into configuration

Parameters:

Returns:

  • (self)


29
30
31
32
33
# File 'lib/carnivore/config.rb', line 29

def configure(args)
  build(args[:config_path]) if args[:config_path]
  self.merge!(args)
  self
end

+ (Object) get(*ary)

Fetch value from configuration

Examples:

Config.build(:my_app => {:port => 30})
Config.get(:my_app, :port) => 30
Config.get(:my_app, :host) => nil
Config.get(:other_app, :port) => nil
Config.get(:my_app, :mail, :server) => nil

Parameters:

  • ary (String, Symbol)

    list of strings or symbols as hash path

Returns:

  • (Object)

    return value or nil



66
67
68
69
70
71
72
73
# File 'lib/carnivore/config.rb', line 66

def get(*ary)
  value = Carnivore::Utils.retrieve(self, *ary)
  if(value.is_a?(Hash) && auto_symbolize)
    Smash.new(value)
  else
    value
  end
end