class Hashape::Shape

A template hash representing how a hash should be structured, allowing other hashes to be validated against it.

Attributes

shape[R]

Public Class Methods

new(shape) click to toggle source

Create a new shape. @param [Hash] shape The template hash. @return [Shape] The new shape.

# File lib/hashape.rb, line 77
def initialize(shape)
  @shape = shape
end

Public Instance Methods

matches!(subject) click to toggle source

Calls matches? and raises a RuntimeError if it does not return true. @param [Hash] subject The hash to compare the template hash against.

# File lib/hashape.rb, line 97
def matches!(subject)
  shape.each do |k, spec|
    v = subject[k]
    if v.is_a?(Hash) && spec.is_a?(Hash)
      Shape.new(spec).matches!(v)
    else
      unless spec === v
        raise ShapeMatchError,
          "key #{k} with value #{v} does not match spec #{spec}" \
      end
    end
  end
end
matches?(subject) click to toggle source

Returns a boolean indicating whether the given hash matches the template hash which this shape was constructed with. @param [Hash] subject The hash to compare the template hash against. @return [TrueClass|FalseClass] A boolean indicating whether the subject

hash matches the template hash.
# File lib/hashape.rb, line 87
def matches?(subject)
  matches!(subject)
  true
rescue ShapeMatchError
  false
end