Module: Cline::Utils::EnumerableDirObjects
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/cline/utils/enumerable_dir_objects.rb
Overview
Simple mixin providing an Enumerable and Set interfaces on objects initialized from a list of sub-directories. Classes including this mixin should call Class.open(dir) to instantiate a new instance initialized from a directory, and implement the method object_from(dir) to return the corresponding name and object parsed from a sub-directory.
Internal collapse
-
.include_for(calling_class, object_class) ⇒ Object
Include the mixin and configure it for a specific object class This method automatically implements the required object_from method.
-
.included(base) ⇒ Object
Hook used when this mixin is included in a base class.
-
#refresh! ⇒ Object
Remove caches.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equality check.
-
#new(name) ⇒ Object
Create a new object from this sub-directories set.
Class Method Details
.include_for(calling_class, object_class) ⇒ Object
Include the mixin and configure it for a specific object class This method automatically implements the required object_from method
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cline/utils/enumerable_dir_objects.rb', line 54 def self.include_for(calling_class, object_class) calling_class.class_eval do include EnumerableDirObjects private # Get an object and its name from a sub-directory # # @param dir [String] The directory containing the object # @param create [Boolean] Should the instance be created if it does not exist? # @return [Array(String, Object)] Return 2 values: # 0. [String] The object name # 1. [Object] The object itself define_method(:object_from) do |dir, create:| [::File.basename(dir), object_class.open(dir, create:)] end end end |
.included(base) ⇒ Object
Hook used when this mixin is included in a base class
45 46 47 |
# File 'lib/cline/utils/enumerable_dir_objects.rb', line 45 def self.included(base) base.include(Serializable::Dir) end |
Instance Method Details
#==(other) ⇒ Boolean
Equality check
23 24 25 26 27 |
# File 'lib/cline/utils/enumerable_dir_objects.rb', line 23 def ==(other) other.is_a?(EnumerableDirObjects) && other.size == size && other.each.to_a.to_h == each.to_a.to_h end |
#new(name) ⇒ Object
Create a new object from this sub-directories set. This also creates a new sub-directory.
34 35 36 37 38 |
# File 'lib/cline/utils/enumerable_dir_objects.rb', line 34 def new(name) _name, instance = object_from(::File.join(dir, name), create: true) objects_set[name] = instance instance end |
#refresh! ⇒ Object
Remove caches.
74 75 76 |
# File 'lib/cline/utils/enumerable_dir_objects.rb', line 74 def refresh! @objects_set = nil end |