Sha256: c58d9fc982a9ec89b83b38eb95c7f89e4241a544a3054d3d9d90a050c0ec6541

Contents?: true

Size: 1.18 KB

Versions: 14

Compression:

Stored size: 1.18 KB

Contents

module Shamu
  module Attributes

    # Override equality methods to support shallow comparison of attribute
    # values for equality.
    #
    # Add `ignore_inequality: true` to any {Attributes::DSL#attribute} that
    # shouldn't be included in equalty comparisons.
    module Equality

      # @param [Attributes] other object to compare with.
      # @return [Boolean] true if the two objects are of the same type and
      #     attributes are all eql? to each other.
      def ==( other )
        return false unless other.is_a?( self.class ) || is_a?( other.class )
        attributes_eql?( other )
      end
      alias_method :eql?, :==

      # @return [Integer] a hash computed from the attributes of the object.
      def hash
        self.class.attributes.map do |key, _|
          send( key )
        end.hash
      end

      private

        # @return [Boolean] true if the object's attributes and `other`
        #     attributes are all `eql?` to each other.
        def attributes_eql?( other )
          self.class.attributes.all? do |key, attr|
            next true if attr[:ignore_inequality]
            send( key ).eql?( other.send( key ) )
          end
        end

    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
shamu-0.0.19 lib/shamu/attributes/equality.rb
shamu-0.0.18 lib/shamu/attributes/equality.rb
shamu-0.0.17 lib/shamu/attributes/equality.rb
shamu-0.0.15 lib/shamu/attributes/equality.rb
shamu-0.0.14 lib/shamu/attributes/equality.rb
shamu-0.0.13 lib/shamu/attributes/equality.rb
shamu-0.0.11 lib/shamu/attributes/equality.rb
shamu-0.0.9 lib/shamu/attributes/equality.rb
shamu-0.0.8 lib/shamu/attributes/equality.rb
shamu-0.0.7 lib/shamu/attributes/equality.rb
shamu-0.0.5 lib/shamu/attributes/equality.rb
shamu-0.0.4 lib/shamu/attributes/equality.rb
shamu-0.0.3 lib/shamu/attributes/equality.rb
shamu-0.0.2 lib/shamu/attributes/equality.rb