<commit_before># YamlConverters  [![Build Status](https://secure.travis-ci.org/tomekw/yaml_converters.png)](http://travis-ci.org/tomekw/yaml_converters)

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:

* `1.9.3`
* `1.9.2`

## Usage

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

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`.

``` ruby
module YamlConverters
  class SegmentToHashWriter
    attr_reader :result

    def initialize
      @result = {}
    end

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

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

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`.

``` ruby
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.

## 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
<commit_msg>Provide examples how to use converters<commit_after># YamlConverters  [![Build Status](https://secure.travis-ci.org/tomekw/yaml_converters.png)](http://travis-ci.org/tomekw/yaml_converters)

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:

* `1.9.3`
* `1.9.2`

## Usage

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

``` ruby
converter = YamlConverters::YamlToSegmentsConverter.new(
  'path/to/file.yml', 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`.

``` ruby
module YamlConverters
  class SegmentToHashWriter
    attr_reader :result

    def initialize
      @result = {}
    end

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

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

``` ruby
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`.

``` ruby
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.

## 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

{'fraction_non_alphanumeric': 0.09820309235269536, 'fraction_numerical': 0.00459674049310489, 'mean_word_length': 4.125267665952891, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1807154', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1479, 'n_words': 518}