Class: DirectoryOfClasses

Inherits:
Object
  • Object
show all
Defined in:
lib/quality/directory_of_classes.rb

Overview

Represents a source directory full of classe - e.g., lib/vincelifedaily/checks/*

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir: raise, class_suffix: '', module_name: '') ⇒ DirectoryOfClasses

Returns a new instance of DirectoryOfClasses.



11
12
13
14
15
# File 'lib/quality/directory_of_classes.rb', line 11

def initialize(dir: raise, class_suffix: '', module_name: '')
  @dir = dir
  @class_suffix = class_suffix
  @module_name = module_name
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



9
10
11
# File 'lib/quality/directory_of_classes.rb', line 9

def dir
  @dir
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



9
10
11
# File 'lib/quality/directory_of_classes.rb', line 9

def module_name
  @module_name
end

Instance Method Details

#basenamesObject



21
22
23
# File 'lib/quality/directory_of_classes.rb', line 21

def basenames
  filenames.map { |filename| File.basename(filename) }
end

#basenames_without_extensionObject



25
26
27
# File 'lib/quality/directory_of_classes.rb', line 25

def basenames_without_extension
  filenames.map { |filename| File.basename(filename, '.*') }
end

#filenamesObject



17
18
19
# File 'lib/quality/directory_of_classes.rb', line 17

def filenames
  Dir.glob("#{dir}/**.rb").sort
end

#require_classesObject



29
30
31
32
33
34
# File 'lib/quality/directory_of_classes.rb', line 29

def require_classes
  basenames.each do |basename|
    path = "#{dir}/#{basename}"
    require_relative path
  end
end

#symbols_and_classesObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/quality/directory_of_classes.rb', line 36

def symbols_and_classes
  @symbols_and_classes ||= filenames.map do |filename|
    basename = File.basename(filename, '.rb')

    class_name = "#{module_name}::#{basename.camelize}#{@class_suffix}"
    clazz = class_name.constantize
    symbol = basename.to_sym
    [symbol, clazz]
  end
end