From f8c73201f46b0b8611c72e9e699aca924529676c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eloy=20Dur=C3=A1n?= Date: Tue, 15 Apr 2014 12:13:16 +0200 Subject: [PATCH] [Platform] Make instances usable as Hash keys. Fixes #109. --- CHANGELOG.md | 4 ++++ lib/cocoapods-core/platform.rb | 12 ++++++++++++ spec/platform_spec.rb | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b939f47ff..54044243b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ ##### Enhancements +* Make Platform instances usable as Hash keys. + [Eloy Durán](https://github.com/alloy) + [#109](https://github.com/CocoaPods/Core/pull/109) + * Accept new sources for Pods when they are just redirects of the old one. [Boris Bügling](https://github.com/neonichu) [#101](https://github.com/CocoaPods/Core/issues/101) diff --git a/lib/cocoapods-core/platform.rb b/lib/cocoapods-core/platform.rb index 9e2e62dcd..df814263e 100644 --- a/lib/cocoapods-core/platform.rb +++ b/lib/cocoapods-core/platform.rb @@ -86,6 +86,18 @@ def ==(other) end end + # (see #==) + alias_method :eql?, :== + + # Hashes the instance by the platform name and deployment target. + # + # This adds support to make instances usable as Hash keys. + # + # @!visibility private + def hash + name.hash ^ deployment_target.hash + end + # Checks whether a platform supports another one. # # In the context of operating system SDKs, a platform supports another diff --git a/spec/platform_spec.rb b/spec/platform_spec.rb index b16773d28..c32b2dde9 100644 --- a/spec/platform_spec.rb +++ b/spec/platform_spec.rb @@ -83,6 +83,18 @@ module Pod Platform.new(:ios, '5.0').requires_legacy_ios_archs?.should.be.false end + it 'is usable as hash keys' do + ios = Platform.new(:ios) + osx = Platform.new(:osx) + ios6 = Platform.new(:ios, '6.0') + ios61 = Platform.new(:ios, '6.1') + hash = { ios => ios, osx => osx, ios6 => ios6, ios61 => ios61 } + hash[Platform.new(:ios)].should.be.eql ios + hash[Platform.new(:osx)].should.be.eql osx + hash[Platform.new(:ios, '6.0')].should.be.eql ios6 + hash[Platform.new(:ios, '6.1')].should.be.eql ios61 + end + end describe 'Supporting other platforms' do