Class: Schemacop::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/schemacop/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



76
77
78
79
80
81
82
83
# File 'lib/schemacop/node.rb', line 76

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
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options



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

def options
  @options
end

Class Method Details

.build(options, &block) ⇒ Object



72
73
74
# File 'lib/schemacop/node.rb', line 72

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

.class_matches?(type) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/schemacop/node.rb', line 64

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



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

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

.clear_symbolsObject



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

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

.klass(klass) ⇒ Object



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

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

.option(key, default: nil) ⇒ Object



14
15
16
# File 'lib/schemacop/node.rb', line 14

def self.option(key, default: nil)
  self.allowed_options = allowed_options.merge(key => default)
end

.register(symbols: [], klasses: [], clear: true) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/schemacop/node.rb', line 43

def self.register(symbols: [], klasses: [], clear: true)
  NodeResolver.register(self)
  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



27
28
29
# File 'lib/schemacop/node.rb', line 27

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

.symbol_matches?(type) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/schemacop/node.rb', line 59

def self.symbol_matches?(type)
  return false unless type.is_a?(Symbol)
  symbols.include?(type)
end

.type_matches?(type) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/schemacop/node.rb', line 55

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

Instance Method Details

#exec_blockObject



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

def exec_block
  fail Exceptions::InvalidSchemaError, 'Node does not support block.' if block_given?
end

#option(key) ⇒ Object



85
86
87
# File 'lib/schemacop/node.rb', line 85

def option(key)
  options[key]
end

#option?(key) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
# File 'lib/schemacop/node.rb', line 89

def option?(key)
  # rubocop:disable Style/DoubleNegation
  !!options[key]
  # rubocop:enable Style/DoubleNegation
end

#resolve_type_klass(type) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/schemacop/node.rb', line 99

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)


115
116
117
# File 'lib/schemacop/node.rb', line 115

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

#type_labelObject



21
22
23
24
25
# File 'lib/schemacop/node.rb', line 21

def type_label
  str = (symbols.first || 'unknown').to_s
  str += '*' if option?(:if)
  return str
end

#type_matches?(data) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/schemacop/node.rb', line 111

def type_matches?(data)
  self.class.type_matches?(data.class) && type_filter_matches?(data)
end

#validate(data, collector) ⇒ Object



107
108
109
# File 'lib/schemacop/node.rb', line 107

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