Class: RSchema::Schemas::FixedLengthArray

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

Overview

A schema that represents an array of fixed length

Each element in the fixed-length array has its own subschema

Examples:

A fixed-length array schema

schema = RSchema.define { array(_Integer, _String) }
schema.valid?([5, "hello"]) #=> true
schema.valid?([5]) #=> false
schema.valid?([5, "hello", "world"]) #=> false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subschemas) ⇒ FixedLengthArray

Returns a new instance of FixedLengthArray.


19
20
21
# File 'lib/rschema/schemas/fixed_length_array.rb', line 19

def initialize(subschemas)
  @subschemas = subschemas
end

Instance Attribute Details

#subschemasObject (readonly)

Returns the value of attribute subschemas


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

def subschemas
  @subschemas
end

Instance Method Details

#call(value, options) ⇒ Object


23
24
25
26
27
28
# File 'lib/rschema/schemas/fixed_length_array.rb', line 23

def call(value, options)
  return type_failure(value) unless value.is_a?(Array)
  return size_failure(value) unless value.size == @subschemas.size

  accumulate_elements(value, options).to_result
end

#with_wrapped_subschemas(wrapper) ⇒ Object


30
31
32
33
# File 'lib/rschema/schemas/fixed_length_array.rb', line 30

def with_wrapped_subschemas(wrapper)
  wrapped_subschemas = subschemas.map { |ss| wrapper.wrap(ss) }
  self.class.new(wrapped_subschemas)
end