ivar_equatable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6b276b45a3ef1e65428e5748139a67361eb10d1dd1275c4a7907945f171202ca
4
+ data.tar.gz: 9561022fa9dd03dbc7b01ffb91e168aaaafb4c041ded1baff13e26515882fe6e
5
+ SHA512:
6
+ metadata.gz: a08b829d40cbd66599cd8e471153e735493e855ab755980531fe03006cdf92a8eb5ec7b32853e9859c3ef37c43ba173c33d14ccc41504f626cb74da1ee602c0c
7
+ data.tar.gz: 4cb9b95f370cffbeab8cf99631c50639cd89692c310a2414f15b0a67b418bcd6e96249502ddb8e9524d0d24bf6d03903768b85afa1ac197f5dea18d8ef6adeed
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ inherit_gem:
2
+ rubocop-espago: rubocop.yml
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.7
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-02-21
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in diggable.gemspec
6
+ gemspec
7
+
8
+ gem 'minitest', '~> 5.0' # test framework
9
+ gem 'rake', '~> 13.0' # automation tasks
10
+ gem 'rubocop-espago', '~> 1.0' # ruby linter
11
+ gem 'shoulda-context', '~> 2.0' # more pleasant test syntax
12
+ gem 'solargraph', '~> 0.48' # language server
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ivar_equatable (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ backport (1.2.0)
11
+ benchmark (0.2.1)
12
+ diff-lcs (1.5.0)
13
+ e2mmap (0.1.0)
14
+ jaro_winkler (1.5.4)
15
+ json (2.6.3)
16
+ kramdown (2.4.0)
17
+ rexml
18
+ kramdown-parser-gfm (1.1.0)
19
+ kramdown (~> 2.0)
20
+ minitest (5.17.0)
21
+ nokogiri (1.14.2-arm64-darwin)
22
+ racc (~> 1.4)
23
+ nokogiri (1.14.2-x86_64-linux)
24
+ racc (~> 1.4)
25
+ parallel (1.22.1)
26
+ parser (3.2.1.0)
27
+ ast (~> 2.4.1)
28
+ racc (1.6.2)
29
+ rainbow (3.1.1)
30
+ rake (13.0.6)
31
+ regexp_parser (2.7.0)
32
+ reverse_markdown (2.1.1)
33
+ nokogiri
34
+ rexml (3.2.5)
35
+ rubocop (1.45.1)
36
+ json (~> 2.3)
37
+ parallel (~> 1.10)
38
+ parser (>= 3.2.0.0)
39
+ rainbow (>= 2.2.2, < 4.0)
40
+ regexp_parser (>= 1.8, < 3.0)
41
+ rexml (>= 3.2.5, < 4.0)
42
+ rubocop-ast (>= 1.24.1, < 2.0)
43
+ ruby-progressbar (~> 1.7)
44
+ unicode-display_width (>= 2.4.0, < 3.0)
45
+ rubocop-ast (1.26.0)
46
+ parser (>= 3.2.1.0)
47
+ rubocop-espago (1.0.2)
48
+ rubocop
49
+ ruby-progressbar (1.11.0)
50
+ shoulda-context (2.0.0)
51
+ solargraph (0.48.0)
52
+ backport (~> 1.2)
53
+ benchmark
54
+ bundler (>= 1.17.2)
55
+ diff-lcs (~> 1.4)
56
+ e2mmap
57
+ jaro_winkler (~> 1.5)
58
+ kramdown (~> 2.3)
59
+ kramdown-parser-gfm (~> 1.1)
60
+ parser (~> 3.0)
61
+ reverse_markdown (>= 1.0.5, < 3)
62
+ rubocop (>= 0.52)
63
+ thor (~> 1.0)
64
+ tilt (~> 2.0)
65
+ yard (~> 0.9, >= 0.9.24)
66
+ thor (1.2.1)
67
+ tilt (2.1.0)
68
+ unicode-display_width (2.4.2)
69
+ webrick (1.7.0)
70
+ yard (0.9.28)
71
+ webrick (~> 1.7.0)
72
+
73
+ PLATFORMS
74
+ arm64-darwin-20
75
+ x86_64-linux
76
+
77
+ DEPENDENCIES
78
+ ivar_equatable!
79
+ minitest (~> 5.0)
80
+ rake (~> 13.0)
81
+ rubocop-espago (~> 1.0)
82
+ shoulda-context (~> 2.0)
83
+ solargraph (~> 0.48)
84
+
85
+ BUNDLED WITH
86
+ 2.4.7
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Mateusz Drewniak
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # IvarEquatable
2
+
3
+ This Ruby gem which adds a module which when included to a class enables its instances to be compared based on their instance variables.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add ivar_equatable
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install ivar_equatable
14
+
15
+ ## Usage
16
+
17
+ Include the `IvarEquatable` module in your class to make its
18
+ instances capable of comparing themselves with the `==` and `eql?` methods.
19
+
20
+ In order for two objects to be equal, they've got to have
21
+ the same instance variables, and the values stored in them have to return
22
+ `true` when compared using the `==` operator.
23
+
24
+ ```rb
25
+ class MyClass
26
+ include IvarEquatable
27
+
28
+ attr_accessor :foo, :bar
29
+
30
+ def initialize(foo: nil, bar: nil)
31
+ @foo = foo
32
+ @bar = bar
33
+ end
34
+ end
35
+
36
+ obj1 = MyClass.new foo: :some_value
37
+ obj2 = MyClass.new foo: :some_value
38
+ obj1 == obj2 #=> true
39
+
40
+ obj1 = MyClass.new foo: :some_value
41
+ obj2 = MyClass.new foo: :other_value
42
+ obj1 == obj2 #=> false
43
+
44
+ obj1 = MyClass.new foo: :some_value
45
+ obj2 = MyClass.new foo: :some_value, bar: 2
46
+ obj1 == obj2 #=> false
47
+
48
+ obj1 = MyClass.new foo: { some: { nested: [:hash, :and, :array] } }
49
+ obj2 = MyClass.new foo: { some: { nested: [:hash, :and, :array] } }
50
+ obj1 == obj2 #=> true
51
+
52
+ obj1 = MyClass.new foo: { some: { nested: [:hash, :and, :array] } }
53
+ obj2 = MyClass.new foo: { some: { nested: :different } }
54
+ obj1 == obj2 #=> false
55
+
56
+ # works on subclasses
57
+ class ChildClass < MyClass; end
58
+
59
+ obj1 = MyClass.new foo: :some_value
60
+ obj2 = ChildClass.new foo: :some_value
61
+ obj1 == obj2 #=> true
62
+
63
+ # comparing instances of unrelated classes always returns `false`
64
+ class NonRelatedClass
65
+ include IvarEquatable
66
+
67
+ attr_accessor :foo, :bar
68
+
69
+ def initialize(foo: nil, bar: nil)
70
+ @foo = foo
71
+ @bar = bar
72
+ end
73
+ end
74
+
75
+ obj1 = MyClass.new foo: :some_value
76
+ obj2 = NonRelatedClass.new foo: :some_value
77
+ obj1 == obj2 #=> false
78
+ ```
79
+
80
+ ## Development
81
+
82
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
83
+
84
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
85
+
86
+ ## Contributing
87
+
88
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Verseth/ivar_equatable.
89
+
90
+ ## License
91
+
92
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ ::Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = ::FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ require 'rubocop/rake_task'
13
+
14
+ ::RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IvarEquatable
4
+ # @return [String]
5
+ VERSION = '0.1.0'
6
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+
5
+ require_relative 'ivar_equatable/version'
6
+
7
+ # Include in a class to make its instances capable
8
+ # of comparing themselves with other objects of the same class
9
+ # by calling `==` on their instance variables.
10
+ #
11
+ # Example:
12
+ #
13
+ # class MyClass
14
+ # include IvarEquatable
15
+ #
16
+ # attr_accessor :foo, :bar
17
+ #
18
+ # def initialize(foo: nil, bar: nil)
19
+ # @foo = foo
20
+ # @bar = bar
21
+ # end
22
+ # end
23
+ #
24
+ # obj1 = MyClass.new foo: :some_value
25
+ # obj2 = MyClass.new foo: :some_value
26
+ # obj1 == obj2 #=> true
27
+ #
28
+ # obj1 = MyClass.new foo: :some_value
29
+ # obj2 = MyClass.new foo: :other_value
30
+ # obj1 == obj2 #=> false
31
+ #
32
+ # obj1 = MyClass.new foo: :some_value
33
+ # obj2 = MyClass.new foo: :some_value, bar: 2
34
+ # obj1 == obj2 #=> false
35
+ #
36
+ module IvarEquatable
37
+ # @param other [Object]
38
+ # @return [Boolean]
39
+ def eql?(other)
40
+ return true if equal?(other)
41
+ return false unless other.is_a?(self.class) || is_a?(other.class)
42
+
43
+ # @type [Set<Symbol>]
44
+ self_ivars = instance_variables.to_set
45
+ # @type [Set<Symbol>]
46
+ other_ivars = other.instance_variables.to_set
47
+
48
+ return false unless self_ivars == other_ivars
49
+
50
+ self_ivars.each do |ivar|
51
+ return false if instance_variable_get(ivar) != other.instance_variable_get(ivar)
52
+ end
53
+
54
+ true
55
+ end
56
+
57
+ alias == eql?
58
+ end
@@ -0,0 +1,4 @@
1
+ module IvarEquatable
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ivar_equatable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mateusz Drewniak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Ruby gem which adds a module which when includedin a class enables
14
+ its instances to be compared based on their instance variables.
15
+ email:
16
+ - matmg24@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE
26
+ - README.md
27
+ - Rakefile
28
+ - lib/ivar_equatable.rb
29
+ - lib/ivar_equatable/version.rb
30
+ - sig/ivar_equatable.rbs
31
+ homepage: https://github.com/Verseth/ruby-ivar_equatable
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ homepage_uri: https://github.com/Verseth/ruby-ivar_equatable
36
+ source_code_uri: https://github.com/Verseth/ruby-ivar_equatable
37
+ rubygems_mfa_required: 'true'
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.7.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.4.7
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: A Ruby gem which adds a module which when includedin a class enables its
57
+ instances to be compared based on their instance variables.
58
+ test_files: []