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

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.

Parameters:

  • file (String)

    File path to be monitored

  • on_change (#call)

    Block called each time there is an update.

    • Param mtime [Time, nil] New file modification time, or nil if no file
  • monitoring_interval_secs (Float) (defaults to: 1)

    The monitoring interval in seconds

Yields:

  • Optional code called while monitoring is in place. If used then monitoring is stopped at the end of the block's execution.

Returns:

  • (Utils::FileMonitor, nil)

    If no block has been given, return the monitor that needs to be stopped by the caller when monitoring should end.



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.

Parameters:

  • file (String)

    File path to be monitored

  • args (Array)

    Extra parameters to give to the instance's constructor

  • on_change (#call)

    Block called each time there is an update.

    • Param instance [Object, nil] New instance with updates, or nil if no instance
  • monitoring_interval_secs (Float) (defaults to: 1)

    The monitoring interval in seconds

  • kwargs (Hash)

    Extra kwargs to give to the instance's constructor

Yields:

  • Optional code called while monitoring is in place. If used then monitoring is stopped at the end of the block's execution.

Returns:

  • (Utils::FileMonitor, nil)

    If no block has been given, return the monitor that needs to be stopped by the caller when monitoring should end.



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.

Parameters:

  • _file (String)

    File to initialize from.

  • args (Array)

    Extra parameters to give to the instance's constructor.

  • kwargs (Hash)

    Extra kwargs to give to the instance's constructor.

Returns:

  • (Object)

    A new instance.



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.

Parameters:

  • file (String)

    File path used to initialize the new instance

  • args (Array)

    Extra parameters to give to the instance's constructor

  • default (String, nil) (defaults to: nil)

    Default file content to be created, or nil to only read existing one

  • kwargs (Hash)

    Extra kwargs to give to the instance's constructor

Returns:

  • (Object, nil)

    The instance, or nil if no file exists



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