Module: Cline::Serializable::Dir::ClassMethods

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

Overview

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

Public API collapse

Internal collapse

Instance Method Details

#new_instance(_dir, *args, **kwargs) ⇒ Object

Default factory for instances. This could be overriden by some classes that need to instantiate differently.

Parameters:

  • _dir (String)

    The directory to create the instance for.

  • 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.



43
44
45
# File 'lib/cline/serializable/dir.rb', line 43

def new_instance(_dir, *args, **kwargs)
  new(*args, **kwargs)
end

#open(dir, *args, create: false, **kwargs) ⇒ Object?

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

Parameters:

  • dir (String)

    Directory used to initialize the new instance

  • args (Array)

    Extra parameters to give to the instance's constructor

  • create (Boolean) (defaults to: false)

    Should the directory be created if it does not exist?

  • kwargs (Hash)

    Extra kwargs to give to the instance's constructor

Returns:

  • (Object, nil)

    The instance initialized from this directory, or nil if none



23
24
25
26
27
28
29
30
31
32
# File 'lib/cline/serializable/dir.rb', line 23

def open(dir, *args, create: false, **kwargs)
  unless ::File.exist?(dir) && ::File.directory?(dir)
    return unless create

    FileUtils.mkdir_p dir
  end
  instance = new_instance(dir, *args, **kwargs)
  instance.initialize_from_dir(dir, create:)
  instance
end