Class: LightTr::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/light_tr/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, file_name: nil, file_path: nil) ⇒ Store

Returns a new instance of Store.



7
8
9
10
11
12
# File 'lib/light_tr/store.rb', line 7

def initialize(name:, file_name: nil, file_path: nil)
  @name = name
  file_name ||= "#{name.to_s.downcase}.yml"
  file_path ||= file_path || Dir.pwd
  @file = File.join(file_path, file_name)
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



14
15
16
# File 'lib/light_tr/store.rb', line 14

def file
  @file
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/light_tr/store.rb', line 14

def name
  @name
end

Instance Method Details

#currentObject



16
17
18
19
20
# File 'lib/light_tr/store.rb', line 16

def current
  YAML.safe_load(File.read(file)) || {}
rescue StandardError => _e
  {}
end

#load(target, query) ⇒ Object



22
23
24
# File 'lib/light_tr/store.rb', line 22

def load(target, query)
  (current[target] || {})[query.downcase]
end

#save(target, query, translation) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/light_tr/store.rb', line 26

def save(target, query, translation)
  payload = current.merge(
    target => (current[target] || {}).merge(
      query.downcase => translation
    )
  )
  File.open(file, 'w') { |f| YAML.dump(payload, f) }
  translation
end