Sha256: 5e762583e6b93a4c6121766e5382d14b1acd5b30bfd7511aac474c3001f4ea91

Contents?: true

Size: 1.95 KB

Versions: 25

Compression:

Stored size: 1.95 KB

Contents

require 'convert'

class Convert::Columnar < Convert
  class << self
    # Converts from YAML to Columnar format. This *always* converts to multiple
    # files.
    def from_yaml_to_columnar(args)
      from_yaml(yaml_path(args.source)).
        to_columnar(destination: columnar_path(args.destination))
    end

    private

    def columnar_path(path)
      path_or_default(path, 'data')
    end
  end

  # Convert the data to multiple text files.
  def to_columnar(options = {})
    root = options[:destination] or require_destination!
    @root = must_be_directory!(root)
    @data = @loader.container.sort.map(&:to_h)

    column_file('content_type') do |type|
      [ type['content-type'], Array(type['extensions']).join(' ') ].
        flatten.join(' ').strip
    end

    required_file('encoding')
    optional_file('docs')
    bool_file('obsolete')
    bool_file('registered')
    bool_file('signature')
    dict_file('xrefs')
    dict_file('friendly')
    optional_file('use_instead', 'use-instead')
  end

  def column_file(name, &block)
    File.open(File.join(@root, "mime.#{name}.column"), 'wb') do |f|
      f.puts @data.map(&block)
    end
  end

  def bool_file(name, *fields)
    fields = [ name ] if fields.empty?
    column_file(name) do |type|
      fields.map { |field|
        type[field] ? 1 : 0
      }.join(' ')
    end
  end

  def required_file(name, field = name)
    column_file(name) { |type| type[field] }
  end

  def optional_file(name, field = name)
    column_file(name) { |type| opt(type[field]) }
  end

  def array_file(name, field = name)
    column_file(name) { |type| arr(type[field]) }
  end

  def dict_file(name, field = name)
    column_file(name) { |type| dict(type[field]) }
  end

  def opt(value)
    value || '-'
  end

  def arr(value)
    Array(opt(value)).join('|')
  end

  def dict(value)
    if value
      value.sort.map { |k, v|
        [ k, Array(v).compact.join('^') ].join('^')
      }.join('|')
    else
      '-'
    end
  end
end

Version data entries

25 entries across 23 versions & 5 rubygems

Version Path
mrcooper-logstash-output-azuresearch-0.2.2 vendor/jruby/2.5.0/gems/mime-types-2.99.3/support/convert/columnar.rb
tdiary-5.0.8 vendor/bundle/gems/tdiary-5.0.7/vendor/bundle/gems/mime-types-2.99.3/support/convert/columnar.rb
tdiary-5.0.7 vendor/bundle/gems/mime-types-2.99.3/support/convert/columnar.rb
tdiary-5.0.6 vendor/bundle/gems/mime-types-2.99.3/support/convert/columnar.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/mime-types-2.99.3/support/convert/columnar.rb
tdiary-5.0.5 vendor/bundle/gems/mime-types-2.99.3/support/convert/columnar.rb
tdiary-5.0.4 vendor/bundle/gems/mime-types-2.99.3/support/convert/columnar.rb
vagrant-unbundled-1.9.1.1 vendor/bundle/ruby/2.4.0/gems/mime-types-2.99.3/support/convert/columnar.rb
tdiary-5.0.2 vendor/bundle/gems/mime-types-2.99.2/support/convert/columnar.rb
vagrant-compose-yaml-0.1.3 vendor/bundle/ruby/2.2.0/gems/mime-types-2.99.2/support/convert/columnar.rb
vagrant-compose-yaml-0.1.2 vendor/bundle/ruby/2.2.0/gems/mime-types-2.99.2/support/convert/columnar.rb
vagrant-compose-yaml-0.1.1 vendor/bundle/ruby/2.2.0/gems/mime-types-2.99.2/support/convert/columnar.rb
vagrant-compose-yaml-0.1.0 vendor/bundle/ruby/2.2.0/gems/mime-types-2.99.2/support/convert/columnar.rb
mime-types-2.99.3 support/convert/columnar.rb
vagrant-unbundled-1.8.5.2 vendor/bundle/ruby/2.3.0/gems/mime-types-2.99.2/support/convert/columnar.rb
vagrant-unbundled-1.8.5.1 vendor/bundle/ruby/2.3.0/gems/mime-types-2.99.2/support/convert/columnar.rb
vagrant-unbundled-1.8.4.2 vendor/bundle/ruby/2.3.0/gems/mime-types-2.99.2/support/convert/columnar.rb
vagrant-unbundled-1.8.4.1 vendor/bundle/ruby/2.3.0/gems/mime-types-2.99.2/support/convert/columnar.rb
tdiary-5.0.1 vendor/bundle/gems/mime-types-2.99.2/support/convert/columnar.rb
mime-types-2.99.2 support/convert/columnar.rb