Class: Licensure::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/licensure/configuration.rb

Overview

Loads and validates .licensure.yml configuration.

Constant Summary collapse

VALID_KEYS =
%w[allowed_licenses ignored_gems deny_unknown].freeze
SAMPLE_CONFIG =
<<~YAML.freeze
  # .licensure.yml
  allowed_licenses:
    - MIT
    - Apache-2.0
    - BSD-2-Clause
    - BSD-3-Clause
    - ISC
    - Ruby

  ignored_gems:
    - bundler
    - rake

  # Treat gems with unspecified licenses as warnings
  deny_unknown: true
YAML

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Configuration

Returns a new instance of Configuration.

Parameters:

  • data (Hash)


51
52
53
54
55
56
57
# File 'lib/licensure/configuration.rb', line 51

def initialize(data)
  warn_unknown_keys(data)

  @allowed_licenses = validate_array!(data.fetch("allowed_licenses", []), "allowed_licenses")
  @ignored_gems = validate_array!(data.fetch("ignored_gems", []), "ignored_gems")
  @deny_unknown = validate_boolean!(data.fetch("deny_unknown", true), "deny_unknown")
end

Instance Attribute Details

#allowed_licenses ⇒ Object (readonly)

Returns the value of attribute allowed_licenses.



48
49
50
# File 'lib/licensure/configuration.rb', line 48

def allowed_licenses
  @allowed_licenses
end

#ignored_gems ⇒ Object (readonly)

Returns the value of attribute ignored_gems.



48
49
50
# File 'lib/licensure/configuration.rb', line 48

def ignored_gems
  @ignored_gems
end

Class Method Details

.default ⇒ Licensure::Configuration



44
45
46
# File 'lib/licensure/configuration.rb', line 44

def self.default
  new({})
end

.load(path = ".licensure.yml") ⇒ Licensure::Configuration

Parameters:

  • path (String) (defaults to: ".licensure.yml")

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/licensure/configuration.rb', line 30

def self.load(path = ".licensure.yml")
  raise ConfigurationError, "Configuration file not found: #{path}" unless File.exist?(path)

  payload = YAML.safe_load(File.read(path), aliases: false) || {}
  unless payload.is_a?(Hash)
    raise ConfigurationError, "Configuration must be a mapping"
  end

  new(payload.transform_keys(&:to_s))
rescue Psych::SyntaxError => e
  raise ConfigurationError, "Failed to parse configuration: #{e.message}"
end

Instance Method Details

#deny_unknown? ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/licensure/configuration.rb', line 60

def deny_unknown?
  @deny_unknown
end