equatable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.sw*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ -f s
data/.rvmrc ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env bash
2
+
3
+ environment_id="ruby-1.9.3-p0@equatable"
4
+
5
+ #
6
+ # First we attempt to load the desired environment directly from the environment
7
+ # file. This is very fast and efficient compared to running through the entire
8
+ # CLI and selector. If you want feedback on which environment was used then
9
+ # insert the word 'use' after --create as this triggers verbose mode.
10
+ #
11
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
12
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
13
+ then
14
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
15
+
16
+ if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
17
+ then
18
+ . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
19
+ fi
20
+ else
21
+ # If the environment file has not yet been created, use the RVM CLI to select.
22
+ if ! rvm --create "$environment_id"
23
+ then
24
+ echo "Failed to create RVM environment '${environment_id}'."
25
+ return 1
26
+ fi
27
+ fi
28
+
29
+ if [[ $- == *i* ]] # check for interactive shells
30
+ then
31
+ echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
32
+ else
33
+ echo "Using: $GEM_HOME" # don't use colors in interactive shells
34
+ fi
@@ -0,0 +1,20 @@
1
+ language: ruby
2
+ before_install:
3
+ - gem install bundler
4
+ rvm:
5
+ - 1.8.7
6
+ - 1.9.2
7
+ - 1.9.3
8
+ - ruby-head
9
+ - jruby-18mode
10
+ - jruby-19mode
11
+ - rbx-18mode
12
+ - rbx-19mode
13
+ - ree
14
+ - jruby-head
15
+ matrix:
16
+ allow_failures:
17
+ - rvm: ruby-head
18
+ - rvm: jruby-head
19
+ - rvm: jruby-19mode
20
+ - rvm: rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Piotr Murach
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,66 @@
1
+ # Equatable
2
+ [![Build Status](https://secure.travis-ci.org/peter-murach/equatable.png?branch=master)][travis] [![Code Climate](https://codeclimate.com/badge.png)][codeclimate]
3
+
4
+ [travis]: http://travis-ci.org/peter-murach/equatable
5
+ [codeclimate]: https://codeclimate.com/github/peter-murach/equatable
6
+
7
+ Allows ruby objects to implement equality comparison and inspection methods.
8
+
9
+ By including this module, a class indicates that its instances have explicit general contracts for `hash`, `==` and `eql?` methods. Specifically `eql?` contract requires that it implements an equivalence relation. By default each instance of the class is equal only to itself. This is a right behaviour when you have distinct objects. Howerver, it is the responsibility of any class to clearly define their equality. Failure to do so may prevent instances to behave as expected when for instance `Array#uniq` is invoked or when they are used as `Hash` keys.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'equatable'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install equatable
24
+
25
+ ## Usage
26
+
27
+ It is assumed that your objects are value objects and the only values that affect equality comparison are the ones specified by your attribute readers. Each attribute reader should be a significant field in determining objects values.
28
+
29
+ ```ruby
30
+ class Value
31
+ include Equatable
32
+
33
+ attr_reader :value
34
+
35
+ def initialize(value)
36
+ @value = value
37
+ end
38
+ end
39
+
40
+ val1 = Value.new(11)
41
+ val2 = Value.new(11)
42
+ val3 = Value.new(13)
43
+
44
+ val1 == val2 # => true
45
+ val1.hash == val2.hash # => true
46
+ val1 eql? val2 # => true
47
+
48
+ val1 == val3 # => false
49
+ val1.hash == val3.hash # => false
50
+ val1 eql? val3 # => false
51
+
52
+ ```
53
+
54
+ It is important that the attribute readers should allow for performing deterministic computations on class instances.
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create new Pull Request
63
+
64
+ ## Copyright
65
+
66
+ Copyright (c) 2012 Piotr Murach. See LICENSE for further details.
@@ -0,0 +1,12 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.unshift File.expand_path('../lib', __FILE__)
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = FileList['spec/**/*_spec.rb']
8
+ end
9
+
10
+ FileList['tasks/**/*.rake'].each { |task| import task }
11
+
12
+ task :default => [:spec]
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'equatable/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "equatable"
8
+ gem.version = Equatable::VERSION
9
+ gem.authors = ["Piotr Murach"]
10
+ gem.email = [""]
11
+ gem.description = %q{Allows ruby objects to implement equality comparison and inspection methods. By including this module, a class indicates that its instances have explicit general contracts for `hash`, `==` and `eql?` methods.}
12
+ gem.summary = %q{Allows ruby objects to implement equality comparison and inspection methods.}
13
+ gem.homepage = "http://github.com/peter-murach/equatable"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'rspec'
21
+ gem.add_development_dependency 'rake'
22
+ gem.add_development_dependency 'yard'
23
+ end
@@ -0,0 +1,150 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require "equatable/version"
4
+
5
+ # Make it easy to define equality and hash methods.
6
+ module Equatable
7
+
8
+ # Hook into module inclusion.
9
+ #
10
+ # @param [Module] base
11
+ # the module or class including Equatable
12
+ #
13
+ # @return [self]
14
+ #
15
+ # @api private
16
+ def self.included(base)
17
+ super
18
+ base.extend(self)
19
+ base.class_eval do
20
+ define_comparison_attrs
21
+ include Methods
22
+ define_methods
23
+ end
24
+ self
25
+ end
26
+
27
+ # Holds all attributes used for comparison.
28
+ #
29
+ # @return [Array<Symbol>]
30
+ #
31
+ # @api private
32
+ attr_reader :comparison_attrs
33
+
34
+ # Objects that include this module are assumed to be value objects.
35
+ # It is also assumed that the only values that affect the results of
36
+ # equality comparison are the values of the object's attributes.
37
+ #
38
+ # @param [Array<Symbol>] *args
39
+ #
40
+ # @return [undefined]
41
+ #
42
+ # @api public
43
+ def attr_reader(*args)
44
+ super
45
+ @comparison_attrs.concat(args)
46
+ end
47
+
48
+ # Copy the comparison_attrs into the subclass.
49
+ #
50
+ # @param [Class] subclass
51
+ #
52
+ # @api private
53
+ def inherited(subclass)
54
+ super
55
+ subclass.instance_variable_set(:@comparison_attrs, comparison_attrs.dup)
56
+ end
57
+
58
+ private
59
+
60
+ # Define class instance #comparison_attrs as an empty array.
61
+ #
62
+ # @return [undefined]
63
+ #
64
+ # @api private
65
+ def define_comparison_attrs
66
+ instance_variable_set('@comparison_attrs', [])
67
+ end
68
+
69
+ # Define all methods needed for ensuring object's equality.
70
+ #
71
+ # @return [undefined]
72
+ #
73
+ # @api private
74
+ def define_methods
75
+ define_compare
76
+ define_hash
77
+ define_inspect
78
+ end
79
+
80
+ # Define a #compare? method to check if the receiver is the same
81
+ # as the other object.
82
+ #
83
+ # @return [undefined]
84
+ #
85
+ # @api private
86
+ def define_compare
87
+ define_method(:compare?) do |comparator, other|
88
+ attrs = comparison_attrs || []
89
+ !attrs.find { |attr| send(attr).send(comparator, other.send(attr)) }
90
+ end
91
+ end
92
+
93
+ # Define a hash method that ensures that the hash value is the same for
94
+ # the same instance attributes and their corresponding values.
95
+ #
96
+ # @api private
97
+ def define_hash
98
+ define_method(:hash) do
99
+ klass = self.class
100
+ attrs = klass.comparison_attrs || []
101
+ ([klass] + attrs.map { |attr| send(attr)}).hash
102
+ end
103
+ end
104
+
105
+ # Define an inspect method that shows the class name and the values for the
106
+ # instance's attributes.
107
+ #
108
+ # @return [undefined]
109
+ #
110
+ # @api private
111
+ def define_inspect
112
+ define_method(:inspect) do
113
+ klass = self.class
114
+ name = klass.name || klass.inspect
115
+ attrs = klass.comparison_attrs || []
116
+ "#<#{name}#{attrs.map { |attr| " #{attr}=#{send(attr).inspect}" }.join}>"
117
+ end
118
+ end
119
+
120
+ module Methods
121
+
122
+ # Compare two objects for equality based on their value
123
+ # and being an instance of the given class.
124
+ #
125
+ # @param [Object] other
126
+ # the other object in comparison
127
+ #
128
+ # @return [Boolean]
129
+ #
130
+ # @api public
131
+ def eql?(other)
132
+ instance_of?(other.class) and compare?(__method__, other)
133
+ end
134
+
135
+ # Compare two objects for equality based on their value
136
+ # and being a subclass of the given class.
137
+ #
138
+ # @param [Object] other
139
+ # the other object in comparison
140
+ #
141
+ # @return [Boolean]
142
+ #
143
+ # @api public
144
+ def ==(other)
145
+ return false unless self.class <=> other.class
146
+ compare?(__method__, other)
147
+ end
148
+
149
+ end # Methods
150
+ end # Equatable
@@ -0,0 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Equatable
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,206 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Equatable do
6
+ let(:name) { 'Value' }
7
+
8
+ context 'without attributes' do
9
+ let(:klass) { ::Class.new }
10
+
11
+ subject { klass.new }
12
+
13
+ before {
14
+ klass.stub(:name).and_return 'Value'
15
+ klass.send :include, described_class
16
+ }
17
+
18
+ it { should respond_to :compare? }
19
+
20
+ it { should be_instance_of klass }
21
+
22
+ describe '#eql?' do
23
+ context 'when objects are similar' do
24
+ let(:other) { subject.dup }
25
+
26
+ it { subject.eql?(other).should be_true }
27
+ end
28
+
29
+ context 'when objects are different' do
30
+ let(:other) { stub('other') }
31
+
32
+ it { subject.eql?(other).should be_false }
33
+ end
34
+ end
35
+
36
+ describe '#==' do
37
+ context 'when objects are similar' do
38
+ let(:other) { subject.dup }
39
+
40
+ it { (subject == other).should be_true }
41
+ end
42
+
43
+ context 'when objects are different' do
44
+ let(:other) { stub('other') }
45
+
46
+ it { (subject == other)}
47
+ end
48
+ end
49
+
50
+ describe '#inspect' do
51
+ it { subject.inspect.should eql('#<Value>') }
52
+ end
53
+
54
+ describe '#hash' do
55
+ it { subject.hash.should eql([klass].hash) }
56
+ end
57
+
58
+
59
+ end
60
+
61
+ context 'with attributes' do
62
+ let(:value) { 11 }
63
+ let(:klass) {
64
+ ::Class.new do
65
+ include Equatable
66
+
67
+ attr_reader :value
68
+
69
+ def initialize(value)
70
+ @value = value
71
+ end
72
+ end
73
+ }
74
+
75
+ before {
76
+ klass.stub(:name).and_return name
77
+ }
78
+
79
+ subject { klass.new(value) }
80
+
81
+ it 'dynamically defines #hash method' do
82
+ klass.method_defined?(:hash).should be_true
83
+ end
84
+
85
+ it 'dynamically defines #inspect method' do
86
+ klass.method_defined?(:inspect).should be_true
87
+ end
88
+
89
+ it { should respond_to :compare? }
90
+
91
+ it { should respond_to :eql? }
92
+
93
+ describe '#eql?' do
94
+ context 'when objects are similar' do
95
+ let(:other) { subject.dup }
96
+
97
+ it { subject.eql?(other).should be_true }
98
+ end
99
+
100
+ context 'when objects are different' do
101
+ let(:other) { stub('other') }
102
+
103
+ it { subject.eql?(other).should be_false }
104
+ end
105
+ end
106
+
107
+ describe '#==' do
108
+ context 'when objects are similar' do
109
+ let(:other) { subject.dup }
110
+
111
+ it { (subject == other).should be_true }
112
+ end
113
+
114
+ context 'when objects are different' do
115
+ let(:other) { stub('other') }
116
+
117
+ it { (subject == other).should be_false }
118
+ end
119
+ end
120
+
121
+ describe '#inspect' do
122
+ it { subject.inspect.should eql('#<Value value=11>') }
123
+ end
124
+
125
+ describe '#hash' do
126
+ it { subject.hash.should eql( ([klass] + [value]).hash) }
127
+ end
128
+
129
+ context 'equivalence relation' do
130
+ let(:other) { subject.dup }
131
+
132
+ it 'is not equal to nil reference' do
133
+ subject.eql?(nil).should be_false
134
+ end
135
+
136
+ it 'is reflexive' do
137
+ subject.eql?(subject).should be_true
138
+ end
139
+
140
+ it 'is symmetric' do
141
+ (subject.eql?(other)).should eql( other.eql?(subject) )
142
+ end
143
+
144
+ it 'is transitive'
145
+ end
146
+ end
147
+
148
+ context 'subclass' do
149
+ let(:value) { 11 }
150
+ let(:klass) {
151
+ ::Class.new do
152
+ include Equatable
153
+
154
+ attr_reader :value
155
+
156
+ def initialize(value)
157
+ @value = value
158
+ end
159
+ end
160
+ }
161
+ let(:subclass) { ::Class.new(klass) }
162
+
163
+ before {
164
+ klass.stub(:name).and_return name
165
+ }
166
+
167
+ subject { subclass.new(value) }
168
+
169
+ it { subclass.superclass.should == klass }
170
+
171
+ it { should respond_to :value }
172
+
173
+ describe '#inspect' do
174
+ it { subject.inspect.should eql('#<Value value=11>') }
175
+ end
176
+
177
+ describe '#eql?' do
178
+ context 'when objects are similar' do
179
+ let(:other) { subject.dup }
180
+
181
+ it { subject.eql?(other).should be_true }
182
+ end
183
+
184
+ context 'when objects are different' do
185
+ let(:other) { stub('other') }
186
+
187
+ it { subject.eql?(other).should be_false }
188
+ end
189
+ end
190
+
191
+ describe '#==' do
192
+ context 'when objects are similar' do
193
+ let(:other) { subject.dup }
194
+
195
+ it { (subject == other).should be_true }
196
+ end
197
+
198
+ context 'when objects are different' do
199
+ let(:other) { stub('other') }
200
+
201
+ it { (subject == other)}
202
+ end
203
+ end
204
+ end
205
+
206
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+
6
+ require 'rspec'
7
+ require 'equatable'
8
+
9
+ RSpec.configure do |config|
10
+ config.order = :rand
11
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: equatable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Piotr Murach
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2152488300 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2152488300
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &2152487800 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2152487800
36
+ - !ruby/object:Gem::Dependency
37
+ name: yard
38
+ requirement: &2152487380 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2152487380
47
+ description: Allows ruby objects to implement equality comparison and inspection methods.
48
+ By including this module, a class indicates that its instances have explicit general
49
+ contracts for `hash`, `==` and `eql?` methods.
50
+ email:
51
+ - ''
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - .gitignore
57
+ - .rspec
58
+ - .rvmrc
59
+ - .travis.yml
60
+ - Gemfile
61
+ - LICENSE.txt
62
+ - README.md
63
+ - Rakefile
64
+ - equatable.gemspec
65
+ - lib/equatable.rb
66
+ - lib/equatable/version.rb
67
+ - spec/equatable/eql_spec.rb
68
+ - spec/spec_helper.rb
69
+ homepage: http://github.com/peter-murach/equatable
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.10
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Allows ruby objects to implement equality comparison and inspection methods.
93
+ test_files:
94
+ - spec/equatable/eql_spec.rb
95
+ - spec/spec_helper.rb
96
+ has_rdoc: