Class: Leftovers::Precompilers::YAML::Builder

Inherits:
Psych::TreeBuilder
  • Object
show all
Defined in:
lib/leftovers/precompilers/yaml/builder.rb

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



7
8
9
10
11
# File 'lib/leftovers/precompilers/yaml/builder.rb', line 7

def initialize
  @constants = []

  super
end

Instance Method Details

#add_constant_for_tag(tag, value = nil) ⇒ Object



13
14
15
16
17
18
# File 'lib/leftovers/precompilers/yaml/builder.rb', line 13

def add_constant_for_tag(tag, value = nil)
  match = %r{\A!ruby/[^:]*(?::(.*))?\z}.match(tag)
  return unless match

  @constants << (match[1] || value)
end

#scalar(value, _anchor, tag, *rest) ⇒ Object

leftovers:keep



34
35
36
37
38
39
# File 'lib/leftovers/precompilers/yaml/builder.rb', line 34

def scalar(value, _anchor, tag, *rest) # leftovers:keep
  add_constant_for_tag(tag, value)
  tag = nil

  super
end

#start_mapping(_anchor, tag, *rest) ⇒ Object

leftovers:keep



20
21
22
23
24
25
# File 'lib/leftovers/precompilers/yaml/builder.rb', line 20

def start_mapping(_anchor, tag, *rest) # leftovers:keep
  add_constant_for_tag(tag)
  tag = nil

  super
end

#start_sequence(_anchor, tag, *rest) ⇒ Object

leftovers:keep



27
28
29
30
31
32
# File 'lib/leftovers/precompilers/yaml/builder.rb', line 27

def start_sequence(_anchor, tag, *rest) # leftovers:keep
  add_constant_for_tag(tag)
  tag = nil

  super
end

#to_ruby_fileObject



41
42
43
44
45
46
# File 'lib/leftovers/precompilers/yaml/builder.rb', line 41

def to_ruby_file
  <<~FILE
    __leftovers_document(#{to_ruby_argument(root.to_ruby.first)})
    #{@constants.join("\n")}
  FILE
end