Sha256: 4a15cfcbd13364c068bc1c499d2334ec80a2791b3e90510f1734cd4d4fd19ead

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

require 'yaml'

class SlimKeyfy::Slimutils::YamlProcessor

  attr_reader :locale, :yaml_output, :yaml_hash

  def initialize(locale, key_base, yaml_output=nil)
    @locale = locale
    @key_base = key_base
    @yaml_output = process_output_file(yaml_output)
    @yaml_hash = load_hash
  end

  def process_output_file(yaml_output)
    if yaml_output.nil?
      dir_of_key = @key_base.split(".").first
      yaml_output = default_yaml(dir_of_key, @locale)
    end
    path = File.expand_path(yaml_output.to_s)
    if File.exist?(path) 
      path
    else
      SlimKeyfy::Slimutils::FileWriter.write(path, "---")
      path
    end
  end

  def store!
    merged = {@locale => @yaml_hash}
    SlimKeyfy::Slimutils::FileWriter.write(@yaml_output, merged.to_yaml)
  end

  def load_hash
    h = YAML::load_file(@yaml_output)
    if h
      h[@locale]
    else 
      {} 
    end
  end

  def default_yaml(key, locale)
    File.expand_path("./config/locales/#{key}.#{locale}.yml")
  end

  def merge!(translation_key, translation)
    @yaml_hash, translation_key, translation = SlimKeyfy::Slimutils::Merger.merge_single_translation(@yaml_hash, translation_key, translation)
    [translation_key, translation]
  end

  def delete_translations(translations)
    return if translations.nil? or translations.empty? or @yaml_hash.empty?
    translations.each do |k, v|
      path = k.split('.')
      leaf = path.pop
      path.inject(@yaml_hash){|h, el| h[el]}.delete(leaf)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slimkeyfy-0.1.0 lib/slimkeyfy/slimutils/yaml_processor.rb
slimkeyfy-0.0.4 lib/slimkeyfy/slimutils/yaml_processor.rb
slimkeyfy-0.0.3 lib/slimkeyfy/slimutils/yaml_processor.rb