Class: LightTr::Store
- Inherits:
-
Object
- Object
- LightTr::Store
- Defined in:
- lib/light_tr/store.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #current ⇒ Object
-
#initialize(name:, file_name: nil, file_path: nil) ⇒ Store
constructor
A new instance of Store.
- #load(target, query) ⇒ Object
- #save(target, query, translation) ⇒ Object
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
#file ⇒ Object (readonly)
Returns the value of attribute file.
14 15 16 |
# File 'lib/light_tr/store.rb', line 14 def file @file end |
#name ⇒ Object (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
#current ⇒ Object
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 |