Class: Schemacop::V2::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/schemacop/v2/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Node

Returns a new instance of Node.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/schemacop/v2/node.rb', line 4

def options
  @options
end

Class Method Details

.build(options, &block) ⇒ Object



77
78
79
# File 'lib/schemacop/v2/node.rb', line 77

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

.class_matches?(type) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
# 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_klassesObject



42
43
44
# File 'lib/schemacop/v2/node.rb', line 42

def self.clear_klasses
  self.klasses = [].freeze
end

.clear_symbolsObject



34
35
36
# File 'lib/schemacop/v2/node.rb', line 34

def self.clear_symbols
  self.symbols = [].freeze
end

.klass(klass) ⇒ Object



38
39
40
# File 'lib/schemacop/v2/node.rb', line 38

def self.klass(klass)
  self.klasses += [klass]
end

.option(key, default: nil) ⇒ Object



15
16
17
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



30
31
32
# File 'lib/schemacop/v2/node.rb', line 30

def self.symbol(symbol)
  self.symbols += [symbol]
end

.symbol_matches?(type) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# 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) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/schemacop/v2/node.rb', line 58

def self.type_matches?(type)
  symbol_matches?(type) || class_matches?(type)
end

Instance Method Details

#exec_blockObject



104
105
106
# 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) ⇒ Object



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

def option(key)
  options[key]
end

#option?(key) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/schemacop/v2/node.rb', line 100

def option?(key)
  !!options[key]
end

#resolve_type_klass(type) ⇒ Object



108
109
110
111
112
113
114
115
# 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) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/schemacop/v2/node.rb', line 125

def type_filter_matches?(data)
  !option?(:if) || option(:if).call(data)
end

#type_labelObject



24
25
26
27
28
# 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) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# 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) ⇒ Object



117
118
119
# File 'lib/schemacop/v2/node.rb', line 117

def validate(data, collector)
  validate_custom_check(data, collector)
end