Class: Gemnasium::Configuration
- Inherits:
-
Object
- Object
- Gemnasium::Configuration
- Defined in:
- lib/gemnasium/configuration.rb
Constant Summary collapse
- DEFAULT_CONFIG =
{ 'site' => 'gemnasium.com', 'use_ssl' => true, 'api_version' => 'v3', 'ignored_paths' => [] }
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#ignored_paths ⇒ Object
Returns the value of attribute ignored_paths.
-
#profile_name ⇒ Object
Keep profile name for backward compatibility with version =< 2.0.
-
#project_branch ⇒ Object
Returns the value of attribute project_branch.
-
#project_name ⇒ Object
Name is required only to create the project, make it mandatory.
-
#project_slug ⇒ Object
Slug is required only to push the dependency files, make it optional.
-
#site ⇒ Object
Returns the value of attribute site.
-
#use_ssl ⇒ Object
Returns the value of attribute use_ssl.
Instance Method Summary collapse
-
#initialize(config_file) ⇒ Configuration
constructor
Initialize the configuration object from a YAML file.
- #migrate! ⇒ Object
- #needs_to_migrate? ⇒ Boolean
-
#store_value!(key, value, comment = nil) ⇒ Object
Store a key-value pair in the configuration file with an optional comment.
- #writable? ⇒ Boolean
Constructor Details
#initialize(config_file) ⇒ Configuration
Initialize the configuration object from a YAML file
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/gemnasium/configuration.rb', line 20 def initialize config_file unless File.file?(config_file) raise Errno::ENOENT, "Configuration file (#{config_file}) does not exist.\nPlease run `gemnasium install`." end @path = config_file config_hash = DEFAULT_CONFIG.merge(YAML.load(ERB.new(File.read(config_file)).result)) config_hash.each do |k, v| writer_method = "#{k}=" if respond_to?(writer_method) v = convert_ignored_paths_to_regexp(v) if k.to_s == 'ignored_paths' send(writer_method, v) end end raise 'Your configuration file does not contain all mandatory parameters or contain invalid values. Please check the documentation.' unless is_valid? end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
6 7 8 |
# File 'lib/gemnasium/configuration.rb', line 6 def api_key @api_key end |
#api_version ⇒ Object
Returns the value of attribute api_version.
6 7 8 |
# File 'lib/gemnasium/configuration.rb', line 6 def api_version @api_version end |
#ignored_paths ⇒ Object
Returns the value of attribute ignored_paths.
6 7 8 |
# File 'lib/gemnasium/configuration.rb', line 6 def ignored_paths @ignored_paths end |
#profile_name ⇒ Object
Keep profile name for backward compatibility with version =< 2.0
10 11 12 |
# File 'lib/gemnasium/configuration.rb', line 10 def profile_name @profile_name end |
#project_branch ⇒ Object
Returns the value of attribute project_branch.
6 7 8 |
# File 'lib/gemnasium/configuration.rb', line 6 def project_branch @project_branch end |
#project_name ⇒ Object
Name is required only to create the project, make it mandatory.
8 9 10 |
# File 'lib/gemnasium/configuration.rb', line 8 def project_name @project_name end |
#project_slug ⇒ Object
Slug is required only to push the dependency files, make it optional.
9 10 11 |
# File 'lib/gemnasium/configuration.rb', line 9 def project_slug @project_slug end |
#site ⇒ Object
Returns the value of attribute site.
6 7 8 |
# File 'lib/gemnasium/configuration.rb', line 6 def site @site end |
#use_ssl ⇒ Object
Returns the value of attribute use_ssl.
6 7 8 |
# File 'lib/gemnasium/configuration.rb', line 6 def use_ssl @use_ssl end |
Instance Method Details
#migrate! ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/gemnasium/configuration.rb', line 67 def migrate! # replace profile_name key with project_slug key (no value) content = File.readlines(path).map do |line| if line =~ /\Aprofile_name:.*\Z/ "project_slug:" else line end end.map{ |l| l.rstrip }.join("\n") + "\n" File.write path, content end |
#needs_to_migrate? ⇒ Boolean
63 64 65 |
# File 'lib/gemnasium/configuration.rb', line 63 def needs_to_migrate? !profile_name.nil? end |
#store_value!(key, value, comment = nil) ⇒ Object
Store a key-value pair in the configuration file with an optional comment. Try to preserve the comments and the indentation of the file. We assume the configuration file already features the given key.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/gemnasium/configuration.rb', line 47 def store_value!(key, value, comment = nil) pattern = /\A#{ key }:.*\Z/ new_line = "#{ key }: #{ value }" new_line += " # #{ comment }" if comment content = File.readlines(path).map do |line| line.rstrip.sub pattern, new_line end.join("\n") + "\n" File.write path, content end |
#writable? ⇒ Boolean
59 60 61 |
# File 'lib/gemnasium/configuration.rb', line 59 def writable? File.writable? path end |