Class: Dspace::Config

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



5
6
7
# File 'lib/dspace/config.rb', line 5

def initialize
  self.opts = {}
end

Instance Attribute Details

#optsObject

Returns the value of attribute opts.



3
4
5
# File 'lib/dspace/config.rb', line 3

def opts
  @opts
end

Class Method Details

.load(path) ⇒ Object

def find



23
24
25
26
27
28
29
# File 'lib/dspace/config.rb', line 23

def self.load(path)
  if File.exist?(path)
    Config.parse File.read(path)
  else
    raise "Configuration file does not exist.  #{path}"
  end
end

.parse(config) ⇒ Object



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
# File 'lib/dspace/config.rb', line 31

def self.parse(config)
  retval = Config.new
  
  line_stack = []
  config.each do |line|
    line.sub!(/\#(.+?)$/, '') # ignore comments
    
    # join multi-line statements
    if line.strip =~ /\\$/
      line_stack.push line.sub(/\\/, '').strip
      next
    elsif line_stack.length != 0
      line = (line_stack + [line]).join(' ')
      line_stack = []
    end

    l = line.split('=').collect {|i| i.strip}
    if l.length > 1
      key = l.delete_at(0)
      val = l.join(' = ').split(',').collect {|i| i.strip}
      retval[key] = val.length == 1 ? val.first : val
    end
  end
  retval
end

Instance Method Details

#[](k) ⇒ Object



9
10
11
# File 'lib/dspace/config.rb', line 9

def [] (k)
  opts[k]
end

#[]=(k, v) ⇒ Object



13
14
15
# File 'lib/dspace/config.rb', line 13

def []= (k, v)
  opts[k] = v
end

#lengthObject



17
18
19
# File 'lib/dspace/config.rb', line 17

def length
  opts.length
end