Class: Comodule::ConfigSupport::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/comodule/config_support.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_hash = {}) ⇒ Config

Returns a new instance of Config.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/comodule/config_support.rb', line 31

def initialize(config_hash={})
  config_hash[:configure_type] ||= :soft

  config_hash.each do |directive, value|
    value = value.to_sym if directive == :configure_type

    if Hash === value
      value[:configure_type] ||= config_hash[:configure_type]
      value = self.class.new(value)
    end

    set_directive directive, value
  end

  if block_given?
    yield self
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(directive, arg = nil) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/comodule/config_support.rb', line 50

def method_missing(directive, arg=nil)
  if directive =~ /^(.+)=/
    set_directive($1, arg)
  else
    get_directive(directive)
  end
end

Class Method Details

.combine(config1, config2) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/comodule/config_support.rb', line 12

def combine(config1, config2)
  new_obj = new

  config1.each do |key, value|
    new_obj[key] = value
  end

  config2.each do |key, value|
    if self === new_obj[key] && self === value
      new_obj[key] = combine(new_obj[key], value)
    else
      new_obj[key] = value
    end
  end

  new_obj
end

.original_methodsObject



4
5
6
7
8
9
10
# File 'lib/comodule/config_support.rb', line 4

def original_methods
  return @original_methods if @original_methods

  @original_methods =  public_instance_methods
  @original_methods += protected_instance_methods
  @original_methods += private_instance_methods
end

Instance Method Details

#[](directive) ⇒ Object



58
59
60
# File 'lib/comodule/config_support.rb', line 58

def [](directive)
  get_directive(directive)
end

#[]=(directive, arg) ⇒ Object



62
63
64
# File 'lib/comodule/config_support.rb', line 62

def []=(directive, arg)
  set_directive(directive, arg)
end

#eachObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/comodule/config_support.rb', line 72

def each
  instance_variables.each do |variable_name|
    next if variable_name == :@configure_type

    key = variable_name.to_s.sub(/@/, '').to_sym
    value = instance_variable_get(variable_name)

    yield key, value
  end
end

#merge(other_config) ⇒ Object Also known as: +



66
67
68
# File 'lib/comodule/config_support.rb', line 66

def merge(other_config)
  self.class.combine(self, other_config)
end

#slice(*keys) ⇒ Object



92
93
94
95
96
97
# File 'lib/comodule/config_support.rb', line 92

def slice(*keys)
  keys.inject(self.class.new) do |new_obj, key|
    new_obj[key] = self[key] if self[key]
    new_obj
  end
end

#to_hashObject



83
84
85
86
87
88
89
90
# File 'lib/comodule/config_support.rb', line 83

def to_hash
  hsh = {}
  each do |key, value|
    value = value.to_hash if self.class === value
    hsh[key] = value
  end
  hsh
end