Class: Schemacop::V2::NodeSupportingType

Inherits:
NodeWithBlock show all
Defined in:
lib/schemacop/v2/node_supporting_type.rb

Direct Known Subclasses

ArrayValidator, FieldNode

Instance Attribute Summary

Attributes inherited from Node

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NodeWithBlock

block_method

Methods inherited from Node

class_matches?, clear_klasses, clear_symbols, klass, option, #option, #option?, register, #resolve_type_klass, symbol, symbol_matches?, #type_filter_matches?, #type_label, #type_matches?, type_matches?

Constructor Details

#initialize(options = {}, &block) ⇒ NodeSupportingType

Returns a new instance of NodeSupportingType.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/schemacop/v2/node_supporting_type.rb', line 9

def initialize(options = {}, &block)
  super(options)

  @types = []
  exec_block(&block)

  if @types.none?
    fail Exceptions::InvalidSchemaError, 'Block must contain a type definition or not be given at all.' if block_given?

    type :object
  end

  # if @types.none?
  #   super(options)
  #   exec_block(&block)
  #   if @types.none?
  #     fail Exceptions::InvalidSchemaError, 'Block must contain a type definition or not be given at all.' if block_given?
  #   end
  # else
  #   super({})
  # end
end

Class Method Details

.build(options, &block) ⇒ Object



5
6
7
# File 'lib/schemacop/v2/node_supporting_type.rb', line 5

def self.build(options, &block)
  new(nil, options, &block)
end

Instance Method Details

#exec_block(&block) ⇒ Object



32
33
34
35
36
37
# File 'lib/schemacop/v2/node_supporting_type.rb', line 32

def exec_block(&block)
  super
rescue NoMethodError
  @types = []
  type :hash, &block
end

#type(*args, **kwargs, &block) ⇒ Object

required signature: First argument must be a type or an array of types Following arguments may be subtypes Last argument may be an options hash. Options and given block are passed to the last specified type. Not permitted to give subtypes / options / a block if an array of types is given.

TODO: Probably change this method so that the 'rescue NoMethodError' happens in here directly and not in the constructor. This way we can always call 'type', even if we don't have one and the type is auto-guessed as it formerly was the case in the constructor.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/schemacop/v2/node_supporting_type.rb', line 50

def type(*args, **kwargs, &block)
  options = kwargs
  types = [*args.shift]
  subtypes = args

  unless types.any?
    fail Exceptions::InvalidSchemaError, 'At least one type must be given.'
  end

  if subtypes.any? && types.size > 1
    fail Exceptions::InvalidSchemaError, "First given type can't be an array if subtypes are given."
  end

  if types.size > 1 && options.any?
    fail Exceptions::InvalidSchemaError, 'No options can be specified if multiple types are given.'
  end

  types.each do |type|
    klass = resolve_type_klass(type)

    if subtypes.any?
      unless klass <= NodeSupportingType
        fail "Node #{klass} does not support subtypes."
      end

      child = klass.new do
        self.type(*subtypes, **options, &block)
      end

      # child = klass.build(options)
      #
      #
      # child.type(*subtypes, &block)
    else
      if klass == ObjectValidator && type.is_a?(Class)
        options[:classes] = type
      end

      child = klass.new(options, &block)
    end

    @types << child
  end
end

#validate(data, collector) ⇒ Object



95
96
97
98
# File 'lib/schemacop/v2/node_supporting_type.rb', line 95

def validate(data, collector)
  super
  validate_types(data, collector)
end