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
- #read_raw ⇒ Object
- #write(keys) ⇒ Object
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
#path ⇒ Object
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
#read ⇒ Object
18 19 20 |
# File 'lib/translate/file.rb', line 18 def read File.exists?(path) ? YAML::load(IO.read(path)) : {} end |
#read_raw ⇒ Object
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 |