Sha256: 7c113c38d6904d3ea3d33046aab56ecc24028dbc402d4c7a931a5bf56ff1cf09

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

# :stopdoc:
# this shims the Hash functionality to ensure we have an ordered hash guarantee
module Archetype
  if RUBY_VERSION < '1.9'
    require 'hashery/ordered_hash'
    class Hash < Hashery::OrderedHash
      include Archetype::Functions::Hash

      # make select behave like its 1.9+ counterpart
      def select
        hsh = ::Archetype::Hash.new
        self.each do |key, value|
          hsh[key] = value if yield(key, value)
        end
        return hsh
      end

      # make select! behave like its 1.9+ counterpart
      def select!
        self.each do |key, value|
          self.delete[key] unless yield(key, value)
        end
        return self
      end

      # make reject behave like its 1.9+ counterpart
      def reject
        hsh = ::Archetype::Hash.new
        self.each do |key, value|
          hsh[key] = value unless yield(key, value)
        end
        return hsh
      end

      # make reject! behave like its 1.9+ counterpart
      def reject!
        self.each do |key, value|
          self.delete[key] if yield(key, value)
        end
        return self
      end
    end
  else
    class Hash < ::Hash
      include Archetype::Functions::Hash
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
archetype-1.0.0.alpha.5 lib/archetype/functions/hash/shim.rb
archetype-1.0.0.alpha.4 lib/archetype/functions/hash/shim.rb
archetype-1.0.0.alpha.3 lib/archetype/functions/hash/shim.rb
archetype-1.0.0.alpha.2 lib/archetype/functions/hash/shim.rb
archetype-1.0.0.alpha.1 lib/archetype/functions/hash/shim.rb