Sha256: fd58e67c4361bda683fb2f679199b8bfca24ff0ac1093da3f4de627fd6c6e7cd

Contents?: true

Size: 1.29 KB

Versions: 9

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require_relative 'parameter'
require_relative 'exceptions'

module RakeFactory
  class ParameterSet
    def initialize
      @parameter_set = {}
    end

    def add(name, options)
      parameter = Parameter.new(name, options)
      @parameter_set[parameter.name] = parameter
      parameter
    end

    def find(name)
      @parameter_set[name.to_sym]
    end

    def each(&block)
      @parameter_set.values.each(&block)
    end

    def update_default_for(name, value)
      find(name).default = value
    end

    def apply_defaults_to(instance)
      @parameter_set.each_value do |parameter|
        parameter.apply_default_to(instance)
      end
    end

    def enforce_requirements_on(instance)
      dissatisfied = @parameter_set.values.reject do |parameter|
        parameter.satisfied_by?(instance)
      end
      return if dissatisfied.empty?

      names = dissatisfied.map(&:name)
      names_string = names.join(',')
      pluralisation_string = names.length > 1 ? 's' : ''

      raise RequiredParameterUnset,
            "Required parameter#{pluralisation_string} #{names_string} unset."
    end

    def read_from(instance)
      @parameter_set.reduce({}) do |acc, (key, parameter)|
        acc.merge({ key => parameter.read_from(instance) })
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rake_factory-0.32.0.pre.5 lib/rake_factory/parameter_set.rb
rake_factory-0.32.0.pre.4 lib/rake_factory/parameter_set.rb
rake_factory-0.32.0.pre.3 lib/rake_factory/parameter_set.rb
rake_factory-0.32.0.pre.2 lib/rake_factory/parameter_set.rb
rake_factory-0.32.0.pre.1 lib/rake_factory/parameter_set.rb
rake_factory-0.31.0 lib/rake_factory/parameter_set.rb
rake_factory-0.30.0.pre.8 lib/rake_factory/parameter_set.rb
rake_factory-0.30.0.pre.7 lib/rake_factory/parameter_set.rb
rake_factory-0.30.0.pre.6 lib/rake_factory/parameter_set.rb