Class: Configureasy::Config

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

Overview

Store data structure (Hash) and allow access values using method calling.

Confugureasy::Config.new(foo: 'foo').foo
# => 'foo'

Configureasy::Config.new(:foo => {:bar => :value}).foo.bar
# => :value

You can access values using [] too

Configureasy::Config.new(foo: 'bar')[:foo]
# => 'bar'

Beside you retrive data as hash

Configureasy::Config(foo: 'bar').as_hash
# => {:foo => 'bar'}

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Config

Convert a hash data into methods recursively. Params:

[+params+]:: Hash with data do convert

Returns new instance of [Configureasy::Config].



27
28
29
30
31
32
33
34
35
# File 'lib/configureasy/config.rb', line 27

def initialize(params = {})
  @hash = params
  params = params.inject({}) do |hash, data|
    key, value = data
    value = self.class.new(value) if value.is_a? Hash
    hash.merge key.to_sym => value
  end
  super params
end

Instance Method Details

#[](key) ⇒ Object

Retrive some config value. Params:

[+key+]:: name of config

  Configureasy::Config.new(foo: 'bar')[:foo]
  # => 'bar'

Returns config value, or nil if can’t reach config value.



45
46
47
# File 'lib/configureasy/config.rb', line 45

def [](key)
  self.send key
end

#as_hashObject

Returns config as hash.



50
51
52
# File 'lib/configureasy/config.rb', line 50

def as_hash
  @hash
end