class Configurious::Operations::OperationBase

Attributes

content[RW]
path[RW]

Public Instance Methods

apply(existing_content) click to toggle source
# File lib/configurious/operations.rb, line 62
def apply(existing_content)
  self.normalize_content!

  key, dict = select_node(existing_content)
  if dict && dict.is_a?(Hash)
    do_operation(dict, key)
  elsif dict.is_a?(String)
    raise "It looks like you selected a value when you meant to select a dictionary. Path #{path} resolves to #{dict}"
  else
    extra = @traverse_messages&.join("\n") || ""
    raise "No node at path '#{path}'\n #{extra}"
  end
end
do_operation(dict, key) click to toggle source

Performs the actual transform Is given the dictionary to transform and the key that should be transformed

# File lib/configurious/operations.rb, line 79
def do_operation(dict, key)

end
normalize_content!() click to toggle source
# File lib/configurious/operations.rb, line 14
def normalize_content!
  if @content.is_a? Hash
    @content.stringify_keys!
  end
end
select_node(dict) click to toggle source

Handles retrieving the content we really want to update from the content that is passed in

It's expected that the outer most node comes in and then we need to look at our key and if we have a path we need to traverse it

# File lib/configurious/operations.rb, line 28
def select_node(dict)
  if simple_path?
    [path, dict]
  else
    traverse_path(path,dict)
  end

end
simple_path?() click to toggle source
# File lib/configurious/operations.rb, line 10
def simple_path?
  !@path.include?('.')
end
traverse_path(path, dict) click to toggle source
# File lib/configurious/operations.rb, line 37
def traverse_path(path, dict)
  @traverse_messages = ["Traversing path #{path}"]

  path_parts = path.split('.')
  piece = dict

  path_parts[0..-2].each do |key|

    case
    when piece.is_a?(Hash) && piece.has_key?(key)
      @traverse_messages << "Traversed path part #{key}"
      piece = piece[key]
    when piece.is_a?(String)
      @traverse_messages << "Tried traversing to #{key} but the current piece #{piece} is not a hash"
      return [key, nil]
    else
      @traverse_messages << "Could not find #{key} in current piece. Existing Keys: #{piece.keys.join(',')}"
      return [key, nil]
    end
  end

  [path_parts.last, piece]

end