Class: PVN::Configuration

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



10
11
12
13
# File 'lib/pvn/config.rb', line 10

def initialize
  @@instance = self
  @values = Hash.new { |h, k| h[k] = Array.new }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pvn/config.rb', line 46

def method_missing meth, *args, &blk
  eqre = Regexp.new('^(\w+)=')
  # puts "method missing: #{meth}"
  if md = eqre.match(meth.to_s)
    name = md[1]
    @values[@current] << [ name.to_s, *args ]
    # puts "@values: #{@values}"
  else
    @current = meth.to_s
    yield self
    @current = nil
  end
end

Class Method Details

.config(&blk) ⇒ Object



19
20
21
# File 'lib/pvn/config.rb', line 19

def self.config &blk
  @@instance.config(&blk)
end

.readObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pvn/config.rb', line 23

def self.read
  @@instance ||= begin
    cfg = self.new
    pvndir = ENV['HOME'] + '/.pvn'
    cfgfile = pvndir + '/config'
    
    begin
      require cfgfile
    rescue LoadError => e
      # no configuration
    end
    cfg
  end
end

Instance Method Details

#config(&blk) ⇒ Object



15
16
17
# File 'lib/pvn/config.rb', line 15

def config &blk
  blk.call self
end

#section(name) ⇒ Object



42
43
44
# File 'lib/pvn/config.rb', line 42

def section name
  @values[name]
end

#value(name, field) ⇒ Object



38
39
40
# File 'lib/pvn/config.rb', line 38

def value name, field
  @values[name][field]
end