Class: Licensed::Sources::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/licensed/sources/source.rb

Defined Under Namespace

Classes: DependencyEnumerationNotImplementedError, Error

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Source

Returns a new instance of Source.



54
55
56
# File 'lib/licensed/sources/source.rb', line 54

def initialize(configuration)
  @config = configuration
end

Class Attribute Details

.sourcesObject (readonly)

Returns the value of attribute sources.



15
16
17
# File 'lib/licensed/sources/source.rb', line 15

def sources
  @sources
end

Instance Attribute Details

#configObject

all sources have a configuration



52
53
54
# File 'lib/licensed/sources/source.rb', line 52

def config
  @config
end

Class Method Details

.full_typeObject

Returns the source name as a “/” delimited string of all the module and class names following “Licensed::Sources::”. This is the type that is used to distinguish multiple versions of a sources from each other. e.g. for ‘Licensed::Sources::Yarn::V1`, this returns `yarn/v1`



34
35
36
# File 'lib/licensed/sources/source.rb', line 34

def full_type
  type_and_version.join("/")
end

.inherited(klass) ⇒ Object



16
17
18
19
20
# File 'lib/licensed/sources/source.rb', line 16

def inherited(klass)
  # add child source classes are defined,
  # add them to the known sources list
  (@sources ||= []) << klass
end

.typeObject

Returns the source name as the first snake cased class or module name following “Licensed::Sources::”. This is the type that is included in metadata files and cache paths. e.g. for ‘Licensed::Sources::Yarn::V1`, this returns “yarn”



26
27
28
# File 'lib/licensed/sources/source.rb', line 26

def type
  type_and_version[0]
end

.type_and_versionObject

Returns an array that includes the source’s type name at the first index, and optionally a version string for the source as the second index. Callers should override this function and not ‘type` or `full_type` when needing to adjust the default type and version parsing logic



42
43
44
45
46
47
48
# File 'lib/licensed/sources/source.rb', line 42

def type_and_version
  self.name.gsub("#{Licensed::Sources.name}::", "")
           .gsub(/([A-Z\d]+)([A-Z][a-z])/, "\\1_\\2".freeze)
           .gsub(/([a-z\d])([A-Z])/, "\\1_\\2".freeze)
           .downcase
           .split("::")
end

Instance Method Details

#dependenciesObject

Returns all dependencies that should be evaluated. Excludes ignored dependencies.



66
67
68
# File 'lib/licensed/sources/source.rb', line 66

def dependencies
  cached_dependencies.reject { |d| ignored?(d) }
end

#enabled?Boolean

Returns whether a source is enabled based on the environment in which licensed is run Defaults to false.

Returns:

  • (Boolean)


60
61
62
# File 'lib/licensed/sources/source.rb', line 60

def enabled?
  false
end

#enumerate_dependenciesObject

Enumerate all source dependencies. Must be implemented by each source class.



71
72
73
# File 'lib/licensed/sources/source.rb', line 71

def enumerate_dependencies
  raise DependencyEnumerationNotImplementedError
end

#ignored?(dependency) ⇒ Boolean

Returns whether a dependency is ignored in the configuration.

Returns:

  • (Boolean)


76
77
78
# File 'lib/licensed/sources/source.rb', line 76

def ignored?(dependency)
  config.ignored?("type" => self.class.type, "name" => dependency.name)
end