Class: Webbynode::Properties

Inherits:
Hash
  • Object
show all
Defined in:
lib/webbynode/properties.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, create = true) ⇒ Properties

Returns a new instance of Properties.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/webbynode/properties.rb', line 9

def initialize(file, create=true)
  raise "Tried to instantiate a real property file" if $testing
  @file = file

  begin
    IO.foreach(file) do |line|
      if line = ~ /([^=]*)=(.*)\/\/(.*)/ || line =~ /([^=]*)=(.*)/
        key = $1.strip.to_s
        value = $2.strip 
        
        if value =~ /^\((.*)\)$/
          value = $1.split(' ')
        end
        
        self[key] = value
      end
    end
  rescue
  end
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



3
4
5
# File 'lib/webbynode/properties.rb', line 3

def file
  @file
end

#propertiesObject

Returns the value of attribute properties.



3
4
5
# File 'lib/webbynode/properties.rb', line 3

def properties
  @properties
end

Instance Method Details

#add(key, value = nil) ⇒ Object



36
37
38
39
# File 'lib/webbynode/properties.rb', line 36

def add(key, value = nil)
  return unless key.length > 0
  self[key] = value
end

#convertedObject



52
53
54
55
56
57
58
59
60
# File 'lib/webbynode/properties.rb', line 52

def converted
  hash = self.clone
  hash.each do |k, v|
    if v.is_a?(Array)
      hash[k] = "(#{v.join(" ")})"
    end
  end
  hash
end

#ioObject



5
6
7
# File 'lib/webbynode/properties.rb', line 5

def io
  @io ||= Webbynode::Io.new
end

#remove(key) ⇒ Object



41
42
43
44
# File 'lib/webbynode/properties.rb', line 41

def remove(key)
  return unless key.length > 0
  self.delete(key)
end

#saveObject



46
47
48
49
50
# File 'lib/webbynode/properties.rb', line 46

def save
  file = File.new(@file, "w+")
  self.converted.each { |key, value| file.puts "#{key}=#{value}\n" }
  file.close
end

#to_sObject



30
31
32
33
34
# File 'lib/webbynode/properties.rb', line 30

def to_s
  output = "File name #{@file}\n"
  self.each { |key, value| output += " #{key} = #{value}\n" }
  output
end