Class: RKit::Core::Configurer

Inherits:
Object
  • Object
show all
Defined in:
lib/r_kit/core/configurer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, config = {}) ⇒ Configurer

Returns a new instance of Configurer.



4
5
6
7
8
9
# File 'lib/r_kit/core/configurer.rb', line 4

def initialize base, config = {}
  @_base = base
  @_config = config
  @_presets = Hash.new{ |hash, key| hash[key] = self.class.new(_base) }
  @_aliases = []
end

Instance Attribute Details

#_aliasesObject

Returns the value of attribute _aliases.



2
3
4
# File 'lib/r_kit/core/configurer.rb', line 2

def _aliases
  @_aliases
end

#_baseObject

Returns the value of attribute _base.



2
3
4
# File 'lib/r_kit/core/configurer.rb', line 2

def _base
  @_base
end

#_configObject

Returns the value of attribute _config.



2
3
4
# File 'lib/r_kit/core/configurer.rb', line 2

def _config
  @_config
end

#_presetsObject

Returns the value of attribute _presets.



2
3
4
# File 'lib/r_kit/core/configurer.rb', line 2

def _presets
  @_presets
end

Instance Method Details

#alias(*name, old_name) ⇒ Object



34
35
36
37
38
39
# File 'lib/r_kit/core/configurer.rb', line 34

def alias *name, old_name
  new_name = name.pop
  config = name.inject(_config){ |config, nested| config = config[nested] }

  config[new_name] = config[old_name] if config[new_name].blank?
end

#alias_config(*name, old_name) ⇒ Object



30
31
32
# File 'lib/r_kit/core/configurer.rb', line 30

def alias_config *name, old_name
  _aliases << [name, old_name]
end

#config(*name, default) ⇒ Object



21
22
23
# File 'lib/r_kit/core/configurer.rb', line 21

def config *name, default
  _config.deep_merge! [*name, default].reverse.inject{ |nested, key| Hash[key, nested] }
end

#fingerprintObject



66
67
68
# File 'lib/r_kit/core/configurer.rb', line 66

def fingerprint
  _config.sort.join
end

#inspectObject



62
63
64
# File 'lib/r_kit/core/configurer.rb', line 62

def inspect
  _config.inspect
end

#load!(config) ⇒ Object



54
55
56
57
58
59
# File 'lib/r_kit/core/configurer.rb', line 54

def load! config
  load_preset! config.delete(:preset)
  load_config! config
  load_alias!
  load_public_accessor!
end

#load_alias!Object



41
42
43
44
45
# File 'lib/r_kit/core/configurer.rb', line 41

def load_alias!
  _aliases.each do |(name, old_name)|
    self.alias *name, old_name
  end
end

#load_config!(config_options) ⇒ Object



25
26
27
# File 'lib/r_kit/core/configurer.rb', line 25

def load_config! config_options
  _config.deep_merge! config_options
end

#load_preset!(name) ⇒ Object



16
17
18
# File 'lib/r_kit/core/configurer.rb', line 16

def load_preset! name
  _config.deep_merge! _presets[name]._config
end

#load_public_accessor!Object



48
49
50
51
# File 'lib/r_kit/core/configurer.rb', line 48

def load_public_accessor!
  _base.const_set :CONFIG, OpenStruct.new(_config)
  _base.define_singleton_method("config"){ self::CONFIG }
end

#preset(name, config) ⇒ Object



12
13
14
# File 'lib/r_kit/core/configurer.rb', line 12

def preset name, config
  _presets[name] = self.class.new(_base, config)
end