Sha256: 74eaa0830627e73c861d5174f7de7723dd25abf9e6aba328d5c7a4d67ba0a798

Contents?: true

Size: 1.28 KB

Versions: 5

Compression:

Stored size: 1.28 KB

Contents

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.values.each 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
      unless 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
    end

    def read_from(instance)
      @parameter_set.reduce({}) do |acc, (key, parameter)|
        acc.merge(Hash[key, parameter.read_from(instance)])
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rake_factory-0.9.0.pre.1 lib/rake_factory/parameter_set.rb
rake_factory-0.8.0 lib/rake_factory/parameter_set.rb
rake_factory-0.7.0 lib/rake_factory/parameter_set.rb
rake_factory-0.6.0 lib/rake_factory/parameter_set.rb
rake_factory-0.5.0 lib/rake_factory/parameter_set.rb