Class: Schemacop::FieldNode

Inherits:
NodeSupportingType show all
Defined in:
lib/schemacop/field_node.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#options

Instance Method Summary collapse

Methods inherited from NodeSupportingType

build, #exec_block, #type

Methods inherited from NodeWithBlock

block_method, #exec_block

Methods inherited from Node

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

Constructor Details

#initialize(name, required, options = {}, &block) ⇒ FieldNode

Returns a new instance of FieldNode



5
6
7
8
9
10
11
12
13
14
# File 'lib/schemacop/field_node.rb', line 5

def initialize(name, required, options = {}, &block)
  if options.any?
    fail Exceptions::InvalidSchemaError, 'Node does not support options.'
  end

  super({}, &block)

  @name = name
  @required = required
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name



3
4
5
# File 'lib/schemacop/field_node.rb', line 3

def name
  @name
end

Instance Method Details

#validate(data, collector) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/schemacop/field_node.rb', line 16

def validate(data, collector)
  unless data.key?(name)
    collector.error "Missing key #{name.inspect}." if @required
    return
  end
  collector.path "/#{name}" do
    super(data[name], collector)
  end
end