class YamlVault::YAMLTreeBuilder

Public Class Methods

new(target_paths, prefix, suffix, cryptor, mode) click to toggle source
Calls superclass method
# 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

Public Instance Methods

alias(anchor) click to toggle source
Calls superclass method
# 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_document(*) click to toggle source
Calls superclass method
# File lib/yaml_vault/yaml_tree_builder.rb, line 23
def end_document(*)
  @path_stack.pop
  super
end
end_mapping(*) click to toggle source
Calls superclass method
# File lib/yaml_vault/yaml_tree_builder.rb, line 37
def end_mapping(*)
  @path_stack.pop
  super
end
end_sequence(*) click to toggle source
Calls superclass method
# 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) click to toggle source
Calls superclass method
# 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_document(*) click to toggle source
Calls superclass method
# File lib/yaml_vault/yaml_tree_builder.rb, line 17
def start_document(*)
  result = super
  @path_stack.push "$"
  result
end
start_mapping(*) click to toggle source
Calls superclass method
# 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_sequence(*) click to toggle source
Calls superclass method
# 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

Private Instance Methods

add_prefix_and_suffix(value) click to toggle source
# File lib/yaml_vault/yaml_tree_builder.rb, line 107
def add_prefix_and_suffix(value)
  return "#{@prefix}#{value}#{@suffix}"
end
match_path?() click to toggle source
# File lib/yaml_vault/yaml_tree_builder.rb, line 121
def match_path?
  @target_paths.any? do |target_path|
    target_path.each_with_index.all? do |path, i|
      if path == "*"
        true
      else
        if path.is_a?(Regexp)
          path.match(@path_stack[i])
        elsif path.is_a?(Symbol)
          path.inspect == @path_stack[i]
        else
          path == @path_stack[i]
        end
      end
    end
  end
end
remove_prefix_and_suffix(value) click to toggle source
# File lib/yaml_vault/yaml_tree_builder.rb, line 111
def remove_prefix_and_suffix(value)
  if @prefix != nil && value.start_with?(@prefix)
    value = value.delete_prefix(@prefix)
  end
  if @suffix != nil && value.end_with?(@suffix)
    value = value.delete_suffix(@suffix)
  end
  value
end