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.



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

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.



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

def dir
  @dir
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



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

def module_name
  @module_name
end

Instance Method Details

#basenamesObject



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

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

#basenames_without_extensionObject



33
34
35
# File 'lib/quality/directory_of_classes.rb', line 33

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

#filenamesObject



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

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

#require_classesObject



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

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

#symbols_and_classesObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/quality/directory_of_classes.rb', line 44

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