Class: Licensed::Configuration

Inherits:
AppConfiguration show all
Defined in:
lib/licensed/configuration.rb

Defined Under Namespace

Classes: LoadError

Constant Summary

Constants inherited from AppConfiguration

AppConfiguration::DEFAULT_CACHE_PATH, AppConfiguration::DEFAULT_CONFIG_FILES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AppConfiguration

#allow, #allowed?, #cache_path, #enabled?, #ignore, #ignored?, #pwd, #review, #reviewed?, #root, #source_path, #sources

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



132
133
134
135
136
137
# File 'lib/licensed/configuration.rb', line 132

def initialize(options = {})
  apps = options.delete("apps") || []
  super(default_options.merge(options))

  self["apps"] = apps.map { |app| AppConfiguration.new(app, options) }
end

Class Method Details

.expand_config_roots(config, config_path) ⇒ Object

Expand any roots specified in a configuration file based on the configuration files directory.



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/licensed/configuration.rb', line 183

def self.expand_config_roots(config, config_path)
  if config["root"] == true
    config["root"] = File.dirname(config_path)
  elsif config["root"]
    config["root"] = File.expand_path(config["root"], File.dirname(config_path))
  end

  if config["apps"]&.any?
    config["apps"].each { |app_config| expand_config_roots(app_config, config_path) }
  end
end

.find_config(directory) ⇒ Object

Find a default configuration file in the given directory. File preference is given by the order of elements in DEFAULT_CONFIG_FILES

Raises Licensed::Configuration::LoadError if a file isn’t found



153
154
155
156
157
158
# File 'lib/licensed/configuration.rb', line 153

def self.find_config(directory)
  config_file = DEFAULT_CONFIG_FILES.map { |file| directory.join(file) }
                                    .find { |file| file.exist? }

  config_file || raise(LoadError, "Licensed configuration not found in #{directory}")
end

.load_from(path) ⇒ Object

Loads and returns a Licensed::Configuration object from the given path. The path can be relative or absolute, and can point at a file or directory. If the path given is a directory, the directory will be searched for a ‘config.yml` file.



126
127
128
129
130
# File 'lib/licensed/configuration.rb', line 126

def self.load_from(path)
  config_path = Pathname.pwd.join(path)
  config_path = find_config(config_path) if config_path.directory?
  Configuration.new(parse_config(config_path))
end

.parse_config(config_path) ⇒ Object

Parses the configuration given at ‘config_path` and returns the values as a Hash

Raises Licensed::Configuration::LoadError if the file type isn’t known



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/licensed/configuration.rb', line 164

def self.parse_config(config_path)
  return {} unless config_path.file?

  extension = config_path.extname.downcase.delete "."
  config = case extension
  when "json"
    JSON.parse(File.read(config_path))
  when "yml", "yaml"
    YAML.load_file(config_path)
  else
    raise LoadError, "Unknown file type #{extension} for #{config_path}"
  end

  expand_config_roots(config, config_path)
  config
end

Instance Method Details

#appsObject

Returns an array of the applications for this licensed configuration. If the configuration did not explicitly configure any applications, return self as an application configuration.



142
143
144
145
# File 'lib/licensed/configuration.rb', line 142

def apps
  return [self] if self["apps"].empty?
  self["apps"]
end