Class: Packwerk::Package

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Comparable
Defined in:
lib/packwerk/package.rb

Overview

The basic unit of modularity for packwerk; a folder that has been declared to define a package. The package contains all constants defined in files in this folder and all subfolders that are not packages themselves.

Constant Summary collapse

ROOT_PACKAGE_NAME =
"."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, config: nil) ⇒ Package

Returns a new instance of Package.



24
25
26
27
28
29
# File 'lib/packwerk/package.rb', line 24

def initialize(name:, config: nil)
  @name = name
  @config = T.let(config || {}, T::Hash[String, T.untyped])
  @dependencies = T.let(Array(@config["dependencies"]).freeze, T::Array[String])
  @public_path = T.let(nil, T.nilable(String))
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/packwerk/package.rb', line 21

def config
  @config
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



18
19
20
# File 'lib/packwerk/package.rb', line 18

def dependencies
  @dependencies
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/packwerk/package.rb', line 15

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Object



49
50
51
52
53
# File 'lib/packwerk/package.rb', line 49

def <=>(other)
  return nil unless other.is_a?(self.class)

  name <=> other.name
end

#dependency?(package) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/packwerk/package.rb', line 37

def dependency?(package)
  @dependencies.include?(package.name)
end

#enforce_dependencies?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/packwerk/package.rb', line 32

def enforce_dependencies?
  [true, "strict"].include?(@config["enforce_dependencies"])
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/packwerk/package.rb', line 56

def eql?(other)
  self == other
end

#hashObject



61
62
63
# File 'lib/packwerk/package.rb', line 61

def hash
  name.hash
end

#package_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/packwerk/package.rb', line 42

def package_path?(path)
  return true if root?

  path.start_with?(@name + "/")
end

#root?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/packwerk/package.rb', line 71

def root?
  @name == ROOT_PACKAGE_NAME
end

#to_sObject



66
67
68
# File 'lib/packwerk/package.rb', line 66

def to_s
  name
end