Class: Travis::Yaml::Nodes::Mapping

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

Instance Attribute Summary collapse

Attributes inherited from Node

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#decrypt, #decrypted?, #dup, #encrypt, #encrypted?, #error, #errors, #errors?, has_default?, #initialize, #method_missing, #nested_warning, #respond_to_missing?, #serialize, #to_json, #to_legacy_ruby, #to_ruby, #to_s, #to_yaml, #verify_language, #visit_child, #visit_scalar, #visit_sequence, #visit_unexpected, #warngings?, #warning, #warnings, #with_value

Constructor Details

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

Dynamic Method Handling

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

Instance Attribute Details

#mappingObject (readonly) Also known as: __getobj__

Returns the value of attribute mapping.



78
79
80
# File 'lib/travis/yaml/nodes/mapping.rb', line 78

def mapping
  @mapping
end

Class Method Details

.aliasesObject



20
21
22
# File 'lib/travis/yaml/nodes/mapping.rb', line 20

def self.aliases
  @aliases ||= superclass.respond_to?(:aliases) ? superclass.aliases.dup : {}
end

.auto_prefix(key) ⇒ Object



40
41
42
43
# File 'lib/travis/yaml/nodes/mapping.rb', line 40

def self.auto_prefix(key)
  prefix_sequence(key)
  prefix_scalar(key)
end

.define_map_accessor(key) ⇒ Object



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

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

.drop_emptyObject



16
17
18
# File 'lib/travis/yaml/nodes/mapping.rb', line 16

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

.experimentalObject



12
13
14
# File 'lib/travis/yaml/nodes/mapping.rb', line 12

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

.map(*list) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/travis/yaml/nodes/mapping.rb', line 24

def self.map(*list)
  options = Hash === list.last ? list.pop : {}
  list.each do |key|
    drop_empty   << key.to_s if options.fetch(:drop_empty, true)
    required     << key.to_s if options[:required]
    experimental << key.to_s if options[:experimental]
    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



4
5
6
# File 'lib/travis/yaml/nodes/mapping.rb', line 4

def self.mapping
  @mapping ||= superclass.respond_to?(:mapping) ? superclass.mapping.dup : {}
end

.prefix_scalar(key = nil, *types) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/travis/yaml/nodes/mapping.rb', line 56

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? and not types.include?(type)
      visit_key_value(visitor, key, value)
    end
  end
  @prefix_scalar
end

.prefix_sequence(key = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/travis/yaml/nodes/mapping.rb', line 45

def self.prefix_sequence(key = nil)
  @prefix_sequence ||= superclass.respond_to?(:prefix_sequence) ? superclass.prefix_sequence : nil
  if key
    @prefix_sequence = key.to_s
    define_method(:visit_sequence) do |visitor, value|
      visit_key_value(visitor, key, value)
    end
  end
  @prefix_sequence
end

.requiredObject



8
9
10
# File 'lib/travis/yaml/nodes/mapping.rb', line 8

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

.subnode_for(key) ⇒ Object



74
75
76
# File 'lib/travis/yaml/nodes/mapping.rb', line 74

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

Instance Method Details

#==(other) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/travis/yaml/nodes/mapping.rb', line 148

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



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

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

#[]=(key, value) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/travis/yaml/nodes/mapping.rb', line 106

def []=(key, value)
  if mapped_key = mapped_key(key)
    unless value.is_a? Node
      node  = subnode_for(mapped_key)
      value = node if Parser::Ruby.new(value).parse(node)
    end
    @mapping[mapped_key] = value
  else
    warning("unexpected key %p, dropping", key)
  end
end

#accept_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/travis/yaml/nodes/mapping.rb', line 135

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

#deep_verifyObject



203
204
205
206
# File 'lib/travis/yaml/nodes/mapping.rb', line 203

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

#drop_empty?(key) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/travis/yaml/nodes/mapping.rb', line 102

def drop_empty?(key)
  self.class.drop_empty.include? mapped_key(key)
end

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



219
220
221
222
# File 'lib/travis/yaml/nodes/mapping.rb', line 219

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)


126
127
128
# File 'lib/travis/yaml/nodes/mapping.rb', line 126

def empty?
  @mapping.empty?
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#inspectObject



144
145
146
# File 'lib/travis/yaml/nodes/mapping.rb', line 144

def inspect
  @mapping.inspect
end

#mapped_key(key) ⇒ Object



130
131
132
133
# File 'lib/travis/yaml/nodes/mapping.rb', line 130

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



208
209
210
211
212
# File 'lib/travis/yaml/nodes/mapping.rb', line 208

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

#prepareObject



81
82
83
84
# File 'lib/travis/yaml/nodes/mapping.rb', line 81

def prepare
  @mapping = {}
  super
end

#subnode_for(key) ⇒ Object



139
140
141
142
# File 'lib/travis/yaml/nodes/mapping.rb', line 139

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

#verifyObject



157
158
159
160
161
162
163
# File 'lib/travis/yaml/nodes/mapping.rb', line 157

def verify
  verify_experimental
  verify_errors
  verify_empty
  verify_required
  verify_errors
end

#verify_emptyObject



172
173
174
175
176
177
178
179
# File 'lib/travis/yaml/nodes/mapping.rb', line 172

def verify_empty
  @mapping.delete_if do |key, value|
    next unless drop_empty? key and value.empty?
    value.nested_warnings.each { |p, w| nested_warning(w, key, *p) }
    warning('value for %p section is empty, dropping', key)
    true
  end
end

#verify_errorsObject



194
195
196
197
198
199
200
201
# File 'lib/travis/yaml/nodes/mapping.rb', line 194

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_experimentalObject



165
166
167
168
169
170
# File 'lib/travis/yaml/nodes/mapping.rb', line 165

def verify_experimental
  self.class.experimental.each do |key|
    next unless @mapping.include? key
    warning "%p is experimental and might be removed in the future", key
  end
end

#verify_requiredObject



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/travis/yaml/nodes/mapping.rb', line 181

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



95
96
97
98
99
100
# File 'lib/travis/yaml/nodes/mapping.rb', line 95

def visit_key_value(visitor, key, value)
  return warning("unexpected key %p, dropping", key) unless node = subnode_for(key)
  warning("has multiple %p entries, keeping last entry", key) if self[key]
  self[key] = node
  visitor.accept(node, value)
end

#visit_mapping(visitor, value) ⇒ Object



86
87
88
# File 'lib/travis/yaml/nodes/mapping.rb', line 86

def visit_mapping(visitor, value)
  visitor.apply_mapping(self, value)
end

#visit_pair(visitor, key, value) ⇒ Object



90
91
92
93
# File 'lib/travis/yaml/nodes/mapping.rb', line 90

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

#with_value!(value) ⇒ Object



214
215
216
217
# File 'lib/travis/yaml/nodes/mapping.rb', line 214

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