class Constancy::Diff

Public Class Methods

new(target:, local:, remote:, mode:) click to toggle source
# File lib/constancy/diff.rb, line 5
def initialize(target:, local:, remote:, mode:)
  @target = target
  @local = local
  @remote = remote
  @mode = mode

  @all_keys = (@local.keys + @remote.keys).sort.uniq

  @diff =
    @all_keys.collect do |key|
      excluded = false
      op = :noop
      if @remote.has_key?(key) and not @local.has_key?(key)
        case @mode
        when :push
          op = @target.delete? ? :delete : :ignore
        when :pull
          op = :create
        end
      elsif @local.has_key?(key) and not @remote.has_key?(key)
        case @mode
        when :push
          op = :create
        when :pull
          op = @target.delete? ? :delete : :ignore
        end
      else
        if @remote[key] == @local[key]
          op = :noop
        else
          op = :update
        end
      end

      consul_key = [@target.prefix, key].compact.join("/").squeeze("/")

      if @target.exclude.include?(key) or @target.exclude.include?(consul_key)
        op = :ignore
        excluded = true
      end

      filename =
        case @target.type
        when :dir then File.join(@target.base_path, key)
        when :file then @target.base_path
        end

      display_filename =
        case @target.type
        when :dir then File.join(@target.base_path, key).trim_path
        when :file then "#{@target.base_path.trim_path}#{':'.gray}#{key.cyan}"
        end

      OpenStruct.new(
        op: op,
        excluded: excluded,
        relative_path: key,
        filename: filename,
        display_filename: display_filename,
        consul_key: consul_key,
        local_content: @local[key],
        remote_content: @remote[key],
      )
    end
end

Public Instance Methods

any_changes?() click to toggle source
# File lib/constancy/diff.rb, line 106
def any_changes?
  self.items_to_change.count > 0
end
final_items() click to toggle source
# File lib/constancy/diff.rb, line 99
def final_items
  case @mode
  when :push then @local
  when :pull then @remote
  end
end
items_to_change() click to toggle source
# File lib/constancy/diff.rb, line 95
def items_to_change
  @diff.select { |d| [:delete, :update, :create].include?(d.op) }
end
items_to_create() click to toggle source
# File lib/constancy/diff.rb, line 79
def items_to_create
  @diff.select { |d| d.op == :create }
end
items_to_delete() click to toggle source
# File lib/constancy/diff.rb, line 71
def items_to_delete
  @diff.select { |d| d.op == :delete }
end
items_to_exclude() click to toggle source
# File lib/constancy/diff.rb, line 87
def items_to_exclude
  @diff.select { |d| d.op == :ignore and d.excluded == true }
end
items_to_ignore() click to toggle source
# File lib/constancy/diff.rb, line 83
def items_to_ignore
  @diff.select { |d| d.op == :ignore }
end
items_to_noop() click to toggle source
# File lib/constancy/diff.rb, line 91
def items_to_noop
  @diff.select { |d| d.op == :noop }
end
items_to_update() click to toggle source
# File lib/constancy/diff.rb, line 75
def items_to_update
  @diff.select { |d| d.op == :update }
end
print_report() click to toggle source