Class: CC::Yaml::Nodes::Mapping

Inherits:
Node
  • Object
show all
Defined in:
lib/cc/yaml/nodes/mapping.rb

Direct Known Subclasses

Check, Engine, LanguageList, OpenMapping, Ratings, Root

Constant Summary collapse

INCOMPATIBLE_KEYS_WARNING =
"Use either a Languages key or an Engines key, but not both. They are mutually exclusive.".freeze

Instance Attribute Summary collapse

Attributes inherited from Node

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#as_json, #dup, #error, #errors, #errors?, has_default?, #initialize, #method_missing, #nested_warning, #respond_to_missing?, #to_s, #visit_child, #visit_scalar, #visit_sequence, #visit_unexpected, #warning, #warnings, #warnings?, #with_value

Constructor Details

This class inherits a constructor from CC::Yaml::Nodes::Node

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class CC::Yaml::Nodes::Node

Instance Attribute Details

#mappingObject (readonly) Also known as: __getobj__

Returns the value of attribute mapping.



54
55
56
# File 'lib/cc/yaml/nodes/mapping.rb', line 54

def mapping
  @mapping
end

Class Method Details

.aliasesObject



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



32
33
34
35
36
# 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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

.mappingObject



6
7
8
# 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) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# 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

.requiredObject



10
11
12
# File 'lib/cc/yaml/nodes/mapping.rb', line 10

def self.required
  @required ||= superclass.respond_to?(:required) ? superclass.required.dup : []
end

.subnode_for(key) ⇒ Object



38
39
40
# File 'lib/cc/yaml/nodes/mapping.rb', line 38

def self.subnode_for(key)
  mapping[aliases.fetch(key.to_s, key.to_s)]
end

Instance Method Details

#==(other) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/cc/yaml/nodes/mapping.rb', line 126

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) ⇒ Object



96
97
98
# File 'lib/cc/yaml/nodes/mapping.rb', line 96

def [](key)
  @mapping[mapped_key(key)]
end

#[]=(key, value) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/cc/yaml/nodes/mapping.rb', line 88

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) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/cc/yaml/nodes/mapping.rb', line 113

def accept_key?(key)
  self.class.mapping.include? key
end

#deep_verifyObject



163
164
165
166
# File 'lib/cc/yaml/nodes/mapping.rb', line 163

def deep_verify
  @mapping.each_value(&:deep_verify)
  super
end

#each_scalar(type = nil, &block) ⇒ Object



179
180
181
182
# File 'lib/cc/yaml/nodes/mapping.rb', line 179

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?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/cc/yaml/nodes/mapping.rb', line 104

def empty?
  @mapping.empty?
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/cc/yaml/nodes/mapping.rb', line 100

def include?(key)
  @mapping.include? mapped_key(key)
end

#inspectObject



122
123
124
# File 'lib/cc/yaml/nodes/mapping.rb', line 122

def inspect
  @mapping.inspect
end

#mapped_key(key) ⇒ Object



108
109
110
111
# File 'lib/cc/yaml/nodes/mapping.rb', line 108

def mapped_key(key)
  key = self.class.aliases.fetch(key.to_s, key.to_s)
  key if accept_key?(key)
end

#nested_warnings(*prefix) ⇒ Object



168
169
170
171
172
# File 'lib/cc/yaml/nodes/mapping.rb', line 168

def nested_warnings(*prefix)
  @mapping.inject(super) do |list, (key, value)|
    list = value.nested_warnings(*prefix, key) + list
  end
end

#prepareObject



57
58
59
60
# File 'lib/cc/yaml/nodes/mapping.rb', line 57

def prepare
  @mapping = {}
  super
end

#set_warnings(key) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/cc/yaml/nodes/mapping.rb', line 80

def set_warnings(key)
  if subnode_for(key)
    check_duplicates(key)
  else
    warning("unexpected key %p, dropping", key)
  end
end

#subnode_for(key) ⇒ Object



117
118
119
120
# File 'lib/cc/yaml/nodes/mapping.rb', line 117

def subnode_for(key)
  type = self.class.subnode_for(key)
  type.new(self) if type
end

#verifyObject



135
136
137
138
139
# File 'lib/cc/yaml/nodes/mapping.rb', line 135

def verify
  verify_errors
  verify_required
  verify_errors
end

#verify_errorsObject



154
155
156
157
158
159
160
161
# File 'lib/cc/yaml/nodes/mapping.rb', line 154

def verify_errors
  @mapping.delete_if do |key, value|
    if value.errors?
      warning "dropping %p section: %s", key, value.errors.join(', ')
      true
    end
  end
end

#verify_requiredObject



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cc/yaml/nodes/mapping.rb', line 141

def verify_required
  self.class.required.each do |key|
    next if @mapping.include? key
    type = self.class.subnode_for(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) ⇒ Object



74
75
76
77
78
# File 'lib/cc/yaml/nodes/mapping.rb', line 74

def visit_key_value(visitor, key, value)
  node = subnode_for(key)
  self[key] = node
  visitor.accept(node, value)
end

#visit_mapping(visitor, value) ⇒ Object



62
63
64
# 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) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/cc/yaml/nodes/mapping.rb', line 66

def visit_pair(visitor, key, value)
  key = visitor.generate_key(self, key)
  unless set_warnings(key)
    check_incompatibility(key)
    visit_key_value(visitor, key, value)
  end
end

#with_value!(value) ⇒ Object



174
175
176
177
# File 'lib/cc/yaml/nodes/mapping.rb', line 174

def with_value!(value)
  value = value.mapping while value.is_a? Mapping
  value.each { |key, value| self[key] = value }
end