Class: Jschematic::Attributes::AdditionalItems

Inherits:
Object
  • Object
show all
Includes:
Element
Defined in:
lib/jschematic/attributes/additional_items.rb

Instance Attribute Summary

Attributes included from Element

#id, #parent

Instance Method Summary collapse

Methods included from Element

#required?, #schema_for, #title, #to_s

Constructor Details

#initialize(allowed, &block) ⇒ AdditionalItems

Returns a new instance of AdditionalItems.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jschematic/attributes/additional_items.rb', line 8

def initialize(allowed, &block)
  case items = block.call("items")
  when Array
    @allowed = allowed
    @tuple_types_count = items.length
  else
    # TODO spec: additionalItems applies only with tuple-typing, so
    # even if it is set to false we allow anything... should probably raise
    # but the I-D is silent on the proper behavior in that case.
    @allowed = true
  end
end

Instance Method Details

#accepts?(instance) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jschematic/attributes/additional_items.rb', line 21

def accepts?(instance)
  return true if TrueClass === @allowed

  case @allowed
  when FalseClass
    (instance.length == @tuple_types_count) || fail_validation!("#{@tuple_types_count} items", "#{instance.length} items")
  when Hash
    schema = Schema.new(@allowed)
    additional = instance[@tuple_types_count..-1]
    additional.all?{ |item| schema.accepts?(item) }
  end
end