Class: YAMLRewriter::Rewriter

Inherits:
Object
  • Object
show all
Defined in:
lib/yaml_rewriter/rewriter.rb

Overview

Usage:

class MyRewriter < YAMLRewriter::Rewriter
  define_rule ['foo' => 'bar']
  define_rule ['one', 'two', 'threeee' => 'three']
end

MyRewriter.new('foo: baz').rewrite # => 'bar: baz'

rewriter = MyRewriter.new(<<-END)
  one:
    two:
      threeee: 123
END
rewriter.rewrite # => one:
                        two:
                          three: 123

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yaml, reverse: false) ⇒ Rewriter

Returns a new instance of Rewriter.

Parameters:

  • yaml (String)
  • reverse (true|false) (defaults to: false)


22
23
24
25
26
# File 'lib/yaml_rewriter/rewriter.rb', line 22

def initialize(yaml, reverse: false)
  @yaml = yaml.dup
  @reverse = reverse
  @offset = 0
end

Class Method Details

.define_rule(rule) ⇒ Object

Parameters:

  • rule (Array)


29
30
31
# File 'lib/yaml_rewriter/rewriter.rb', line 29

def self.define_rule(rule)
  rules.push(Rule.new(rule))
end

.rulesObject



33
34
35
# File 'lib/yaml_rewriter/rewriter.rb', line 33

def self.rules
  @rules ||= []
end

Instance Method Details

#rewriteString

Returns:

  • (String)


38
39
40
41
42
# File 'lib/yaml_rewriter/rewriter.rb', line 38

def rewrite
  tree = Parser.new.parse(@yaml).handler.root
  traverse(tree)
  @yaml
end