Module: Cline::Serializable::ClineData::ClassMethods

Defined in:
lib/cline/serializable/cline_data.rb

Overview

Class methods that should be made accessible to any class including our mixin

Internal collapse

Instance Method Details

#from_cline_data(base_dir, *args, create: false, **kwargs) ⇒ Object?

Instantiate an instance of the including class from a base directory.

Parameters:

  • base_dir (String)

    Base directory used to initialize the new instance

  • args (Array)

    Extra parameters to give to the instance's constructor

  • create (Boolean) (defaults to: false)

    Should data be created if it does not exist?

  • kwargs (Hash)

    Extra kwargs to give to the instance's constructor

Returns:

  • (Object, nil)

    The instance, or nil if no Cline data exists



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cline/serializable/cline_data.rb', line 40

def from_cline_data(base_dir, *args, create: false, **kwargs)
  instance = self.open(
    ::File.join(base_dir, cline_json_file(base_dir)),
    *args,
    default: create ? '{}' : nil,
    **kwargs
  )
  return unless instance

  instance.initialize_from_dir(base_dir, create:)
  instance
end

#monitor_cline_data_changes(base_dir, *args, on_change:, monitoring_interval_secs: 1, **kwargs) { ... } ⇒ Utils::FileMonitor?

Monitor changes done on the file and call a callback for each update.

Parameters:

  • base_dir (String)

    Base directory used to initialize the new instance

  • 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
75
76
77
# File 'lib/cline/serializable/cline_data.rb', line 65

def monitor_cline_data_changes(base_dir, *args, on_change:, monitoring_interval_secs: 1, **kwargs, &)
  monitor_updates(
    ::File.join(base_dir, cline_json_file(base_dir)),
    *args,
    on_change: proc do |instance|
      instance.initialize_from_dir(base_dir, create: false)
      on_change.call(instance)
    end,
    monitoring_interval_secs:,
    **kwargs,
    &
  )
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.



86
87
88
# File 'lib/cline/serializable/cline_data.rb', line 86

def new_instance(file, *args, **kwargs)
  of_hash(Utils::File.safe_json_read(file), *args, **kwargs)
end