Sha256: da48a297f19dd9caf3f6a2ed37be7ce3b97e68bcdd2c2e2dc05b8391af3ad77d

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

require_relative './mixin/colorize'
require_relative './replace_properties'

module Convection
  module Model
    ##
    # Difference between an item in two templates
    ##
    class Diff
      include Comparable
      extend Mixin::Colorize

      attr_reader :key
      attr_accessor :action
      attr_reader :ours
      attr_reader :theirs
      colorize :action, :green => [:create], :yellow => [:update, :retain], :red => [:delete, :replace]

      def initialize(key, ours, theirs)
        @key = key
        @ours = ours
        @theirs = theirs

        @action = if ours && theirs
                    property_name = key[/AWS::[A-Za-z0-9:]+\.[A-Za-z0-9]+/]
                    if REPLACE_PROPERTIES.include?(property_name)
                      :replace
                    else
                      :update
                    end
                  elsif ours
                    :create
                  else
                    :delete
                  end
      end

      def to_thor
        message = case action
                  when :create then "#{ key }: #{ ours }"
                  when :update then "#{ key }: #{ theirs } => #{ ours }"
                  when :replace then "#{ key }: #{ theirs } => #{ ours }"
                  when :delete then key
                  when :retain then key
                  end

        [action, message, color]
      end

      def <=>(other)
        value = @key <=> other.key
        return value if value != 0

        value = @ours <=> other.ours
        return value if value != 0

        value = @theirs <=> other.theirs
        return value if value != 0

        @action <=> other.action
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
convection-2.3.1 lib/convection/model/diff.rb
convection-2.3.0 lib/convection/model/diff.rb