Class: Travis::Yaml::Parser::Ruby

Inherits:
Object
  • Object
show all
Defined in:
lib/travis/yaml/parser/ruby.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, implicit = false) ⇒ Ruby

Returns a new instance of Ruby.



14
15
16
17
# File 'lib/travis/yaml/parser/ruby.rb', line 14

def initialize(value, implicit = false)
  @value    = value
  @implicit = implicit
end

Class Method Details

.parse(value) ⇒ Object



10
11
12
# File 'lib/travis/yaml/parser/ruby.rb', line 10

def self.parse(value)
  new(value).parse
end

.parses?(value) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/travis/yaml/parser/ruby.rb', line 6

def self.parses?(value)
  value.is_a? Hash
end

Instance Method Details

#accept(node, value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/travis/yaml/parser/ruby.rb', line 25

def accept(node, value)
  case value
  when Array                then node.visit_sequence   self, value
  when Hash                 then node.visit_mapping    self, value
  when SecureString         then node.visit_scalar     self, :secure, value,      @implicit
  when String               then node.visit_scalar     self, :str,    value,      @implicit
  when Symbol               then node.visit_scalar     self, :str,    value.to_s, @implicit
  when Integer              then node.visit_scalar     self, :int,    value,      @implicit
  when Float                then node.visit_scalar     self, :float,  value,      @implicit
  when DateTime, Time, Date then node.visit_scalar     self, :time,   value,      @implicit
  when true, false          then node.visit_scalar     self, :bool,   value,      @implicit
  when Regexp               then node.visit_scalar     self, :regexp, value,      @implicit
  when nil                  then node.visit_scalar     self, :null,   value,      @implicit
  else                           node.visit_unexpected self, value
  end
  node.verify
end

#apply_mapping(node, value) ⇒ Object



60
61
62
# File 'lib/travis/yaml/parser/ruby.rb', line 60

def apply_mapping(node, value)
  value.each_pair { |key, value| node.visit_pair(self, key, value) }
end

#apply_sequence(node, value) ⇒ Object



64
65
66
# File 'lib/travis/yaml/parser/ruby.rb', line 64

def apply_sequence(node, value)
  value.each { |child| node.visit_child(self, child) }
end

#cast(type, value) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/travis/yaml/parser/ruby.rb', line 43

def cast(type, value)
  case type
  when :str    then value.to_s
  when :binary then value.unpack('m').first
  when :bool   then !!value
  when :float  then Float   value
  when :int    then Integer value
  when :time   then value.to_time
  when :secure then SecureString === value ? value : SecureString.new(value.value)
  when :regexp then Regexp.new(value)
  when :null   then nil
  else raise ArgumentError, 'unknown scalar type %p' % type
  end
rescue RegexpError => error
  raise ArgumentError, "broken regular expression - #{error.message}"
end

#generate_key(node, value) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/travis/yaml/parser/ruby.rb', line 68

def generate_key(node, value)
  case value
  when String then value
  when Symbol then value.to_s
  else node.visit_unexpected(self, value, "expected string as key")
  end
end

#parse(root = nil) ⇒ Object



19
20
21
22
23
# File 'lib/travis/yaml/parser/ruby.rb', line 19

def parse(root = nil)
  root ||= Travis::Yaml::Nodes::Root.new
  accept(root, @value)
  root
end