Class: NtqTools::Translations::YamlTool

Inherits:
Object
  • Object
show all
Includes:
HashUtils
Defined in:
lib/ntq_tools/translations/yaml_tool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashUtils

#dig_set

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

#contentObject

Returns the value of attribute content.



9
10
11
# File 'lib/ntq_tools/translations/yaml_tool.rb', line 9

def content
  @content
end

#fileObject

Returns the value of attribute file.



9
10
11
# File 'lib/ntq_tools/translations/yaml_tool.rb', line 9

def file
  @file
end

#filenameObject

Returns the value of attribute filename.



9
10
11
# File 'lib/ntq_tools/translations/yaml_tool.rb', line 9

def filename
  @filename
end

#filepathObject

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_contentObject



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