Class: Translate::File
- Inherits:
-
Object
- Object
- Translate::File
- Defined in:
- lib/translate/file.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
-
.deep_stringify_keys(hash) ⇒ Object
Stringifying keys for prettier YAML.
Instance Method Summary collapse
-
#initialize(path) ⇒ File
constructor
A new instance of File.
- #read ⇒ Object
- #write(keys) ⇒ Object
Constructor Details
#initialize(path) ⇒ File
Returns a new instance of File.
7 8 9 |
# File 'lib/translate/file.rb', line 7 def initialize(path) self.path = path end |
Instance Attribute Details
#path ⇒ Object
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
#read ⇒ Object
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 |