Class: RSchema::Schemas::VariableHash

Inherits:
Object
  • Object
show all
Defined in:
lib/rschema/schemas/variable_hash.rb

Overview

A schema that matches variable-sized Hash objects, where the keys are not known ahead of time.

Examples:

A hash of integers to strings

schema = RSchema.define { variable_hash(_Integer => _String) }
schema.valid?({ 5 => "hello", 7 => "world" }) #=> true
schema.valid?({}) #=> true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_schema, value_schema) ⇒ VariableHash

Returns a new instance of VariableHash.


17
18
19
20
# File 'lib/rschema/schemas/variable_hash.rb', line 17

def initialize(key_schema, value_schema)
  @key_schema = key_schema
  @value_schema = value_schema
end

Instance Attribute Details

#key_schemaObject (readonly)

Returns the value of attribute key_schema


15
16
17
# File 'lib/rschema/schemas/variable_hash.rb', line 15

def key_schema
  @key_schema
end

#value_schemaObject (readonly)

Returns the value of attribute value_schema


15
16
17
# File 'lib/rschema/schemas/variable_hash.rb', line 15

def value_schema
  @value_schema
end

Instance Method Details

#call(value, options) ⇒ Object


22
23
24
25
26
# File 'lib/rschema/schemas/variable_hash.rb', line 22

def call(value, options)
  return not_a_hash_result(value) unless value.is_a?(Hash)

  accumulate_elements(value, options).to_result
end

#with_wrapped_subschemas(wrapper) ⇒ Object


28
29
30
31
32
33
# File 'lib/rschema/schemas/variable_hash.rb', line 28

def with_wrapped_subschemas(wrapper)
  self.class.new(
    wrapper.wrap(key_schema),
    wrapper.wrap(value_schema),
  )
end