class Schemacop::V2::Node

Attributes

options[R]

Public Class Methods

build(options, &block) click to toggle source
# File lib/schemacop/v2/node.rb, line 77
def self.build(options, &block)
  new(options, &block)
end
class_matches?(type) click to toggle source
# File lib/schemacop/v2/node.rb, line 68
def self.class_matches?(type)
  return false unless type.is_a?(Class)

  klasses.each do |klass|
    return true if type <= klass
  end
  return false
end
clear_klasses() click to toggle source
# File lib/schemacop/v2/node.rb, line 42
def self.clear_klasses
  self.klasses = [].freeze
end
clear_symbols() click to toggle source
# File lib/schemacop/v2/node.rb, line 34
def self.clear_symbols
  self.symbols = [].freeze
end
klass(klass) click to toggle source
# File lib/schemacop/v2/node.rb, line 38
def self.klass(klass)
  self.klasses += [klass]
end
new(options = {}) click to toggle source
# File lib/schemacop/v2/node.rb, line 81
def initialize(options = {})
  # Check and save given options
  @options = self.class.allowed_options.merge(options)
  if (obsolete_opts = @options.keys - self.class.allowed_options.keys).any?
    fail Exceptions::InvalidSchemaError,
         "Unrecognized option(s) #{obsolete_opts.inspect} for #{self.class.inspect}, allowed options: #{self.class.allowed_options.keys.inspect}."
  end

  if option?(:cast) && self.class.klasses.size > 1
    fail Exceptions::InvalidSchemaError,
         "Casting is only allowed for single-value datatypes, but type #{self.class.inspect} has classes "\
         "#{self.class.klasses.map(&:inspect)}."
  end
end
option(key, default: nil) click to toggle source
# File lib/schemacop/v2/node.rb, line 15
def self.option(key, default: nil)
  self.allowed_options = allowed_options.merge(key => default)
end
register(symbols: [], klasses: [], clear: true, before: nil) click to toggle source
# File lib/schemacop/v2/node.rb, line 46
def self.register(symbols: [], klasses: [], clear: true, before: nil)
  NodeResolver.register(self, before: before)
  symbols = [*symbols]
  klasses = [*klasses]
  if clear
    clear_symbols
    clear_klasses
  end
  symbols.each { |s| symbol s }
  klasses.each { |k| klass k }
end
symbol(symbol) click to toggle source
# File lib/schemacop/v2/node.rb, line 30
def self.symbol(symbol)
  self.symbols += [symbol]
end
symbol_matches?(type) click to toggle source
# File lib/schemacop/v2/node.rb, line 62
def self.symbol_matches?(type)
  return false unless type.is_a?(Symbol)

  symbols.include?(type)
end
type_matches?(type) click to toggle source
# File lib/schemacop/v2/node.rb, line 58
def self.type_matches?(type)
  symbol_matches?(type) || class_matches?(type)
end

Public Instance Methods

exec_block() click to toggle source
# File lib/schemacop/v2/node.rb, line 104
def exec_block
  fail Exceptions::InvalidSchemaError, 'Node does not support block.' if block_given?
end
option(key) click to toggle source
# File lib/schemacop/v2/node.rb, line 96
def option(key)
  options[key]
end
option?(key) click to toggle source
# File lib/schemacop/v2/node.rb, line 100
def option?(key)
  !!options[key]
end
resolve_type_klass(type) click to toggle source
# File lib/schemacop/v2/node.rb, line 108
def resolve_type_klass(type)
  klass = NodeResolver.resolve(type)
  unless klass
    fail Exceptions::InvalidSchemaError, "No validation class found for type #{type.inspect}."
  end

  return klass
end
type_filter_matches?(data) click to toggle source
# File lib/schemacop/v2/node.rb, line 125
def type_filter_matches?(data)
  !option?(:if) || option(:if).call(data)
end
type_label() click to toggle source
# File lib/schemacop/v2/node.rb, line 24
def type_label
  str = (symbols.first || 'unknown').to_s
  str += '*' if option?(:if)
  return str
end
type_matches?(data) click to toggle source
# File lib/schemacop/v2/node.rb, line 121
def type_matches?(data)
  self.class.type_matches?(data.class) && type_filter_matches?(data)
end
validate(data, collector) click to toggle source
# File lib/schemacop/v2/node.rb, line 117
def validate(data, collector)
  validate_custom_check(data, collector)
end

Protected Instance Methods

validate_custom_check(data, collector) click to toggle source
# File lib/schemacop/v2/node.rb, line 131
def validate_custom_check(data, collector)
  if option?(:check) && (check_result = option(:check).call(data)) != true
    if check_result.is_a?(String)
      collector.error "Custom :check failed: #{check_result}."
    else
      collector.error 'Custom :check failed.'
    end
  end
end