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

Returns a new instance of File.



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

def initialize(path)
  self.path = path
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.deep_stringify_keys(hash) ⇒ Object

Stringifying keys for prettier YAML



27
28
29
30
31
32
33
34
# File 'lib/translate/file.rb', line 27

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).present? ?  key.to_s : 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

#read_rawObject



22
23
24
# File 'lib/translate/file.rb', line 22

def read_raw
  File.exists?(path) ? File.read(path) : ""
end

#write(keys) ⇒ Object



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

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