Class: ConfigSet

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

Constant Summary collapse

VERSION =
'0.1.4'
Index =
Hash.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ConfigSet

Returns a new instance of ConfigSet.



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

def initialize(name)
  @name = name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/config_set.rb', line 29

def method_missing(name, *args, &block)
  case
  when name.to_s =~ /\=/
    instance_variable_set("@#{name.to_s.gsub(/\=/, '')}", args[0])
  when @yielding && (args[0] || block)
    if block
      value = ConfigSet.new(name)
      value.copy_vars(self)
      value.yielding { value.instance_eval &block }
    else
      value = args[0]
    end

    instance_variable_set "@#{name}", value
  when !args[0]
    instance_variable_get "@#{name}"      
  end
end

Class Method Details

.for(name, &block) ⇒ Object



17
18
19
20
21
# File 'lib/config_set.rb', line 17

def self.for(name, &block)
  config = Index[name] ||= ConfigSet.new(name)
  block && config.yielding { config.instance_eval &block }
  config
end

Instance Method Details

#copy_vars(base) ⇒ Object



23
24
25
26
27
# File 'lib/config_set.rb', line 23

def copy_vars(base)
  (base.instance_variables - [:@name, :@yielding]).each do |name|
    instance_variable_set name, base.instance_variable_get(name)
  end
end

#yielding(&block) ⇒ Object



11
12
13
14
15
# File 'lib/config_set.rb', line 11

def yielding(&block)
  @yielding = true
  yield
  @yielding = false
end