Sha256: a71d20925ef3e126669b64e7cc5f1e9497f9b454d35c2fb92dcb98bb5d72e484

Contents?: true

Size: 818 Bytes

Versions: 5

Compression:

Stored size: 818 Bytes

Contents

class Isomer::Sources::Base
  attr_reader :parameters, :configuration, :errors

  def initialize(parameters)
    @parameters = parameters
    @errors = []
  end

  def load_and_validate
    load
    validate
  end

  def load
    raise NotImplementedError, "You must implement 'load' in #{self.class.name}"
  end

  def validate
    parameters.each do |parameter|
      if parameter.required?
        value = configuration[parameter.name]
        @errors << "#{parameter.name} is required" if valid(value)
      end
    end

    raise Isomer::RequiredParameterError, errors.join(', ') if !errors.empty?
  end

  def for(parameter)
    configuration[parameter.name] || parameter.default
  end

  private

  def valid(value)
    if value.respond_to?(:empty)
      value.empty?
    else
      value.nil?
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
isomer-0.1.3 lib/isomer/sources/base.rb
isomer-0.1.2 lib/isomer/sources/base.rb
isomer-0.1.1 lib/isomer/sources/base.rb
isomer-0.1.0 lib/isomer/sources/base.rb
isomer-0.0.1 lib/isomer/sources/base.rb