Class: Translate::File

Inherits:
Object
  • Object
show all
Defined in:
lib/translate/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ File



7
8
9
# File 'lib/translate/file.rb', line 7

def initialize(path)
  self.path = path
end

Instance Attribute Details

#pathObject

Returns the value of attribute path



5
6
7
# File 'lib/translate/file.rb', line 5

def path
  @path
end

Class Method Details

.deep_stringify_keys(hash) ⇒ Object

Stringifying keys for prettier YAML



23
24
25
26
27
28
29
# File 'lib/translate/file.rb', line 23

def self.deep_stringify_keys(hash)
  hash.inject({}) { |result, (key, value)|
    value = deep_stringify_keys(value) if value.is_a? Hash
    result[(key.to_s rescue key) || key] = value
    result
  }
end

Instance Method Details

#readObject



18
19
20
# File 'lib/translate/file.rb', line 18

def read
  File.exists?(path) ? YAML::load(IO.read(path)) : {}
end

#write(keys) ⇒ Object



11
12
13
14
15
16
# File 'lib/translate/file.rb', line 11

def write(keys)
  FileUtils.mkdir_p File.dirname(path)
  File.open(path, "w") do |file|
    file.puts keys_to_yaml(Translate::File.deep_stringify_keys(keys))
  end
end