Class: RbYAML::BaseResolver
Direct Known Subclasses
Constant Summary collapse
- @@yaml_implicit_resolvers =
{}
- @@yaml_path_resolvers =
{}
Class Method Summary collapse
- .add_implicit_resolver(tag, regexp, first) ⇒ Object
- .add_path_resolver(tag, path, kind = nil) ⇒ Object
Instance Method Summary collapse
- #ascend_resolver ⇒ Object
- #check_resolver_prefix(depth, path, kind, current_node, current_index) ⇒ Object
- #descend_resolver(current_node, current_index) ⇒ Object
-
#initialize ⇒ BaseResolver
constructor
A new instance of BaseResolver.
- #resolve(kind, value, implicit) ⇒ Object
Constructor Details
#initialize ⇒ BaseResolver
Returns a new instance of BaseResolver.
17 18 19 20 |
# File 'lib/rbyaml/resolver.rb', line 17 def initialize @resolver_exact_paths = [] @resolver_prefix_paths = [] end |
Class Method Details
.add_implicit_resolver(tag, regexp, first) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/rbyaml/resolver.rb', line 22 def self.add_implicit_resolver(tag, regexp, first) if first.nil? first = "" end first.each_byte { |ch| @@yaml_implicit_resolvers[ch] ||= [] @@yaml_implicit_resolvers[ch] << [tag,regexp] } end |
.add_path_resolver(tag, path, kind = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rbyaml/resolver.rb', line 32 def self.add_path_resolver(tag, path, kind=nil) new_path = [] for element in path if element.__is_a if element.length == 2 node_check, index_check = element elsif element.length == 1 node_check = element[0] index_check = true else raise ResolverError.new("Invalid path element: #{element}") end else node_check = nil index_check = element end if String == node_check node_check = ScalarNode elsif Array == node_check node_check = SequenceNode elsif Hash == node_check node_check = MappingNode elsif ![ScalarNode, SequenceNode, MappingNode].include?(node_check) && !node_check.__is_sym && !node_check.nil? raise ResolverError.new("Invalid node checker: #{node_check}") end if !(index_check.__is_str || index_check.__is_int) && !index_check.nil? raise ResolverError.new("Invalid index checker: #{index_check}") end new_path << [node_check, index_check] end if String == kind kind = ScalarNode elsif Array == kind kind = SequenceNode elsif Hash == kind kind = MappingNode elsif ![ScalarNode, SequenceNode, MappingNode].include?(kind) && !kind.nil? raise ResolverError.new("Invalid node kind: #{kind}") end @@yaml_path_resolvers[[[new_path], kind]] = tag end |
Instance Method Details
#ascend_resolver ⇒ Object
101 102 103 104 |
# File 'lib/rbyaml/resolver.rb', line 101 def ascend_resolver @resolver_exact_paths.pop @resolver_prefix_paths.pop end |
#check_resolver_prefix(depth, path, kind, current_node, current_index) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rbyaml/resolver.rb', line 106 def check_resolver_prefix(depth, path, kind, current_node, current_index) node_check, index_check = path[depth-1] if node_check.__is_str return false if current_node.tag != node_check elsif !node_check.nil? return false if !node_check === current_node end return false if index_check==true && !current_index.nil? return false if !index_check && current_index.nil? if index_check.__is_str return false if !(current_index.__is_scalar && index_check == current_index.value) elsif index_check.__is_int return false if index_check != current_index end true end |
#descend_resolver(current_node, current_index) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rbyaml/resolver.rb', line 74 def descend_resolver(current_node, current_index) exact_paths = {} prefix_paths = [] if current_node depth = @resolver_prefix_paths.length for path, kind in @resolver_prefix_paths[-1] if check_resolver_prefix(depth, path, kind,current_node, current_index) if path.length > depth prefix_paths << [path, kind] else exact_paths[kind] = @@yaml_path_resolvers[[path, kind]] end end end else for path, kind in @@yaml_path_resolvers if !path exact_paths[kind] = @@yaml_path_resolvers[[path, kind]] else prefix_paths << [path, kind] end end end @resolver_exact_paths << exact_paths @resolver_prefix_paths << prefix_paths end |
#resolve(kind, value, implicit) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/rbyaml/resolver.rb', line 123 def resolve(kind, value, implicit) if ScalarNode == kind && implicit[0] if value == "" resolvers = @@yaml_implicit_resolvers.fetch("",[]) else resolvers = @@yaml_implicit_resolvers.fetch(value[0],[]) end resolvers += @@yaml_implicit_resolvers.fetch(nil,[]) for tag, regexp in resolvers return tag if regexp =~ value end implicit = implicit[1] end exact_paths = @resolver_exact_paths[-1] return exact_paths[kind] if exact_paths.include?(kind) return exact_paths[nil] if exact_paths.include?(nil) if ScalarNode == kind return RbYAML::DEFAULT_SCALAR_TAG elsif SequenceNode == kind return RbYAML::DEFAULT_SEQUENCE_TAG elsif MappingNode == kind return RbYAML::DEFAULT_MAPPING_TAG end end |