Class: Helper::Properties

Inherits:
Object
  • Object
show all
Defined in:
lib/depengine/helper/properties.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProperties

Returns a new instance of Properties.



7
8
9
10
# File 'lib/depengine/helper/properties.rb', line 7

def initialize()
  properties_hash = {}
  separator = "="
end

Instance Attribute Details

#properties_hashObject

Returns the value of attribute properties_hash.



4
5
6
# File 'lib/depengine/helper/properties.rb', line 4

def properties_hash
  @properties_hash
end

#separatorObject

Returns the value of attribute separator.



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

def separator
  @separator
end

Instance Method Details

#patch(source, target) ⇒ Object

patch properties (key/value)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
62
63
# File 'lib/depengine/helper/properties.rb', line 13

def patch(source, target)

  if not File.file? source
    $log.writer.error "File #{source} does not exists"
    exit 1
  end
  
  if target.nil?
    $log.writer.error "Parameter target not set"
    exit 1
  end
  
  if target.length == 0
    $log.writer.error "Parameter target not set"
    exit 1
  end
  
   
  ### read source file in array
  data = File.readlines(source)

  target_file = File.open(target, "w")
  
  data.each { |entry|
    if entry.include? separator
      keyvalue = entry.split(separator,2)

      if keyvalue[1].class == TrueClass
 keyvalue[1] = 'true'
      end
    if properties_hash[keyvalue[0].strip].class == TrueClass
 properties_hash[keyvalue[0].strip] = 'true'
    end

      if keyvalue[1].class == FalseClass
 keyvalue[1] = 'false'
      end
    if properties_hash[keyvalue[0].strip].class == FalseClass
 properties_hash[keyvalue[0].strip] = 'false'
    end

      if properties_hash[keyvalue[0].strip] != nil
        entry = entry.sub(keyvalue[1].strip, properties_hash[keyvalue[0].strip])
      end
    end
    target_file.puts entry
  }

  target_file.close

end

#substitute(source, target) ⇒ Object

patch strings



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/depengine/helper/properties.rb', line 66

def substitute(source, target)

  if not File.file? source
    $log.writer.error "File #{source} does not exists"
    exit 1
  end   
   
  ### read backup file in array
  data = File.readlines(source)

  target_file = File.open(target, "w")
  
  data.each { |entry|
    properties_hash.keys.each { |hashkey|
      entry = entry.sub(hashkey, properties_hash[hashkey])
    }
    target_file.puts entry
  }

  target_file.close

end