Module: Conf::ConfigValue

Included in:
Configuration
Defined in:
lib/conf.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/conf.rb', line 30

def method_missing(meth, *args, &blk)
  m = meth.to_s
  
  if m =~ /^to_/
    super
  elsif m =~ /^(\w+)=/ || args.size == 1
    # setter
    @__root__.check_lock
    key = [@__key__, $1 || m].compact.join(".")
    @__root__[key] = ConfigValue.create(@__root__, key, args.first)
  else
    # getter
    key = [@__key__, m].compact.join(".")

    obj = @__root__.data[key]

    if obj.nil?
      if @__root__.locked?
        obj = @__root__.fetch(key) { raise Conf::InvalidKeyError, key }
      else
        obj = @__root__.data[key] = ConfigValue.create(@__root__, key)
      end
    end

    if blk
      @__root__.check_lock
      obj.instance_eval(&blk)
    end

    obj
  end
end

Class Method Details

.create(root, key, obj = Object.new) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/conf.rb', line 10

def self.create(root, key, obj = Object.new)
  return obj unless [String, Object].any? { |klass| obj.instance_of? klass }
  
  begin
    obj.extend(self)
    obj.__setup__(root, key)
  rescue TypeError
    # can't extend obj
  end

  obj
end

Instance Method Details

#__setup__(root, key) ⇒ Object



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

def __setup__(root, key)
  @__root__ = root
  @__key__  = key

  self
end