class CC::Yaml::Nodes::Mapping
Constants
- INCOMPATIBLE_KEYS_WARNING
Attributes
__getobj__[R]
mapping[R]
Public Class Methods
aliases()
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 14 def self.aliases @aliases ||= superclass.respond_to?(:aliases) ? superclass.aliases.dup : {} end
define_map_accessor(key)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 32 def self.define_map_accessor(key) define_method(key) { | | self[key] } unless method_defined? key define_method("#{key}=") { |v| self[key] = v } unless method_defined? "#{key}=" define_method("#{key}?") { | | !!self[key] } unless method_defined? "#{key}?" end
map(*list)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 18 def self.map(*list) options = Hash === list.last ? list.pop : {} list.each do |key| required << key.to_s if options[:required] define_map_accessor(key) case options[:to] when Symbol then aliases[key.to_s] = options[:to].to_s when Module then mapping[key.to_s] = options[:to] when nil then mapping[key.to_s] = Nodes[key] else raise ArgumentError, "unexpected value for to: %p" % options[:to] end end end
mapping()
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 6 def self.mapping @mapping ||= superclass.respond_to?(:mapping) ? superclass.mapping.dup : {} end
prefix_scalar(key = nil, *types)
click to toggle source
Calls superclass method
# File lib/cc/yaml/nodes/mapping.rb, line 42 def self.prefix_scalar(key = nil, *types) @prefix_scalar ||= superclass.respond_to?(:prefix_scalar) ? superclass.prefix_scalar : nil if key @prefix_scalar = key.to_s define_method(:visit_scalar) do |visitor, type, value, _implicit = true| return super(visitor, type, value, _implicit = true) if types.any? && !types.include?(type) visit_key_value(visitor, key, value) end end @prefix_scalar end
required()
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 10 def self.required @required ||= superclass.respond_to?(:required) ? superclass.required.dup : [] end
subnode_for_key(key)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 38 def self.subnode_for_key(key) mapping[aliases.fetch(key.to_s, key.to_s)] end
Public Instance Methods
==(other)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 125 def ==(other) other = other.mapping if other.is_a? Mapping if other.respond_to? :to_hash and other.to_hash.size == @mapping.size other.to_hash.all? { |k, v| include?(k) and self[k] == v } else false end end
[](key)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 95 def [](key) @mapping[mapped_key(key)] end
[]=(key, value)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 87 def []=(key, value) if mapped_key = mapped_key(key) @mapping[mapped_key] = value else warning("unexpected key %p, dropping", key) end end
accept_key?(key)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 112 def accept_key?(key) self.class.mapping.include? key end
deep_verify()
click to toggle source
Calls superclass method
# File lib/cc/yaml/nodes/mapping.rb, line 162 def deep_verify @mapping.each_value(&:deep_verify) super end
each_scalar(type = nil, &block)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 178 def each_scalar(type = nil, &block) return enum_for(:each_scalar, type) unless block @mapping.each_value { |v| v.each_scalar(type, &block) } end
empty?()
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 103 def empty? @mapping.empty? end
include?(key)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 99 def include?(key) @mapping.include? mapped_key(key) end
inspect()
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 121 def inspect @mapping.inspect end
mapped_key(key)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 107 def mapped_key(key) key = self.class.aliases.fetch(key.to_s, key.to_s) key if accept_key?(key) end
nested_warnings(*prefix)
click to toggle source
Calls superclass method
# File lib/cc/yaml/nodes/mapping.rb, line 167 def nested_warnings(*prefix) @mapping.inject(super) do |list, (key, value)| list = value.nested_warnings(*prefix, key) + list end end
prepare()
click to toggle source
Calls superclass method
# File lib/cc/yaml/nodes/mapping.rb, line 57 def prepare @mapping = {} super end
set_warnings(visitor, key, value)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 79 def set_warnings(visitor, key, value) if subnode_for(visitor, key, value) check_duplicates(key) else warning("unexpected key %p, dropping", key) end end
subnode_for(_visitor, key, _value)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 116 def subnode_for(_visitor, key, _value) type = self.class.subnode_for_key(key) type.new(self) if type end
verify()
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 134 def verify verify_errors verify_required verify_errors end
verify_errors()
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 153 def verify_errors @mapping.delete_if do |key, value| if value.errors? error "invalid %p section: %s", key, value.errors.join(", ") true end end end
verify_required()
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 140 def verify_required self.class.required.each do |key| next if @mapping.include? key type = self.class.subnode_for_key(key) if type.has_default? warning "missing key %p, defaulting to %p", key, type.default @mapping[key] = type.new(self) else error "missing key %p", key end end end
visit_key_value(visitor, key, value)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 74 def visit_key_value(visitor, key, value) node = subnode_for(visitor, key, value) assign_node_and_visit(node, key, value, visitor) end
visit_mapping(visitor, value)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 62 def visit_mapping(visitor, value) visitor.apply_mapping(self, value) end
visit_pair(visitor, key, value)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 66 def visit_pair(visitor, key, value) key = visitor.generate_key(self, key) unless set_warnings(visitor, key, value) check_incompatibility(key) visit_key_value(visitor, key, value) end end
with_value!(value)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 173 def with_value!(value) value = value.mapping while value.is_a? Mapping value.each { |key, value| self[key] = value } end
Protected Instance Methods
assign_node_and_visit(node, key, value, visitor)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 185 def assign_node_and_visit(node, key, value, visitor) self[key] = node visitor.accept(node, value) end
check_duplicates(key)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 196 def check_duplicates(key) warning("has multiple %p entries, keeping last entry", key) if self[key] end
check_incompatibility(key)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 200 def check_incompatibility(key) if creates_incompatibility?(key) warning INCOMPATIBLE_KEYS_WARNING end end
creates_incompatibility?(key)
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 206 def creates_incompatibility?(key) (key == "engines" && self["languages"]) || (key == "languages" && self["engines"]) end
dup_values()
click to toggle source
# File lib/cc/yaml/nodes/mapping.rb, line 190 def dup_values duped_mapping = @mapping.map { |key, value| [key.dup, value.dup] } @mapping = Hash[duped_mapping] self end