Class: LayerChecker::Module

Inherits:
Object
  • Object
show all
Defined in:
lib/layer_checker/module.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config = {}, parent: nil) ⇒ Module

Returns a new instance of Module.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/layer_checker/module.rb', line 7

def initialize(name, config = {}, parent: nil)
  @name = name
  @parent = parent
  @paths = Array(config['paths'] || [])
  @depends_on = Array(config['depends_on'] || [])
  @submodules = {}

  # Parse nested modules if they exist
  if config['modules']
    config['modules'].each do |submodule_name, submodule_config|
      @submodules[submodule_name] = Module.new(submodule_name, submodule_config, parent: self)
    end
  end
end

Instance Attribute Details

#depends_onObject (readonly)

Returns the value of attribute depends_on.



5
6
7
# File 'lib/layer_checker/module.rb', line 5

def depends_on
  @depends_on
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/layer_checker/module.rb', line 5

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



5
6
7
# File 'lib/layer_checker/module.rb', line 5

def parent
  @parent
end

#pathsObject (readonly)

Returns the value of attribute paths.



5
6
7
# File 'lib/layer_checker/module.rb', line 5

def paths
  @paths
end

#submodulesObject (readonly)

Returns the value of attribute submodules.



5
6
7
# File 'lib/layer_checker/module.rb', line 5

def submodules
  @submodules
end

Instance Method Details

#all_modulesObject

Get all modules (including nested ones) as a flat hash



33
34
35
36
37
38
39
# File 'lib/layer_checker/module.rb', line 33

def all_modules
  result = { full_name => self }
  @submodules.each_value do |submodule|
    result.merge!(submodule.all_modules)
  end
  result
end

#allowed_dependenciesObject

Get all allowed dependencies (resolving relative names)



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/layer_checker/module.rb', line 56

def allowed_dependencies
  return @depends_on unless parent

  # For nested modules, resolve dependencies relative to parent
  @depends_on.map do |dep|
    # If it's a simple name, it refers to a sibling
    if parent.submodules.key?(dep)
      "#{parent.full_name}.#{dep}"
    else
      dep
    end
  end
end

#can_depend_on?(other_module_name) ⇒ Boolean

Check if this module can depend on another module

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/layer_checker/module.rb', line 42

def can_depend_on?(other_module_name)
  # Direct dependency check
  return true if @depends_on.include?(other_module_name)

  # Check if it's a sibling module (same parent)
  if parent
    sibling_names = parent.submodules.keys
    return @depends_on.any? { |dep| sibling_names.include?(dep) && dep == other_module_name }
  end

  false
end

#full_nameObject

Get the full qualified name (e.g., “billing.domain”)



28
29
30
# File 'lib/layer_checker/module.rb', line 28

def full_name
  parent ? "#{parent.full_name}.#{name}" : name
end

#matches_file?(file_path) ⇒ Boolean

Check if a file path belongs to this module

Returns:

  • (Boolean)


23
24
25
# File 'lib/layer_checker/module.rb', line 23

def matches_file?(file_path)
  @paths.any? { |pattern| File.fnmatch?(pattern, file_path, File::FNM_PATHNAME) }
end

#to_sObject



70
71
72
# File 'lib/layer_checker/module.rb', line 70

def to_s
  full_name
end