Method: JSI::SchemaSet#initialize

Defined in:
lib/jsi/schema_set.rb

#initialize(enum) {|yields| ... } ⇒ SchemaSet

initializes a SchemaSet from the given enum and freezes it.

if a block is given, each element of the enum is passed to it, and the result must be a Schema. if no block is given, the enum must contain only Schemas.

Parameters:

  • enum (#each)

    the schemas to be included in the SchemaSet, or items to be passed to the block

Yield Parameters:

  • yields

    each element of enum for preprocessing into a Schema

Yield Returns:

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jsi/schema_set.rb', line 42

def initialize(enum, &block)
  if enum.is_a?(Schema)
    raise(ArgumentError, [
      "#{SchemaSet} initialized with a #{Schema}",
      "you probably meant to pass that to #{SchemaSet}[]",
      "or to wrap that schema in a Set or Array for #{SchemaSet}.new",
      "given: #{enum.pretty_inspect.chomp}",
    ].join("\n"))
  end

  unless enum.is_a?(Enumerable)
    raise(ArgumentError, "#{SchemaSet} initialized with non-Enumerable: #{enum.pretty_inspect.chomp}")
  end

  super

  not_schemas = reject { |s| s.is_a?(Schema) }
  if !not_schemas.empty?
    raise(Schema::NotASchemaError, [
      "#{SchemaSet} initialized with non-schema objects:",
      *not_schemas.map { |ns| ns.pretty_inspect.chomp },
    ].join("\n"))
  end

  freeze
end