0.0
No commit activity in last 3 years
No release in over 3 years
Convert YAML to segments and back
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 1.3.3
 Project Readme

YamlConverters Build Status

Convert YAML to segments and back.

Installation

Add this line to your application's Gemfile:

gem 'yaml_converters'

And then execute:

$ bundle

Or install it yourself as:

$ gem install yaml_converters

Supported Ruby versions:

  • 2.0.0
  • 1.9.3

Usage

Converting YAML into segments

Use YamlConverters::YamlToSegmentsConverter#convert to split YAML file into flat key-value pairs.

yaml_reader = YamlConverters::YamlFileReader.new('path/to/file.yml')
converter = YamlConverters::YamlToSegmentsConverter.new(
  yaml_reader, YamlConverters::SegmentToHashWriter.new
)
converter.convert # => { 'key' => 'value', ... }

You can pass custom segment_writer to dump generated segments somewhere (database, for example). Custom writer should respond to two public methods:

  • write(key, value) - performs actual key-value pair dumping
  • result - returns whatever you want from convert

Default segment_writer is an instance of YamlConverters::SegmentToHashWriter that returns a Hash.

module YamlConverters
  class SegmentToHashWriter
    attr_reader :result

    def initialize
      @result = {}
    end

    def write(key, value)
      @result.deep_merge!({ key => value })
    end
  end
end

You can pass custom yaml_reader to read YAML file from a different source (AWS, for example). Custom reader should respond to one public method:

  • read - returns string containing YAML file contents

Example yaml_reader:

module YamlConverters
  class YamlFileReader
    def initialize(file_path)
      @file_path = file_path
    end

    def read
      File.read(@file_path)
    end
  end
end

Converting segments into YAML

Use YamlConverters::SegmentsToYamlConverter#convert to dump segments (key-value pairs) to YAML file.

converter = YamlConverters::SegmentsToYamlConverter(
  { 'key' => 'value' }, YamlConverters::YamlToStringWriter.new
)
converter.convert # => "---\nkey: value\n"

You can pass custom yaml_writer to dump generated YAML file somewhere (file, for example). Custom writer should respond to two public methods:

  • write(yaml_string) - performs actual YAML string dumping
  • result - returns whatever you want from convert

Default yaml_writer is an instance of YamlConverters::YamlToStringWriter that returns a String.

module YamlConverters
  class YamlToStringWriter
    attr_reader :result

    def initialize
      @result = ''
    end

    def write(yaml)
      @result = yaml
    end
  end
end

Acknowledgements

Implementation of flattening a YAML file and generating a YAML file from key-value pairs taken (I hope, for now) from Tolk codebase.

Warning

Until version 1.0.0 is reached API may be changed without any notice!

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request