Class: SlimKeyfy::Slimutils::YamlProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/slimkeyfy/slimutils/yaml_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale, key_base, yaml_output = nil) ⇒ YamlProcessor

Returns a new instance of YamlProcessor.



7
8
9
10
11
12
# File 'lib/slimkeyfy/slimutils/yaml_processor.rb', line 7

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

Instance Attribute Details

#localeObject (readonly)

Returns the value of attribute locale.



5
6
7
# File 'lib/slimkeyfy/slimutils/yaml_processor.rb', line 5

def locale
  @locale
end

#yaml_hashObject (readonly)

Returns the value of attribute yaml_hash.



5
6
7
# File 'lib/slimkeyfy/slimutils/yaml_processor.rb', line 5

def yaml_hash
  @yaml_hash
end

#yaml_outputObject (readonly)

Returns the value of attribute yaml_output.



5
6
7
# File 'lib/slimkeyfy/slimutils/yaml_processor.rb', line 5

def yaml_output
  @yaml_output
end

Instance Method Details

#default_yaml(key, locale) ⇒ Object



42
43
44
# File 'lib/slimkeyfy/slimutils/yaml_processor.rb', line 42

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

#delete_translations(translations) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/slimkeyfy/slimutils/yaml_processor.rb', line 51

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

#load_hashObject



33
34
35
36
37
38
39
40
# File 'lib/slimkeyfy/slimutils/yaml_processor.rb', line 33

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

#merge!(translation_key, translation) ⇒ Object



46
47
48
49
# File 'lib/slimkeyfy/slimutils/yaml_processor.rb', line 46

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

#process_output_file(yaml_output) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/slimkeyfy/slimutils/yaml_processor.rb', line 14

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

#store!Object



28
29
30
31
# File 'lib/slimkeyfy/slimutils/yaml_processor.rb', line 28

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