Class: Rspider::ConfParser

Inherits:
Hash
  • Object
show all
Defined in:
lib/rspider/ConfParser.rb

Direct Known Subclasses

SpiderConfParser

Constant Summary collapse

Version =
'0.4.2'

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ ConfParser

Initialize the class with the path to the ‘config_file’ The class objects are dynamically generated by the name of the ‘param’ in the config file. Therefore, if the config file is ‘param = value’ then the itializer will eval “@param = value”

Raises:

  • (Errno::EACCES)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rspider/ConfParser.rb', line 39

def initialize(config_file)
		super()
		@config_file = config_file
		raise Errno::EACCES, "#{self.config_file} is not readable" unless File.readable?(self.config_file)
		open(self.config_file).each { |line| 
 line.chomp
 unless (/^\#/.match(line))
if(/\s*=\s*/.match(line))
  param, value = line.split(/\s*=\s*/, 2)  
  var_name = "#{param}".chomp.strip
  value = value.chomp.strip
  new_value = ''
  if (value)
	if value =~ /^['"](.*)['"]$/
	  new_value = $1
	else
	  new_value = value
	end
  else
	new_value = ''
  end 
	#          self.instance_variable_set("@#{var_name}", new_value)
	if self.has_key?(var_name) 
		self[var_name].push(new_value)
	else
		self[var_name]=[]
		self[var_name].push(new_value)
	end
  #self[var_name]=new_value
end
 end
		}
end

Instance Method Details

#config_fileObject



98
99
100
# File 'lib/rspider/ConfParser.rb', line 98

def config_file()
		@config_file
end

#config_file=(config_file) ⇒ Object



94
95
96
# File 'lib/rspider/ConfParser.rb', line 94

def config_file=(config_file)
		@config_file = config_file  
end

#get_value(param) ⇒ Object

This method will provide the value held by the object “@param” where “@param” is actually the name of the param in the config file.



76
77
78
# File 'lib/rspider/ConfParser.rb', line 76

def get_value(param)
		self[param]
end

#nil_value(param) ⇒ Object

This method will set the value of ‘@param’ to nil (not in the config file, only in the app).



90
91
92
# File 'lib/rspider/ConfParser.rb', line 90

def nil_value(param)
		self[param]=nil
end

#override_value(param, value) ⇒ Object

This method is simple. Should you need to override a value dynamically, use override_value(param, value) where ‘param’ is the name of the paramater in the config file.



84
85
86
# File 'lib/rspider/ConfParser.rb', line 84

def override_value(param, value)
		self[param]=value
end

#to_sObject



101
102
103
104
105
# File 'lib/rspider/ConfParser.rb', line 101

def to_s()
self.each{|k,v|
	puts "#{k}:=> #{v}\n"
}	
end