Class: Schemacop::Node
- Inherits:
-
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/schemacop/node.rb', line 78
def initialize(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
#options ⇒ Object
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
74
75
76
|
# File 'lib/schemacop/node.rb', line 74
def self.build(options, &block)
new(options, &block)
end
|
.class_matches?(type) ⇒ Boolean
66
67
68
69
70
71
72
|
# File 'lib/schemacop/node.rb', line 66
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 ⇒ Object
41
42
43
|
# File 'lib/schemacop/node.rb', line 41
def self.clear_klasses
self.klasses = [].freeze
end
|
.clear_symbols ⇒ Object
33
34
35
|
# File 'lib/schemacop/node.rb', line 33
def self.clear_symbols
self.symbols = [].freeze
end
|
.klass(klass) ⇒ Object
37
38
39
|
# File 'lib/schemacop/node.rb', line 37
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, before: nil) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/schemacop/node.rb', line 45
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
29
30
31
|
# File 'lib/schemacop/node.rb', line 29
def self.symbol(symbol)
self.symbols += [symbol]
end
|
.symbol_matches?(type) ⇒ Boolean
61
62
63
64
|
# File 'lib/schemacop/node.rb', line 61
def self.symbol_matches?(type)
return false unless type.is_a?(Symbol)
symbols.include?(type)
end
|
.type_matches?(type) ⇒ Boolean
57
58
59
|
# File 'lib/schemacop/node.rb', line 57
def self.type_matches?(type)
symbol_matches?(type) || class_matches?(type)
end
|
Instance Method Details
#exec_block ⇒ Object
103
104
105
|
# File 'lib/schemacop/node.rb', line 103
def exec_block
fail Exceptions::InvalidSchemaError, 'Node does not support block.' if block_given?
end
|
#option(key) ⇒ Object
93
94
95
|
# File 'lib/schemacop/node.rb', line 93
def option(key)
options[key]
end
|
#option?(key) ⇒ Boolean
97
98
99
100
101
|
# File 'lib/schemacop/node.rb', line 97
def option?(key)
!!options[key]
end
|
#resolve_type_klass(type) ⇒ Object
107
108
109
110
111
112
113
|
# File 'lib/schemacop/node.rb', line 107
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
123
124
125
|
# File 'lib/schemacop/node.rb', line 123
def type_filter_matches?(data)
!option?(:if) || option(:if).call(data)
end
|
#type_label ⇒ Object
23
24
25
26
27
|
# File 'lib/schemacop/node.rb', line 23
def type_label
str = (symbols.first || 'unknown').to_s
str += '*' if option?(:if)
return str
end
|
#type_matches?(data) ⇒ Boolean
119
120
121
|
# File 'lib/schemacop/node.rb', line 119
def type_matches?(data)
self.class.type_matches?(data.class) && type_filter_matches?(data)
end
|
#validate(data, collector) ⇒ Object
115
116
117
|
# File 'lib/schemacop/node.rb', line 115
def validate(data, collector)
validate_custom_check(data, collector)
end
|