Class: Shipit::Cli::Configuration

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

Overview

This class maintains all system-wide configuration for shipit. It properly applies the correct source of configuration values based on the different contexts and also makes sure that compulsory data are not missing.

Constant Summary collapse

ATTRIBUTES =

List of all the configuration attributes stored for use within the gem

[:endpoint, :private_token, :protected_branches]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



13
14
15
# File 'lib/shipit/cli/configuration.rb', line 13

def endpoint
  @endpoint
end

#private_tokenObject

Returns the value of attribute private_token.



13
14
15
# File 'lib/shipit/cli/configuration.rb', line 13

def private_token
  @private_token
end

#protected_branchesObject

Returns the value of attribute protected_branches.



13
14
15
# File 'lib/shipit/cli/configuration.rb', line 13

def protected_branches
  @protected_branches
end

Instance Method Details

#apply(attributes = {}) ⇒ Object

Apply a configuration hash to a configuration instance

Examples:

Override one of the configuration attributes

config = Shipit::Cli::Configuration.new
config.apply(private_token: 'supersecret')
config.private_token #=> "supersecret"

Parameters:

  • attributes (Hash) (defaults to: {})

    list of key/values to apply to the configuration

Returns:

  • (Object)

    the configuration object



25
26
27
28
29
30
31
# File 'lib/shipit/cli/configuration.rb', line 25

def apply(attributes = {})
  prepared_attributes = prepare_attributes attributes
  prepared_attributes.each_pair do |attribute, value|
    send("#{attribute}=", value)
  end
  self
end

#motd_listObject



59
60
61
# File 'lib/shipit/cli/configuration.rb', line 59

def motd_list
  File.readlines(File.expand_path('../../motd', __FILE__)).map(&:chomp)
end

#to_hashHash

The configuration instance formatted as a stringified hash

Examples:

Override one of the configuration attributes

config = Shipit::Cli::Configuration.new
config.to_hash
#=> { "endpoint" => "https://gitlab.example.com/api/v3", "private_token" => "supersecret" }

Returns:

  • (Hash)

    the configuration object as a Hash



42
43
44
45
46
47
48
# File 'lib/shipit/cli/configuration.rb', line 42

def to_hash
  config_hash = ATTRIBUTES.inject({}) do |hash, attr|
    hash["#{attr}"] = instance_variable_get("@#{attr}")
    hash
  end
  Shipit::Cli::Sanitizer.symbolize config_hash
end

#to_stdoutObject

Write a configuration summary to STDOUT, useful for output in the CLI



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

def to_stdout
  to_hash.each_pair do |attribute, value|
    puts format("%-20s %-50s", "#{attribute}:", value)
  end
  nil
end