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.



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

def initialize
  @properties_hash = {}
  @separator = '='
end

Instance Attribute Details

#properties_hashObject

Returns the value of attribute properties_hash.



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

def properties_hash
  @properties_hash
end

#separatorObject

Returns the value of attribute separator.



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

def separator
  @separator
end

Instance Method Details

#patch(source, target) ⇒ Object

patch properties (key/value)



12
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
# File 'lib/depengine/helper/properties.rb', line 12

def patch(source, target)
  unless 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 do |entry|
    if entry.include? separator
      keyvalue = entry.split(separator, 2)

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

      entry = entry.sub(keyvalue[1].strip, properties_hash[keyvalue[0].strip]) unless properties_hash[keyvalue[0].strip].nil?
    end
    target_file.puts entry
  end

  target_file.close
end

#substitute(source, target) ⇒ Object

patch strings



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/depengine/helper/properties.rb', line 51

def substitute(source, target)
  unless File.file? source
    $log.writer.error "File #{source} does not exists"
    fail "File #{source} does not exists"
  end

  ### read backup file in array
  data = File.readlines(source)

  target_file = File.open(target, 'w')

  data.each do |entry|
    properties_hash.keys.each do |hashkey|
      entry = entry.sub(hashkey, properties_hash[hashkey])
    end
    target_file.puts entry
  end

  target_file.close
end