Class: Packwerk::Package

Inherits:
Object
  • Object
show all
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

: (name: String, ?config: Hash[String, untyped]?) -> void



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

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

Instance Attribute Details

#configObject (readonly)

: Hash[untyped, untyped]



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

def config
  @config
end

#dependenciesObject (readonly)

: Array



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

def dependencies
  @dependencies
end

#nameObject (readonly)

: String



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

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Object

: (untyped other) -> Integer?



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

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

  name <=> other.name
end

#dependency?(package) ⇒ Boolean

: (Package package) -> bool

Returns:

  • (Boolean)


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

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

#enforce_dependencies?Boolean

: -> bool

Returns:

  • (Boolean)


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

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

#eql?(other) ⇒ Boolean

: (untyped other) -> bool

Returns:

  • (Boolean)


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

def eql?(other)
  self == other
end

#hashObject

: -> Integer



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

def hash
  name.hash
end

#package_path?(path) ⇒ Boolean

: (String path) -> bool

Returns:

  • (Boolean)


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

def package_path?(path)
  return true if root?

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

#root?Boolean

: -> bool

Returns:

  • (Boolean)


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

def root?
  @name == ROOT_PACKAGE_NAME
end

#to_sObject

: -> String



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

def to_s
  name
end