Class: Typespec::Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/typespec.rb

Overview

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pairs = {}) ⇒ Hash

Returns a new instance of Hash.



101
102
103
104
# File 'lib/typespec.rb', line 101

def initialize(pairs={})
  @pairs = pairs
  @pairs = {Typespec.anything => Typespec.anything} if @pairs.nil? or @pairs.empty?
end

Class Method Details

.[](pairs = {}) ⇒ Object



106
107
108
# File 'lib/typespec.rb', line 106

def self.[](pairs={})
  Typespec::Hash.new(pairs)
end

Instance Method Details

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
118
119
120
# File 'lib/typespec.rb', line 110

def valid?(value)
  if value.is_a? ::Hash
    value.all? do |k, v|
      @pairs.any? do |pair|
        pair.zip([k,v]).all? {|typespec, value| typespec.valid?(value)}
      end
    end
  else
    false
  end
end