Class: NtqTools::Translations::YamlTool
- Inherits:
-
Object
- Object
- NtqTools::Translations::YamlTool
- Includes:
- HashUtils
- Defined in:
- lib/ntq_tools/translations/yaml_tool.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#file ⇒ Object
Returns the value of attribute file.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#filepath ⇒ Object
Returns the value of attribute filepath.
Instance Method Summary collapse
- #get_value(key) ⇒ Object
-
#initialize(filepath) ⇒ YamlTool
constructor
A new instance of YamlTool.
- #set_value(key, value) ⇒ Object
- #write_content ⇒ Object
Methods included from HashUtils
Constructor Details
#initialize(filepath) ⇒ YamlTool
Returns a new instance of YamlTool.
11 12 13 14 15 16 17 18 |
# File 'lib/ntq_tools/translations/yaml_tool.rb', line 11 def initialize(filepath) @filepath = filepath @file = File.open(filepath) @content = YAML.load(@file.read) name = filepath.split("/").last.split(".") name = name.size <= 2 ? nil : name.first @filename = name end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
9 10 11 |
# File 'lib/ntq_tools/translations/yaml_tool.rb', line 9 def content @content end |
#file ⇒ Object
Returns the value of attribute file.
9 10 11 |
# File 'lib/ntq_tools/translations/yaml_tool.rb', line 9 def file @file end |
#filename ⇒ Object
Returns the value of attribute filename.
9 10 11 |
# File 'lib/ntq_tools/translations/yaml_tool.rb', line 9 def filename @filename end |
#filepath ⇒ Object
Returns the value of attribute filepath.
9 10 11 |
# File 'lib/ntq_tools/translations/yaml_tool.rb', line 9 def filepath @filepath end |
Instance Method Details
#get_value(key) ⇒ Object
27 28 29 30 |
# File 'lib/ntq_tools/translations/yaml_tool.rb', line 27 def get_value(key) keys = (key.is_a?(Array) ? key : key.split('.')).map(&:to_s) @content.dig(*keys) end |
#set_value(key, value) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/ntq_tools/translations/yaml_tool.rb', line 32 def set_value(key, value) keys = (key.is_a?(Array) ? key : key.split('.')).map(&:to_s) new_content = dig_set(@content, keys, value) write_content new_content end |
#write_content ⇒ Object
20 21 22 23 24 25 |
# File 'lib/ntq_tools/translations/yaml_tool.rb', line 20 def write_content return false unless @content File.write(filepath, @content.to_yaml) true end |