Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit.

  • Loading branch information...
commit fd8fef08c203240ce4f21fb08e50e46762fe31fa 0 parents
@ernie authored
17 .gitignore
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
3  .travis.yml
@@ -0,0 +1,3 @@
+rvm:
+ - 1.8.7
+ - 1.9.3
4 Gemfile
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in equivalence.gemspec
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Ernie Miller
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
136 README.md
@@ -0,0 +1,136 @@
+# Equivalence
+
+Because implementing object equality wasn't easy enough already.
+
+Do your objects recognize their equals? If you have complete control over how
+your objects are used, maybe you don't care. If you're writing code for others
+to reuse, though, your code might be leaving your users perplexed.
+
+Consider the following situation:
+
+ class Awesomeness
+ def initialize(level, description)
+ @level = level
+ @description = description
+ end
+
+ def declare_awesomeness
+ puts "My awesomeness level is #{@level} (#{@description})!"
+ end
+ end
+
+ awesome1 = Awesomeness.new(10, 'really awesome')
+ awesome2 = Awesomeness.new(10, 'really awesome')
+ awesome1.declare_awesomeness
+ # => "My awesomeness level is 10 (really awesome)!"
+ awesome2.declare_awesomeness
+ # => "My awesomeness level is 10 (really awesome)!"
+ [awesome1, awesome2].uniq.size # => 2
+ awesome1 == awesome2 # => false
+
+Surprised? You shouldn't be. Ruby's default implementation of object equality
+considers objects equal only if they are the same object, *not* if they have the
+same contents.
+
+This probably isn't what you want for your Awesomeness class. To get equality
+behaving as you'd expect, you need to do the following:
+
+ class Awesomeness
+ attr_reader :level, :description
+
+ def hash
+ [@level, @description].hash
+ end
+
+ def eql?(other)
+ self.class == other.class &&
+ self.level == other.level &&
+ self.description == other.description
+ end
+ alias :== :eql?
+ end
+
+Implementing the `==` method gets your comparison to return true, as expected,
+and implementing `hash` and `eql?` gets `Array#uniq` to behave as expected, and
+also lets you use your values as Hash keys in a way that works properly with
+`Hash#[]`, `Hash#[]=`, `Hash#merge` and the like.
+
+Have more instance variables? You'll need to add them to the `hash` and `eql?`
+methods. Have other custom objects as instance variables? They'll need to
+implement these methods, too.
+
+It can get to feel a lot like busy work, and let's face it, if we liked doing
+busy work, we'd be using Java.
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'equivalence'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install equivalence
+
+## Usage
+
+### Basic
+
+ class MySpiffyClass
+ extend Equivalence
+ equivalence :@my, :@instance, :@variables # , [...]
+ # Your spiffy class implementation
+ end
+
+You'll get the equality methods we "painstakingly" added above, without all that
+pesky typing. If you don't implement reader methods (as above), Equivalence will
+create some for you, with `protected` access (meaning only other objects within
+MySpiffyClass's class hierarchy will be able to call them), since they're
+necessary for the `eql?` method to work). Defining your own readers? No problem,
+Equivalence won't mess with them.
+
+Let's re-visit the example from above.
+
+ class Awesomeness
+ extend Equivalence
+ equivalence :@level, :@description
+
+ def initialize(level, description)
+ @level = level
+ @description = description
+ end
+
+ def declare_awesomeness
+ puts "My awesomeness level is #{@level} (#{@description})!"
+ end
+ end
+
+ awesome1 = Awesomeness.new(10, 'really awesome')
+ awesome2 = Awesomeness.new(10, 'really awesome')
+ [awesome1, awesome2].uniq.size # => 1
+ awesome1 == awesome2 # => true
+
+Less hassle, same result.
+
+### "Advanced" (if there is such a thing, for such a simple library)
+
+Maybe your attribute readers aren't named the same as your instance variables,
+because you like to confuse people. Or maybe, your readers are lazy-loading
+certain instance variables or doing some casting of Fixnums to Strings. In that
+case, you'll want your `hash` method to be defined with calls to the methods
+instead of accessing the ivars directly, to get the expected results. Just omit
+the leading @ in each parameter, like so:
+
+ equivalence :level, :description
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
9 Rakefile
@@ -0,0 +1,9 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new(:spec) do |rspec|
+ rspec.rspec_opts = ['--backtrace']
+end
+
+task :default => :spec
19 equivalence.gemspec
@@ -0,0 +1,19 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/equivalence/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["Ernie Miller"]
+ gem.email = ["ernie@erniemiller.org"]
+ gem.description = %q{Implement object equality by extending a module and calling a macro. Now you have no excuse for not doing it.}
+ gem.summary = %q{Because implementing object equality wasn't easy enough already.}
+ gem.homepage = "http://github.com/ernie/equivalence"
+
+ gem.add_development_dependency 'rspec', '~> 2.11.0'
+
+ gem.files = `git ls-files`.split($\)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.name = "equivalence"
+ gem.require_paths = ["lib"]
+ gem.version = Equivalence::VERSION
+end
50 lib/equivalence.rb
@@ -0,0 +1,50 @@
+require "equivalence/version"
+
+module Equivalence
+
+ private
+
+ def equivalence(*args)
+ raise ArgumentError, 'At least one attribute is required.' if args.empty?
+ method_names = args.map { |arg| arg.to_s.sub /^@/, '' }
+
+ __define_equivalence_hash_method(args)
+ __define_equivalence_attribute_readers(method_names)
+ __define_equivalence_equality_methods(method_names)
+ end
+
+ def __define_equivalence_hash_method(ivar_or_method_names)
+ # Method names might be keywords. We'll want to prefix them with "self"
+ ivar_or_method_names = ivar_or_method_names.map do |name|
+ name.to_s[0] == '@' ? name : "self.#{name}"
+ end
+
+ class_eval <<-EVAL, __FILE__, __LINE__
+ def hash
+ [#{ivar_or_method_names.join(', ')}].hash
+ end
+ EVAL
+ end
+
+ def __define_equivalence_attribute_readers(method_names)
+ method_names.each do |method|
+ unless method_defined?(method)
+ class_eval <<-EVAL, __FILE__, __LINE__
+ attr_reader :#{method} unless private_method_defined?(:#{method})
+ protected :#{method}
+ EVAL
+ end
+ end
+ end
+
+ def __define_equivalence_equality_methods(method_names)
+ class_eval <<-EVAL, __FILE__, __LINE__
+ def eql?(other)
+ self.class == other.class &&
+ #{method_names.map {|m| "self.#{m} == other.#{m}"}.join(" &&\n")}
+ end
+ alias :== :eql?
+ EVAL
+ end
+
+end
3  lib/equivalence/version.rb
@@ -0,0 +1,3 @@
+module Equivalence
+ VERSION = "1.0.0.pre"
+end
103 spec/equivalence/equivalence_spec.rb
@@ -0,0 +1,103 @@
+require 'spec_helper'
+
+describe Equivalence do
+
+ it 'requires at least one attribute as an argument' do
+ expect {
+ klass = Class.new do
+ extend Equivalence
+ equivalence
+ end
+ }.to raise_error ArgumentError
+ end
+
+ it 'accepts method names as arguments' do
+ klass = Class.new do
+ extend Equivalence
+ attr_accessor :var1, :var2
+ equivalence :var1, :var2
+ end
+ k1 = klass.new
+ k1.var1 = 1
+ k1.var2 = 2
+ k2 = klass.new
+ k2.var1 = 1
+ k2.var2 = 2
+ [k1, k2].uniq.should have(1).item
+ end
+
+ it 'accepts instance variable names as arguments' do
+ klass = Class.new do
+ extend Equivalence
+ attr_accessor :var1, :var2
+ equivalence :var1, :var2
+ end
+ k1 = klass.new
+ k1.var1 = 1
+ k1.var2 = 2
+ k2 = klass.new
+ k2.var1 = 1
+ k2.var2 = 2
+ [k1, k2].uniq.should have(1).item
+ end
+
+ it 'creates a valid hash method if a keyword is used' do
+ klass = Class.new do
+ extend Equivalence
+ attr_accessor :alias
+ equivalence :alias
+ end
+ k1 = klass.new
+ k1.alias = 'bob'
+ k2 = klass.new
+ k2.alias = 'bob'
+ [k1, k2].uniq.should have(1).item
+ end
+
+ it 'defines protected attribute readers if not already defined' do
+ klass = Class.new do
+ extend Equivalence
+ equivalence :@var
+ def initialize(var)
+ @var = var
+ end
+ end
+ klass.protected_method_defined?(:var).should be_true
+ end
+
+ it 'does not alter access of already-accessible methods' do
+ klass = Class.new do
+ extend Equivalence
+ attr_reader :var
+ equivalence :@var
+ def initialize(var)
+ @var = var
+ end
+ end
+ klass.public_method_defined?(:var).should be_true
+ klass.protected_method_defined?(:var).should be_false
+ end
+
+ it 'does not overwrite a private reader method, but makes it protected' do
+ # Not that it's likely that you're going to call equivalence in the order
+ # shown here. Still, better safe than sorry. What you do *after* you call
+ # equivalence is your problem, but we don't want to "unexpectedly" overwrite
+ # anything.
+ klass = Class.new do
+ extend Equivalence
+ def initialize(var)
+ @var = var
+ end
+ private
+ def var
+ 'zomg'
+ end
+ equivalence :@var
+ end
+ klass.protected_method_defined?(:var).should be_true
+ klass.private_method_defined?(:var).should be_false
+ klass.new(1).send(:var).should eq 'zomg'
+ end
+
+end
+
1  spec/spec_helper.rb
@@ -0,0 +1 @@
+require 'equivalence'
Please sign in to comment.
Something went wrong with that request. Please try again.