Class: C11n::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/c11n/configuration.rb

Defined Under Namespace

Classes: NotConfigured

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



11
12
13
14
# File 'lib/c11n/configuration.rb', line 11

def initialize
  @configurations = {}
  @externals = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/c11n/configuration.rb', line 38

def method_missing(method_name, *args, &block)
  if method_name =~ /(.+)=$/
    @configurations[$1.to_sym] = args.first
  elsif value = @configurations[method_name.to_sym]
    value
  else
    super
  end
end

Instance Attribute Details

#configurationsObject (readonly)

Returns the value of attribute configurations.



3
4
5
# File 'lib/c11n/configuration.rb', line 3

def configurations
  @configurations
end

#externalsObject (readonly)

Returns the value of attribute externals.



3
4
5
# File 'lib/c11n/configuration.rb', line 3

def externals
  @externals
end

Class Method Details

.instanceObject



7
8
9
# File 'lib/c11n/configuration.rb', line 7

def self.instance
  @instance ||= new
end

Instance Method Details

#[](key) ⇒ Object



26
27
28
# File 'lib/c11n/configuration.rb', line 26

def [](key)
  @configurations[key]
end

#[]=(key, value) ⇒ Object



30
31
32
# File 'lib/c11n/configuration.rb', line 30

def []=(key, value)
  @configurations[key.to_sym] = value
end

#configure_with_hash(configurations) ⇒ Object



48
49
50
51
52
# File 'lib/c11n/configuration.rb', line 48

def configure_with_hash(configurations)
  configurations.each do |config_key, config_value|
    @configurations[config_key.to_sym] = config_value
  end
end

#external(klass) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/c11n/configuration.rb', line 16

def external(klass)
  if block_given?
    yield(@externals[klass] ||= Configuration.new)

    @externals[klass]
  else
    @externals[klass] || raise(NotConfigured.new)
  end
end

#load_external_configurations(externals) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/c11n/configuration.rb', line 62

def load_external_configurations(externals)
  externals.each do |external_entry, nested_configurations|
    external(external_entry) do |external_config|
      nested_configurations.each do |entry, configuration|
        external_config[entry] = configuration
      end
    end
  end
end

#load_from_hash(configurations) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/c11n/configuration.rb', line 54

def load_from_hash(configurations)
  load_external_configurations(configurations.delete(:external) || {})

  configurations.each do |entry, configuration|
    self[entry] = configuration
  end
end

#to_hashObject



34
35
36
# File 'lib/c11n/configuration.rb', line 34

def to_hash
  @configurations
end