Class: Ehbrs::Tools::Observers::WithPersistence

Inherits:
Base
  • Object
show all
Defined in:
lib/ehbrs/tools/observers/with_persistence.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#blank_value, #last_check_time, #records

Instance Method Summary collapse

Methods inherited from Base

#changing_value?, #last_change_time, #last_value

Constructor Details

#initialize(path, options = {}) ⇒ WithPersistence

Returns a new instance of WithPersistence.



9
10
11
12
13
# File 'lib/ehbrs/tools/observers/with_persistence.rb', line 9

def initialize(path, options = {})
  super(options)
  @path = path.to_pathname
  load
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/ehbrs/tools/observers/with_persistence.rb', line 7

def path
  @path
end

Instance Method Details

#check(value, date = ::Time.required_zone.now) ⇒ Object



15
16
17
# File 'lib/ehbrs/tools/observers/with_persistence.rb', line 15

def check(value, date = ::Time.required_zone.now)
  save if super
end

#loadObject



19
20
21
22
23
24
# File 'lib/ehbrs/tools/observers/with_persistence.rb', line 19

def load
  save unless path.exist?
  data = ::YAML.load_file(path.to_path)
  @records = data.fetch(:records).map(&:to_struct)
  @last_check_time = data.fetch(:last_check_time)
end

#saveObject



26
27
28
29
# File 'lib/ehbrs/tools/observers/with_persistence.rb', line 26

def save
  path.parent.mkpath
  path.write({ records: records.map(&:to_h), last_check_time: last_check_time }.to_yaml)
end