class SettingsSpec::Specs

Attributes

errors[R]

A Hash whose keys are the keys of the invalid entries joined by “.”

Public Class Methods

new(specs) click to toggle source
# File lib/settings_spec/specs.rb, line 7
def initialize(specs)
  @specs = specs
  @errors = {}
end

Public Instance Methods

verify(settings) click to toggle source

Verifies the settings against the specifications having been loaded.

settings can be a Hash, or any object responding to :[] or keys in the settings.

Returns true if there is no error, false if there are errors. Errors can be accessed by Specs#errors.

# File lib/settings_spec/specs.rb, line 19
def verify(settings)
  if settings
    check([], @specs, settings)
  else
    @errors['.'] = 'Settings is a nil?!'
  end
  @errors.empty?
end
verify!(settings) click to toggle source

Same as Specs#verify, but raises an exception when the settings are invalid.

# File lib/settings_spec/specs.rb, line 29
def verify!(settings)
  verify(settings)
  unless @errors.empty?
    error_messages = []
    @errors.each do |k, v|
      error_messages << %{  %s: "%s"} % [k, v]
    end
    raise StandardError, "Some settings do not pass verification:\n#{error_messages.join("\n")}"
  end
end

Private Instance Methods

check(path, spec_node, setting_node) click to toggle source
# File lib/settings_spec/specs.rb, line 42
def check(path, spec_node, setting_node)
  case spec_node
    when Hash
  else
  end
  case spec_node
  when Hash
    spec_node.each do |k, v|
      check(path + [k], v, fetch(setting_node, k))
    end
  when String
    begin
      unless Visitor.new(setting_node).visit(spec_node)
        @errors[path.join(':')] = "#{spec_node} [val: #{setting_node}]"
      end
    rescue
        @errors[path.join(':')] = "#{spec_node} [exception: #{$!}]"
    end
  else
    raise InvalidSpec, "#{path.join('.')}: #{spec_node.to_s} (It should be a String.)"
  end
end
fetch(settings, key) click to toggle source
# File lib/settings_spec/specs.rb, line 73
def fetch(settings, key)
  return unless settings

  getters.each do |getter|
    val = getter.call(settings, key)
    return val unless val.nil?
  end
  return nil
end
getters() click to toggle source
# File lib/settings_spec/specs.rb, line 65
def getters
  @getters ||= [
    lambda { |obj, key| obj[key.to_s] rescue nil if obj.respond_to?(:'[]')},
    lambda { |obj, key| obj[key.to_sym] rescue nil if obj.respond_to?(:'[]')},
    lambda { |obj, key| obj.send(key.to_sym) rescue nil},
  ]
end