Class: Schemacop::NodeSupportingType
- Inherits:
-
NodeWithBlock
- Object
- Node
- NodeWithBlock
- Schemacop::NodeSupportingType
- Defined in:
- lib/schemacop/node_supporting_type.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Node
Class Method Summary collapse
Instance Method Summary collapse
- #exec_block(&block) ⇒ Object
-
#initialize(options = {}, &block) ⇒ NodeSupportingType
constructor
A new instance of NodeSupportingType.
-
#type(*args, &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.
- #validate(data, collector) ⇒ Object
Methods inherited from NodeWithBlock
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 |
# File 'lib/schemacop/node_supporting_type.rb', line 9 def initialize( = {}, &block) super() @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/node_supporting_type.rb', line 5 def self.build(, &block) new(nil, , &block) end |
Instance Method Details
#exec_block(&block) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/schemacop/node_supporting_type.rb', line 31 def exec_block(&block) super rescue NoMethodError @types = [] type :hash, {}, &block end |
#type(*args, &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.
49 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 |
# File 'lib/schemacop/node_supporting_type.rb', line 49 def type(*args, &block) = args.last.is_a?(Hash) ? args.pop : {} 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 && .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, , &block) end # child = klass.build(options) # # # child.type(*subtypes, &block) else if klass == ObjectValidator && type.is_a?(Class) [:classes] = type end child = klass.new(, &block) end @types << child end end |
#validate(data, collector) ⇒ Object
94 95 96 97 98 |
# File 'lib/schemacop/node_supporting_type.rb', line 94 def validate(data, collector) super validate_types(data, collector) end |