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.



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

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.



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

def dir
  @dir
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



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

def module_name
  @module_name
end

Instance Method Details

#basenamesObject



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

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

#basenames_without_extensionObject



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

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

#filenamesObject



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

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

#require_classesObject



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

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

#symbols_and_classesObject



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

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