Class: YamlVault::YAMLTreeBuilder

Inherits:
YAML::TreeBuilder
  • Object
show all
Defined in:
lib/yaml_vault/yaml_tree_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(target_paths, prefix, suffix, cryptor, mode) ⇒ YAMLTreeBuilder

Returns a new instance of YAMLTreeBuilder.



6
7
8
9
10
11
12
13
14
15
# File 'lib/yaml_vault/yaml_tree_builder.rb', line 6

def initialize(target_paths, prefix, suffix, cryptor, mode)
  super()

  @path_stack = []
  @target_paths = target_paths
  @prefix = prefix
  @suffix = suffix
  @cryptor = cryptor
  @mode = mode
end

Instance Method Details

#alias(anchor) ⇒ Object



98
99
100
101
102
103
# File 'lib/yaml_vault/yaml_tree_builder.rb', line 98

def alias(anchor)
  unless @last.is_a?(YAML::Nodes::Sequence)
    @path_stack.pop
  end
  super
end

#end_documentObject



23
24
25
26
# File 'lib/yaml_vault/yaml_tree_builder.rb', line 23

def end_document(*)
  @path_stack.pop
  super
end

#end_mappingObject



37
38
39
40
# File 'lib/yaml_vault/yaml_tree_builder.rb', line 37

def end_mapping(*)
  @path_stack.pop
  super
end

#end_sequenceObject



51
52
53
54
# File 'lib/yaml_vault/yaml_tree_builder.rb', line 51

def end_sequence(*)
  @path_stack.pop
  super
end

#scalar(value, anchor, tag, plain, quoted, style) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/yaml_vault/yaml_tree_builder.rb', line 56

def scalar(value, anchor, tag, plain, quoted, style)
  result = super

  case @last
  when YAML::Nodes::Sequence
    current_path = @last.children.size - 1
    @path_stack << current_path
  when YAML::Nodes::Mapping
    if @last.children.size.odd?
      @path_stack << value
      return result
    end
  end

  if match_path?
    if @mode == :encrypt
      if tag
        result.value = @cryptor.encrypt("#{tag} #{value}")
        result.tag = nil
        result.plain = true
      else
        result.value = @cryptor.encrypt(value)
      end
      result.value = add_prefix_and_suffix(result.value)
    else
      value = remove_prefix_and_suffix(value)
      decrypted_value = @cryptor.decrypt(value).to_s
      if decrypted_value =~ /\A(!.*?)\s+(.*)\z/
        result.tag = $1
        result.plain = false
        result.value = $2
      else
        result.value = decrypted_value
      end
    end
  end

  @path_stack.pop

  result
end

#start_documentObject



17
18
19
20
21
# File 'lib/yaml_vault/yaml_tree_builder.rb', line 17

def start_document(*)
  result = super
  @path_stack.push "$"
  result
end

#start_mappingObject



28
29
30
31
32
33
34
35
# File 'lib/yaml_vault/yaml_tree_builder.rb', line 28

def start_mapping(*)
  if YAML::Nodes::Sequence === @last
    current_path = @last.children.size
    @path_stack << current_path
  end

  super
end

#start_sequenceObject



42
43
44
45
46
47
48
49
# File 'lib/yaml_vault/yaml_tree_builder.rb', line 42

def start_sequence(*)
  if YAML::Nodes::Sequence === @last
    current_path = @last.children.size
    @path_stack << current_path
  end

  super
end