Sha256: 9f9869b557c2aab2b7b68c010f569b4512d2fa527a29654967d5a71777401feb

Contents?: true

Size: 1.59 KB

Versions: 6

Compression:

Stored size: 1.59 KB

Contents

module Hieracles
  module Formats
    # for db compatibility
    class Csv < Hieracles::Format
      CVS_DELIM = ';'

      def info(_)
        make_csv @node.info.values
      end

      def facts(_)
        make_csv(@node.facts.keys) + make_csv(@node.facts.values)
      end

      def files(_)
        make_csv @node.files
      end

      def paths(_)
        make_csv @node.paths
      end

      def build_head(without_common)
        output = []
        @node.files(without_common).each do |f|
          output << f
        end
        output += %w(var value overriden)
        make_csv output
      end

      def build_params_line(key, value, filter)
        output = ''
        if !filter || Regexp.new(filter).match(key)
          if value[:overriden]
            output << build_line('-', key, value[:value])
            value[:found_in].each do |v|
              output << build_line(v[:file], key, v[:value], '1')
            end
          else
            output << build_line(value[:file], key, value[:value])
          end
        end
        output
      end

      def build_modules_line(key, value)
        make_csv [key, value]
      end

    private

      def build_line(whatfile, key, value, overriden = '0')
        make_csv(in_what_file(whatfile) + [key, value.to_s, overriden])
      end

      def make_csv(array)
        array.join(CVS_DELIM) + "\n"
      end

      def in_what_file(file)
        output = []
        @node.files.each do |f|
          if file == f
            output << '1'
          else
            output << '0'
          end
        end
        output
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
hieracles-0.3.6 lib/hieracles/formats/csv.rb
hieracles-0.3.5 lib/hieracles/formats/csv.rb
hieracles-0.3.4 lib/hieracles/formats/csv.rb
hieracles-0.3.3 lib/hieracles/formats/csv.rb
hieracles-0.3.2 lib/hieracles/formats/csv.rb
hieracles-0.3.1 lib/hieracles/formats/csv.rb