Class: Ehbrs::Observers::WithPersistence
- Defined in:
- lib/ehbrs/observers/with_persistence.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Attributes inherited from Base
#blank_value, #last_check_time, #records
Instance Method Summary collapse
- #check(value, date = ::Time.required_zone.now) ⇒ Object
-
#initialize(path, options = {}) ⇒ WithPersistence
constructor
A new instance of WithPersistence.
- #load ⇒ Object
- #save ⇒ Object
Methods inherited from Base
#changing_value?, #last_change_time, #last_value
Constructor Details
#initialize(path, options = {}) ⇒ WithPersistence
Returns a new instance of WithPersistence.
11 12 13 14 15 |
# File 'lib/ehbrs/observers/with_persistence.rb', line 11 def initialize(path, = {}) super() @path = path.to_pathname load end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/ehbrs/observers/with_persistence.rb', line 9 def path @path end |
Instance Method Details
#check(value, date = ::Time.required_zone.now) ⇒ Object
17 18 19 |
# File 'lib/ehbrs/observers/with_persistence.rb', line 17 def check(value, date = ::Time.required_zone.now) save if super end |
#load ⇒ Object
21 22 23 24 25 26 |
# File 'lib/ehbrs/observers/with_persistence.rb', line 21 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 |
#save ⇒ Object
28 29 30 31 |
# File 'lib/ehbrs/observers/with_persistence.rb', line 28 def save path.parent.mkpath path.write({ records: records.map(&:to_h), last_check_time: last_check_time }.to_yaml) end |