delight 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 33be3483fc364798e5ab551821b8f4404709c195eaddad0dc6373b9be7b92296
4
- data.tar.gz: 38e642bd941c1e1292dff9160aa23fabdf5cdfa499d9e81f1769acbed84469f4
3
+ metadata.gz: eb589267629b62a9906d523d22ecf83c034f1df0ce9bf301079891a537d9a688
4
+ data.tar.gz: 1af7bc10bb90205a4127f9aecb936b677e23d561b5a0d96e0785a181aa554ca8
5
5
  SHA512:
6
- metadata.gz: 205d6c36bfe8c7cfd8aafb1be617cbad488f4aff7b257a5f5a5dcb5ec05bd8a9f8e61e5613a212191531c65b7f71d28a26d8adc585dd4c1cdd35f8e20894f168
7
- data.tar.gz: 240ffb9fc46657aa2e9f8a695d06d0ca5976de1c82486c16b5238dd4a77b045e70b46c0391bae02772ca1ba1ba2779117f264c937be88f1b7ecbafc75b44529f
6
+ metadata.gz: fc015857edf6623769c2af429ae7a4308b243b24bd4ad8f6516f7ed885007280615c56119fb12bf43d3323c3d3b56c801341e492d6fbc3c092f75ecba97fb25b
7
+ data.tar.gz: bceac43c952c822ba667faa8f16157108699487f50e006add390bf4e4e059869cf1807b70bad44900db64559cf9312d90069a2704adced962484b10196170351
data/README.md CHANGED
@@ -1,43 +1,72 @@
1
1
  # Delight
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ Delight is a Ruby gem offering small yet useful extensions to the core Ruby classes. Those extensions are implemented as Refinements, which means they are only available in the scope where they are explicitly activated. This allows you to use them without worrying about polluting the global namespace or causing conflicts with other gems.
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/delight`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ ## [Installation](Installation)
6
6
 
7
- ## Installation
7
+ ```bash
8
+ bundle add delight
9
+ ```
8
10
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
11
+ ## Usage
10
12
 
11
- Install the gem and add to the application's Gemfile by executing:
13
+ In order to import all available methods use the global `Delight::Enumerable` refinement.
12
14
 
13
- ```bash
14
- bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```ruby
16
+ class MyClass
17
+ using Delight::Enumerable
18
+
19
+ def initialize(some_list)
20
+ @some_list = some_list
21
+ end
22
+
23
+ def my_method
24
+ some_list.select_by(country: "PL")
25
+ end
26
+ end
15
27
  ```
16
28
 
17
- If bundler is not being used to manage dependencies, install the gem by executing:
29
+ ### `.select_by`
18
30
 
19
- ```bash
20
- gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
31
+ The `.select_by` method allows you to filter an array of objects based on a
32
+ value(s) of single or multiple methods. Object in the collection must respond to
33
+ the methods you are filtering by. Calling non-existing method will raise an
34
+ `NoMethodError`.
35
+
36
+ Following two examples are equivalent:
37
+
38
+ ```ruby
39
+ addresses.select_by(country: "PL", city: "Warsaw")
40
+
41
+ addresses.select do |address|
42
+ address.country == "PL" && address.city == "Warsaw"
43
+ end
21
44
  ```
22
45
 
23
- ## Usage
46
+ Object attributes are compared using `===` operator, so you can use any object
47
+ that implements it. For example, you can use a range:
24
48
 
25
- TODO: Write usage instructions here
49
+ ```ruby
50
+ addresses.select_by(age: 18..)
26
51
 
27
- ## Development
52
+ addresses.select do |address|
53
+ address.age >= 18
54
+ end
55
+ ```
28
56
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57
+ Warning, be aware of of the `===` operator behavior. For example, if you would
58
+ like to filter out the object by class, you need to use the object itself as the
59
+ argument, thus pass the `itself` method:
30
60
 
31
- 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).
61
+ ```ruby
62
+ [18, 2.5, "foo"].select_by(itself: Numeric)
63
+ # => [18, 2.5]
64
+ ```
32
65
 
33
- ## Contributing
66
+ ## Development
34
67
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/delight. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/delight/blob/main/CODE_OF_CONDUCT.md).
68
+ [...]
36
69
 
37
70
  ## License
38
71
 
39
72
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
-
41
- ## Code of Conduct
42
-
43
- Everyone interacting in the Delight project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/delight/blob/main/CODE_OF_CONDUCT.md).
@@ -3,7 +3,7 @@ module Delight
3
3
  module CollectionMatcher
4
4
  def collection_matcher(element, **kwargs)
5
5
  kwargs.all? do |key, value|
6
- element.public_send(key) == value
6
+ value === element.public_send(key)
7
7
  end
8
8
  end
9
9
  end
@@ -1,33 +1,9 @@
1
1
  module Delight
2
2
  module Enumerator
3
3
  module SelectBy
4
- # Filter colleciton elements using method calls and values passed as
5
- # keyword arguments.
6
- #
7
- # @return [Array] containingg elements that match the passed arguments.
8
- #
9
- # @example
10
- # ["String", 1, 3, :symbol, 'string'].select_by(class: String)
11
- # # => ["String", 'string']
12
- #
13
- # Person = Struct.new(:name, :age)
14
- #
15
- # people = [
16
- # Person.new("John", 30),
17
- # Person.new("Jane", 25),
18
- # Person.new("Alice", 30),
19
- # ]
20
- #
21
- # people.select_by(age: 30)
22
- # # => [
23
- # # Person.new("John", 30),
24
- # # Person.new("Alice", 30),
25
- # # ]
26
- #
27
- def select_by(**)
28
- select do |element|
29
- collection_matcher(element, **)
30
- end
4
+ refine Enumerable do
5
+ import_methods CollectionMatcher
6
+ import_methods SelectByMethods
31
7
  end
32
8
  end
33
9
  end
@@ -0,0 +1,34 @@
1
+ module Delight
2
+ module Enumerator
3
+ module SelectByMethods
4
+ # Filter collection elements using method calls and values passed as
5
+ # keyword arguments.
6
+ #
7
+ # @return [Array] containingg elements that match the passed arguments.
8
+ #
9
+ # @example
10
+ # ["String", 1, 3, :symbol, 'string'].select_by(class: String)
11
+ # # => ["String", 'string']
12
+ #
13
+ # Person = Struct.new(:name, :age)
14
+ #
15
+ # people = [
16
+ # Person.new("John", 30),
17
+ # Person.new("Jane", 25),
18
+ # Person.new("Alice", 30),
19
+ # ]
20
+ #
21
+ # people.select_by(age: 30)
22
+ # # => [
23
+ # # Person.new("John", 30),
24
+ # # Person.new("Alice", 30),
25
+ # # ]
26
+ #
27
+ def select_by(**)
28
+ select do |element|
29
+ collection_matcher(element, **)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -4,7 +4,7 @@ module Delight
4
4
  import_methods CollectionMatcher
5
5
  import_methods HashCollectionMatcher
6
6
 
7
- import_methods SelectBy
7
+ import_methods SelectByMethods
8
8
  import_methods SelectByKey
9
9
 
10
10
  import_methods DetectBang
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Delight
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.6"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delight
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mariusz Droździel
@@ -45,6 +45,7 @@ files:
45
45
  - lib/delight/enumerator/hash_collection_matcher.rb
46
46
  - lib/delight/enumerator/select_by.rb
47
47
  - lib/delight/enumerator/select_by_key.rb
48
+ - lib/delight/enumerator/select_by_methods.rb
48
49
  - lib/delight/enumerator/sole_by.rb
49
50
  - lib/delight/enumerator/sole_by_key.rb
50
51
  - lib/delight/enumerator/try_sole_by.rb