Sha256: 75f418d42c2d5c00644b3ad9d7080acf2f2948d49cd2ee055f67a766156c7521

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

module Helper
  class Properties
    attr_accessor :properties_hash
    attr_accessor :separator

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

    ### patch properties (key/value)
    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

    ### patch strings
    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
depengine-3.0.23 lib/depengine/helper/properties.rb
depengine-3.0.22 lib/depengine/helper/properties.rb
depengine-3.0.21 lib/depengine/helper/properties.rb