Sha256: 2d637452f5ab1d575067368ee6d83d67ed469b2cc1eb96975d1d308e6a85d198

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

module Domain
  class Equalizer < Module

    # Parts of this module have been extracted from Virtus, MIT Copyright (c) 2011-2012
    # Piotr Solnica.

    # Creates an Equalizer instance.
    #
    # @param [Proc] bl
    #   the proc to use to extract components that participate to equality
    #
    # @api private
    def initialize(components = nil, &bl)
      extractor = case components
                  when NilClass then bl
                  when Symbol   then proc{ [ send(components) ] }
                  when Array    then proc{ components.map{|n| send(n)} }
                  end
      define_method(:equality_components, &extractor)
      module_eval{ include(Methods) }
    end

    module Methods

      # Returns a hash code for the value
      #
      # @return Integer
      #
      # @api public
      def hash
        equality_components.map{|c| c.hash }.reduce(self.class.hash, :^)
      end

      # Compare the object with other object for equivalency
      #
      # @example
      #   object == other  # => true or false
      #
      # @param [Object] other
      #   the other object to compare with
      #
      # @return [Boolean]
      #
      # @api public
      def ==(other)
        return false unless self.class <=> other.class
        cmp?(__method__, other)
      end
      alias :eql? :==

    private

      def cmp?(comparator, other)
        equality_components.zip(other.equality_components).
                            all?{|l,r| l.send(comparator, r) }
      end

    end # module Methods
  end # class Equalizer
end # module Domain

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
domain-1.0.0.rc3 lib/domain/support/equalizer.rb