Module: Cline::Serializable::File::ClassMethods
- Defined in:
- lib/cline/serializable/file.rb
Overview
Class methods that should be made accessible to any class including our mixin
Internal collapse
-
#monitor_file_changes(file, on_change:, monitoring_interval_secs: 1) { ... } ⇒ Utils::FileMonitor?
Monitor changes done on the file and call a callback for each update.
-
#monitor_updates(file, *args, on_change:, monitoring_interval_secs: 1, **kwargs) { ... } ⇒ Utils::FileMonitor?
Monitor changes and call a callback for each update on the updated instance.
-
#new_instance(_file, *args, **kwargs) ⇒ Object
Default factory for instances.
-
#open(file, *args, default: nil, **kwargs) ⇒ Object?
Instantiate an instance of the including class from a given file.
Instance Method Details
#monitor_file_changes(file, on_change:, monitoring_interval_secs: 1) { ... } ⇒ Utils::FileMonitor?
Monitor changes done on the file and call a callback for each update.
47 48 49 50 51 |
# File 'lib/cline/serializable/file.rb', line 47 def monitor_file_changes(file, on_change:, monitoring_interval_secs: 1, &) monitor = Utils::FileMonitor.new(file, on_change:, monitoring_interval_secs:) monitor.start(&) monitor unless block_given? end |
#monitor_updates(file, *args, on_change:, monitoring_interval_secs: 1, **kwargs) { ... } ⇒ Utils::FileMonitor?
Monitor changes and call a callback for each update on the updated instance.
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cline/serializable/file.rb', line 65 def monitor_updates(file, *args, on_change:, monitoring_interval_secs: 1, **kwargs, &) monitor_file_changes( file, on_change: proc do |_mtime| on_change.call(self.open(file, *args, default: nil, **kwargs)) end, monitoring_interval_secs:, & ) end |
#new_instance(_file, *args, **kwargs) ⇒ Object
Default factory for instances. This could be overriden by some classes that need to instantiate differently.
83 84 85 |
# File 'lib/cline/serializable/file.rb', line 83 def new_instance(_file, *args, **kwargs) new(*args, **kwargs) end |
#open(file, *args, default: nil, **kwargs) ⇒ Object?
Instantiate an instance of the including class from a given file.
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/cline/serializable/file.rb', line 24 def open(file, *args, default: nil, **kwargs) unless ::File.exist?(file) return unless default FileUtils.mkdir_p(::File.dirname(file)) ::File.write(file, default) end instance = new_instance(file, *args, **kwargs) instance.initialize_from_file(file) instance end |