Class: Figi::Config

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/figi/config.rb

Overview

Core class

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Constructor



62
63
64
# File 'lib/figi/config.rb', line 62

def initialize
  @table = Hashie::Mash.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mid, *args) ⇒ Object

Dispatch to Hashie::Mash



81
82
83
# File 'lib/figi/config.rb', line 81

def method_missing(mid, *args)
  @table.send(mid, *args)
end

Class Method Details

.from_json(path) ⇒ Object

Load config from json file

Examples:

Figi::Config.from_json('config/config.json')

Parameters:

  • path (String)

    File path



28
29
30
# File 'lib/figi/config.rb', line 28

def from_json(path)
  instance._figi_load(JSON.parse(File.read(path)))
end

.from_yaml(path) ⇒ Object

Load config from yaml file

Examples:

Figi::Config.from_yaml('config/config.yml')

Parameters:

  • path (String)

    File path



37
38
39
# File 'lib/figi/config.rb', line 37

def from_yaml(path)
  instance._figi_load(YAML.safe_load(File.read(path)))
end

.load(config = {}, &block) ⇒ Object

Load config from hash

Examples:

Figi::Config.load(host: 'localhost', port: '27017')
figi.host
# => localhost

Figi::Config.load do |config|
  config.host = 'localhost'
  config.port = '27017'
end
figi.host
# => localhost

Parameters:

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

    Config



55
56
57
# File 'lib/figi/config.rb', line 55

def load(config = {}, &block)
  instance._figi_load(config, &block)
end

Instance Method Details

#_figi_load(config = {}) {|@table| ... } ⇒ Object

Load config from hash, don’t use this directly

Parameters:

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

    Config

Yields:

  • (@table)


69
70
71
72
73
74
75
76
77
# File 'lib/figi/config.rb', line 69

def _figi_load(config = {})
  if @table.nil?
    @table = Hashie::Mash.new(config)
  else
    @table.update(config)
  end

  yield @table if block_given?
end