Class: Travis::Yaml::Nodes::Scalar

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

Direct Known Subclasses

BundlerArgs, Dist, Env::Variables, FixedValue, Group, Version

Instance Attribute Summary collapse

Attributes inherited from Node

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#decrypt, #decrypted?, #deep_verify, #dup, #encrypt, #encrypted?, #error, #errors, #errors?, #initialize, #method_missing, #nested_warning, #nested_warnings, #respond_to_missing?, #serialize, #to_json, #to_legacy_ruby, #to_ruby, #to_s, #to_yaml, #verify, #verify_language, #visit_mapping, #visit_pair, #visit_unexpected, #warngings?, #warning, #warnings

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

#valueObject Also known as: __getobj__

Returns the value of attribute value.



35
36
37
# File 'lib/travis/yaml/nodes/scalar.rb', line 35

def value
  @value
end

Class Method Details

.[](*types) ⇒ Object



4
5
6
7
8
9
# File 'lib/travis/yaml/nodes/scalar.rb', line 4

def self.[](*types)
  Class.new(self) do
    default_type(types.first)
    cast(*types)
  end
end

.cast(*types) ⇒ Object



15
16
17
18
19
# File 'lib/travis/yaml/nodes/scalar.rb', line 15

def self.cast(*types)
  @cast ||= superclass.respond_to?(:cast) ? superclass.cast.dup : []
  @cast.concat(types) if types.any?
  @cast
end

.cast?(type) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/travis/yaml/nodes/scalar.rb', line 11

def self.cast?(type)
  cast.include? type
end

.default(value = nil) ⇒ Object



26
27
28
29
# File 'lib/travis/yaml/nodes/scalar.rb', line 26

def self.default(value = nil)
  @default = value if value
  @default ||= nil
end

.default_type(type = nil) ⇒ Object



21
22
23
24
# File 'lib/travis/yaml/nodes/scalar.rb', line 21

def self.default_type(type = nil)
  @default_type = type if type
  @default_type ||= superclass.respond_to?(:default_type) ? superclass.default_type : :str
end

.has_default?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/travis/yaml/nodes/scalar.rb', line 31

def self.has_default?
  !default.nil?
end

Instance Method Details

#!@Object



92
93
94
# File 'lib/travis/yaml/nodes/scalar.rb', line 92

def !@
  !value
end

#==(other) ⇒ Object



48
49
50
51
# File 'lib/travis/yaml/nodes/scalar.rb', line 48

def ==(other)
  other = other.value if other.is_a? self.class
  other == value
end

#cast(visitor, type, value) ⇒ Object



82
83
84
85
86
# File 'lib/travis/yaml/nodes/scalar.rb', line 82

def cast(visitor, type, value)
  visitor.cast(type, value)
rescue ArgumentError => error
  error "failed to parse %p - %s", type.to_s, error.message.sub("():", ":")
end

#cast?(type) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/travis/yaml/nodes/scalar.rb', line 88

def cast?(type)
  self.class.cast?(type) or type == default_type
end

#default_typeObject



53
54
55
# File 'lib/travis/yaml/nodes/scalar.rb', line 53

def default_type
  self.class.default_type
end

#each_scalar(type = nil) {|value| ... } ⇒ Object

Yields:



106
107
108
109
# File 'lib/travis/yaml/nodes/scalar.rb', line 106

def each_scalar(type = nil, &block)
  return enum_for(:each_scalar, type) unless block
  yield value if type.nil? or type === value
end

#empty?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/travis/yaml/nodes/scalar.rb', line 38

def empty?
  value.nil?
end

#inspectObject



57
58
59
# File 'lib/travis/yaml/nodes/scalar.rb', line 57

def inspect
  value.inspect
end

#prepareObject



42
43
44
45
46
# File 'lib/travis/yaml/nodes/scalar.rb', line 42

def prepare
  self.value = self.class.default
  @multiple  = false
  super
end

#visit_child(visitor, value) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/travis/yaml/nodes/scalar.rb', line 72

def visit_child(visitor, value)
  if @multiple
    value = cast(visitor, :str, value) rescue value
    warning "does not support multiple values, dropping %p", value
  else
    @multiple = true
    visitor.accept(self, value)
  end
end

#visit_scalar(visitor, type, value, implicit = true) ⇒ Object



61
62
63
64
65
# File 'lib/travis/yaml/nodes/scalar.rb', line 61

def visit_scalar(visitor, type, value, implicit = true)
  return self.value = cast(visitor, type,         value) if cast? type
  return self.value = cast(visitor, default_type, value) if implicit
  error "%p not supported, dropping %p", type.to_s, cast(visitor, :str, value)
end

#visit_sequence(visitor, value) ⇒ Object



67
68
69
70
# File 'lib/travis/yaml/nodes/scalar.rb', line 67

def visit_sequence(visitor, value)
  @multiple = false
  visitor.apply_sequence(self, value)
end

#with_value(value) ⇒ Object



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

def with_value(value)
  return value.dup if value.is_a? self.class
  value = value.value while value.is_a? Scalar
  super(value)
end

#with_value!(value) ⇒ Object



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

def with_value!(value)
  self.value = value
end