class ConfigCurator::Collection
Manages collections of units. @example Load a list of units and install them
Collection.new(manifest_path: 'manifest.yml').install
Constants
- UNIT_ATTRIBUTES
The possible attributes specific to each unit type. This should not include generic attributes such as {Unit#source} and {Unit#destination}.
- UNIT_TYPES
Supported unit types.
Attributes
Public Class Methods
# File lib/config_curator/collection.rb, line 26 def initialize(manifest_path: nil, logger: nil) self.logger = logger unless logger.nil? load_manifest manifest_path unless manifest_path.nil? end
Public Instance Methods
Creates a new unit object for the collection. @param type [Symbol] a unit type in {UNIT_TYPES} @param attributes [Hash] attributes for the unit from {UNIT_ATTRIBUTES} @return [Unit] the unit object of the appropriate subclass
# File lib/config_curator/collection.rb, line 93 def create_unit(type, attributes: {}) "#{self.class.name.split('::').first}::#{type.to_s.camelize}".constantize .new(options: unit_options, logger: logger).tap do |unit| {src: :source, dst: :destination}.each do |k, v| unit.send "#{v}=".to_sym, attributes[k] unless attributes[k].nil? end UNIT_ATTRIBUTES[type].each do |v| unit.send "#{v}=".to_sym, defaults[v] unless defaults[v].nil? unit.send "#{v}=".to_sym, attributes[v] unless attributes[v].nil? end end end
Installs all units from the manifest. @return [Boolean, nil] if units were installed or nil if fails mid-install
# File lib/config_curator/collection.rb, line 65 def install return false unless install? quiet: !(logger.level == Logger::DEBUG) UNIT_TYPES.each do |type| units[type.to_s.pluralize.to_sym].each do |unit| return nil unless install_unit(unit, type) end end true end
Checks all units in the manifest for any detectable install issues. @param quiet [Boolean] suppress some {#logger} output @return [Boolean] if units can be installed
# File lib/config_curator/collection.rb, line 79 def install?(quiet: false) result = true UNIT_TYPES.each do |type| units[type.to_s.pluralize.to_sym].each do |unit| result = install_unit?(unit, type, quiet) ? result : false end end result end
Loads the manifest from file. @param file [Hash] the yaml file to load @return [Hash] the loaded manifest
# File lib/config_curator/collection.rb, line 42 def load_manifest(file) self.manifest = ActiveSupport::HashWithIndifferentAccess.new YAML.load_file(file) end
Private Instance Methods
Hash of any defaults given in the manifest. @return [Hash] the defaults
# File lib/config_curator/collection.rb, line 118 def defaults return {} unless manifest manifest[:defaults].nil? ? {} : manifest[:defaults] end
Installs a unit. @param unit [Unit] the unit to install @param type [Symbol] the unit type @param quiet [Boolean] suppress some {#logger} output @return [Boolean] if unit was installed
# File lib/config_curator/collection.rb, line 139 def install_unit(unit, type, quiet = false) success = unit.install logger.info do "Installed #{type_name(type)}: #{unit.source} => #{unit.destination_path}" end unless quiet || !success return true rescue Unit::InstallFailed => e logger.fatal { "Halting install! Install attempt failed for #{type_name(type)}: #{e}" } return false end
Checks if a unit can be installed. @param unit [Unit] the unit to check @param type [Symbol] the unit type @param quiet [Boolean] suppress some {#logger} output @return [Boolean] if unit can be installed
# File lib/config_curator/collection.rb, line 156 def install_unit?(unit, type, quiet = false) unit.install? logger.info do "Testing install for #{type_name(type)}:" \ " #{unit.source} => #{unit.destination_path}" end unless quiet return true rescue Unit::InstallFailed => e logger.error { "Cannot install #{type_name(type)}: #{e}" } return false end
Formats a unit type for display. @param type [Symbol] the unit type @return [String] formatted unit type
# File lib/config_curator/collection.rb, line 112 def type_name(type) type.to_s.humanize capitalize: false end
Load basic unit options from the manifest. @return [Hash] the options
# File lib/config_curator/collection.rb, line 125 def unit_options options = {} return options unless manifest %i(root package_tool).each do |k| options[k] = manifest[k] unless manifest[k].nil? end options end