firuta 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7f41a2b92cee2b5b8b50c7f440b880d13d5a4b63
4
+ data.tar.gz: e4b267991af3865d910c6bc8796db6fdc7459c47
5
+ SHA512:
6
+ metadata.gz: 9d3a38598168a703b78d82202c6f0f41708bab05f1e5b9a4ed04a62372cc4a29f601a36c734a3e78e2c53911905f9731d91d4a171efb3fee0ac079862bdede96
7
+ data.tar.gz: 9f533dcb395f8c0716e120cd4ce68393076a64e4ec824f8d9cc39c5800ad711fe2c6600452f498c868b46f05c1f5052192f894d93be2db726986913279dc6ed8
@@ -0,0 +1,284 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .byebug_history
12
+ cc-test-reporter
13
+
14
+ # Created by https://www.gitignore.io/api/vim,ruby,linux,emacs,macos,windows,rubymine,sublimetext
15
+
16
+ ### Emacs ###
17
+ # -*- mode: gitignore; -*-
18
+ *~
19
+ \#*\#
20
+ /.emacs.desktop
21
+ /.emacs.desktop.lock
22
+ *.elc
23
+ auto-save-list
24
+ tramp
25
+ .\#*
26
+
27
+ # Org-mode
28
+ .org-id-locations
29
+ *_archive
30
+
31
+ # flymake-mode
32
+ *_flymake.*
33
+
34
+ # eshell files
35
+ /eshell/history
36
+ /eshell/lastdir
37
+
38
+ # elpa packages
39
+ /elpa/
40
+
41
+ # reftex files
42
+ *.rel
43
+
44
+ # AUCTeX auto folder
45
+ /auto/
46
+
47
+ # cask packages
48
+ .cask/
49
+ dist/
50
+
51
+ # Flycheck
52
+ flycheck_*.el
53
+
54
+ # server auth directory
55
+ /server/
56
+
57
+ # projectiles files
58
+ .projectile
59
+
60
+ # directory configuration
61
+ .dir-locals.el
62
+
63
+ ### Linux ###
64
+
65
+ # temporary files which can be created if a process still has a handle open of a deleted file
66
+ .fuse_hidden*
67
+
68
+ # KDE directory preferences
69
+ .directory
70
+
71
+ # Linux trash folder which might appear on any partition or disk
72
+ .Trash-*
73
+
74
+ # .nfs files are created when an open file is removed but is still being accessed
75
+ .nfs*
76
+
77
+ ### macOS ###
78
+ *.DS_Store
79
+ .AppleDouble
80
+ .LSOverride
81
+
82
+ # Icon must end with two \r
83
+ Icon
84
+
85
+ # Thumbnails
86
+ ._*
87
+
88
+ # Files that might appear in the root of a volume
89
+ .DocumentRevisions-V100
90
+ .fseventsd
91
+ .Spotlight-V100
92
+ .TemporaryItems
93
+ .Trashes
94
+ .VolumeIcon.icns
95
+ .com.apple.timemachine.donotpresent
96
+
97
+ # Directories potentially created on remote AFP share
98
+ .AppleDB
99
+ .AppleDesktop
100
+ Network Trash Folder
101
+ Temporary Items
102
+ .apdisk
103
+
104
+ ### Ruby ###
105
+ *.gem
106
+ *.rbc
107
+ /.config
108
+ /coverage/
109
+ /InstalledFiles
110
+ /pkg/
111
+ /spec/reports/
112
+ /spec/examples.txt
113
+ /test/tmp/
114
+ /test/version_tmp/
115
+ /tmp/
116
+
117
+ # Used by dotenv library to load environment variables.
118
+ # .env
119
+
120
+ ## Specific to RubyMotion:
121
+ .dat*
122
+ .repl_history
123
+ build/
124
+ *.bridgesupport
125
+ build-iPhoneOS/
126
+ build-iPhoneSimulator/
127
+
128
+ ## Specific to RubyMotion (use of CocoaPods):
129
+ #
130
+ # We recommend against adding the Pods directory to your .gitignore. However
131
+ # you should judge for yourself, the pros and cons are mentioned at:
132
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
133
+ #
134
+ # vendor/Pods/
135
+
136
+ ## Documentation cache and generated files:
137
+ /.yardoc/
138
+ /_yardoc/
139
+ /doc/
140
+ /rdoc/
141
+
142
+ ## Environment normalization:
143
+ /.bundle/
144
+ /vendor/bundle
145
+ /lib/bundler/man/
146
+
147
+ # for a library or gem, you might want to ignore these files since the code is
148
+ # intended to run in multiple environments; otherwise, check them in:
149
+ # Gemfile.lock
150
+ # .ruby-version
151
+ # .ruby-gemset
152
+
153
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
154
+ .rvmrc
155
+
156
+ ### RubyMine ###
157
+ # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
158
+ # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
159
+
160
+ # User-specific stuff:
161
+ .idea/**/workspace.xml
162
+ .idea/**/tasks.xml
163
+ .idea/dictionaries
164
+
165
+ # Sensitive or high-churn files:
166
+ .idea/**/dataSources/
167
+ .idea/**/dataSources.ids
168
+ .idea/**/dataSources.xml
169
+ .idea/**/dataSources.local.xml
170
+ .idea/**/sqlDataSources.xml
171
+ .idea/**/dynamic.xml
172
+ .idea/**/uiDesigner.xml
173
+
174
+ # Gradle:
175
+ .idea/**/gradle.xml
176
+ .idea/**/libraries
177
+
178
+ # CMake
179
+ cmake-build-debug/
180
+
181
+ # Mongo Explorer plugin:
182
+ .idea/**/mongoSettings.xml
183
+
184
+ ## File-based project format:
185
+ *.iws
186
+
187
+ ## Plugin-specific files:
188
+
189
+ # IntelliJ
190
+ /out/
191
+
192
+ # mpeltonen/sbt-idea plugin
193
+ .idea_modules/
194
+
195
+ # JIRA plugin
196
+ atlassian-ide-plugin.xml
197
+
198
+ # Cursive Clojure plugin
199
+ .idea/replstate.xml
200
+
201
+ # Crashlytics plugin (for Android Studio and IntelliJ)
202
+ com_crashlytics_export_strings.xml
203
+ crashlytics.properties
204
+ crashlytics-build.properties
205
+ fabric.properties
206
+
207
+ ### RubyMine Patch ###
208
+ # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
209
+
210
+ # *.iml
211
+ # modules.xml
212
+ # .idea/misc.xml
213
+ # *.ipr
214
+
215
+ # Sonarlint plugin
216
+ .idea/sonarlint
217
+
218
+ ### SublimeText ###
219
+ # cache files for sublime text
220
+ *.tmlanguage.cache
221
+ *.tmPreferences.cache
222
+ *.stTheme.cache
223
+
224
+ # workspace files are user-specific
225
+ *.sublime-workspace
226
+
227
+ # project files should be checked into the repository, unless a significant
228
+ # proportion of contributors will probably not be using SublimeText
229
+ # *.sublime-project
230
+
231
+ # sftp configuration file
232
+ sftp-config.json
233
+
234
+ # Package control specific files
235
+ Package Control.last-run
236
+ Package Control.ca-list
237
+ Package Control.ca-bundle
238
+ Package Control.system-ca-bundle
239
+ Package Control.cache/
240
+ Package Control.ca-certs/
241
+ Package Control.merged-ca-bundle
242
+ Package Control.user-ca-bundle
243
+ oscrypto-ca-bundle.crt
244
+ bh_unicode_properties.cache
245
+
246
+ # Sublime-github package stores a github token in this file
247
+ # https://packagecontrol.io/packages/sublime-github
248
+ GitHub.sublime-settings
249
+
250
+ ### Vim ###
251
+ # swap
252
+ [._]*.s[a-v][a-z]
253
+ [._]*.sw[a-p]
254
+ [._]s[a-v][a-z]
255
+ [._]sw[a-p]
256
+ # session
257
+ Session.vim
258
+ # temporary
259
+ .netrwhist
260
+ # auto-generated tag files
261
+ tags
262
+
263
+ ### Windows ###
264
+ # Windows thumbnail cache files
265
+ Thumbs.db
266
+ ehthumbs.db
267
+ ehthumbs_vista.db
268
+
269
+ # Folder config file
270
+ Desktop.ini
271
+
272
+ # Recycle Bin used on file shares
273
+ $RECYCLE.BIN/
274
+
275
+ # Windows Installer files
276
+ *.cab
277
+ *.msi
278
+ *.msm
279
+ *.msp
280
+
281
+ # Windows shortcuts
282
+ *.lnk
283
+
284
+ # End of https://www.gitignore.io/api/vim,ruby,linux,emacs,macos,windows,rubymine,sublimetext
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,37 @@
1
+ AllCops:
2
+ Exclude:
3
+ - firuta.gemspec
4
+
5
+ Documentation:
6
+ Enabled: false
7
+
8
+ LineLength:
9
+ Max: 99
10
+
11
+ FrozenStringLiteralComment:
12
+ Enabled: false
13
+
14
+ BlockLength:
15
+ Enabled: false
16
+
17
+ # rubocop-rspec custom configurations
18
+
19
+ require: rubocop-rspec
20
+
21
+ Style/VariableNumber:
22
+ EnforcedStyle: snake_case
23
+
24
+ RSpec/ExampleLength:
25
+ Enabled: false
26
+
27
+ RSpec/AnyInstance:
28
+ Enabled: false
29
+
30
+ RSpec/MultipleExpectations:
31
+ Enabled: false
32
+
33
+ RSpec/NestedGroups:
34
+ Max: 5
35
+
36
+ Lint/AmbiguousBlockAssociation:
37
+ Enabled: false
@@ -0,0 +1,18 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1
4
+ - 2.2
5
+ - 2.3
6
+ - 2.4
7
+
8
+ install:
9
+ - gem install bundler
10
+ - bundle install --retry=3
11
+
12
+ script:
13
+ - bundle exec rspec
14
+ - bundle exec rubocop -R lib spec --format simple
15
+ - CODECLIMATE_REPO_TOKEN=2867149edd72043434446a367a79e60316c7353d02a77633e57cf48cecad0853 bundle exec codeclimate-test-reporter
16
+
17
+ os:
18
+ - linux
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in firuta.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'codeclimate-test-reporter', '~> 1.0.0'
8
+ gem 'rspec', '~> 3.6'
9
+ gem 'rubocop-rspec', '~> 1.15'
10
+ gem 'simplecov', '~> 0.14', require: false
11
+ end
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2015-2017 Alejandro Bezdjian
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,117 @@
1
+ # Firuta
2
+ [![Gem Version](https://badge.fury.io/rb/firuta.svg)](https://badge.fury.io/rb/firuta)
3
+ [![Dependency Status](https://gemnasium.com/alebian/firuta.svg)](https://gemnasium.com/alebian/firuta)
4
+ [![Build Status](https://travis-ci.org/alebian/firuta.svg)](https://travis-ci.org/alebian/firuta)
5
+ [![Code Climate](https://codeclimate.com/github/alebian/firuta/badges/gpa.svg)](https://codeclimate.com/github/alebian/firuta)
6
+ [![Test Coverage](https://codeclimate.com/github/alebian/firuta/badges/coverage.svg)](https://codeclimate.com/github/alebian/firuta/coverage)
7
+ [![Inline docs](http://inch-ci.org/github/alebian/firuta.svg)](http://inch-ci.org/github/alebian/firuta)
8
+
9
+ This gem helps you manage actions with collections in a chainable way. The idea is that you use your favorite collection actions like filter or map with procs, blocks or lambdas that receive one or more parameters. Also you get a paginate method for free ;)
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'firuta'
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install firuta
22
+
23
+ ## Usage
24
+
25
+ After installing the gem, you can use it anywhere:
26
+
27
+ ```ruby
28
+ require 'firuta'
29
+
30
+ # First define your reusable procs or lambdas
31
+ BETWEEN = ->(element, min, max) { element > min && element < max }
32
+ MULTIPLY_BY = ->(element, number) { element * number }
33
+ NATURAL_SORT = ->(x, y) { x <=> y }
34
+ IDENTITY = ->(element) { element }
35
+
36
+ # Then create a collection to apply your actions
37
+ collection = [10, 2, 3, 4, 5, 1, 10, -1]
38
+
39
+ # Then define your reusable applier
40
+ applier = Firuta.new
41
+ .filter(BETWEEN, with: [0, 10])
42
+ .map(MULTIPLY_BY, with: 3)
43
+ .sort(NATURAL_SORT)
44
+ .uniq(IDENTITY)
45
+
46
+ # Finally apply your actions
47
+ puts applier
48
+ .apply(collection)
49
+ .to_s #=> [3, 6, 9, 12, 15]
50
+
51
+ # Then you can change the collection and reuse everything
52
+ collection = [100, 2, 30, 4, -5, 1, 10, -1]
53
+
54
+ puts applier
55
+ .apply(collection)
56
+ .to_s #=> [3, 6, 12]
57
+
58
+ # Also paginate the collection
59
+ puts applier
60
+ .paginate(with: { page: 1, page_size: 2 })
61
+ .apply(collection)
62
+ .to_s #=> [12]
63
+ ```
64
+
65
+ ## Actions
66
+
67
+ All the Firuta actions can receive parameters and blocks in different ways. You can use Procs or lambdas, choose the one you like the most:
68
+
69
+ ```ruby
70
+ collection = [1, 2, 3]
71
+
72
+ EXAMPLE_LAMBDA = ->(element, a, b, c) { element }
73
+ PARAMS = ['a', 'b', 'c']
74
+
75
+ puts Firuta.new
76
+ .filter(EXAMPLE_LAMBDA, PARAMS)
77
+ .filter(EXAMPLE_LAMBDA, with: PARAMS)
78
+ .filter(function: EXAMPLE_LAMBDA, with: PARAMS)
79
+ .filter(with: PARAMS) { |element, a, b, c| element }
80
+ .apply(collection)
81
+ .to_s #=> [1, 2, 3]
82
+ ```
83
+
84
+ The `find` and `reduce` actions are considered final (because they transform the collection into something else). Those actions need to be defined at the end of the chain or else an exception will be raised.
85
+
86
+ ```ruby
87
+ require 'firuta'
88
+
89
+ BETWEEN = ->(element, min, max) { element > min && element < max }
90
+ REDUCER = ->(acc, element) { acc + element }
91
+
92
+ collection = [10, 2, 3, 4, 5, 1, 10, -1]
93
+
94
+ puts Firuta.new
95
+ .filter(BETWEEN, with: [0, 10])
96
+ .reduce(REDUCER, with: 0)
97
+ .apply(collection) #=> 15
98
+
99
+ begin
100
+ puts Firuta.new
101
+ .reduce(REDUCER, with: 0)
102
+ .filter(BETWEEN, with: [0, 10]) # Raises
103
+ .apply(collection)
104
+ rescue Firuta::Errors::MoreThanOneTerminalCommand => e
105
+ puts e #=> Firuta::Errors::MoreThanOneTerminalCommand
106
+ end
107
+ ```
108
+
109
+ ## Contributing
110
+
111
+ 1. Fork it
112
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
113
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
114
+ 4. Run rubocop lint (`rubocop -R lib spec --format simple`)
115
+ 5. Run rspec tests (`bundle exec rspec`)
116
+ 6. Push your branch (`git push origin my-new-feature`)
117
+ 7. Create a new Pull Request
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'firuta/version'
6
+ require 'date'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'firuta'
10
+ spec.version = Firuta::VERSION
11
+ spec.platform = Gem::Platform::RUBY
12
+ spec.authors = ['alebian']
13
+ spec.email = ['alebezdjian@gmail.com']
14
+ spec.date = Date.today
15
+ spec.summary = 'Chain collection actions easily'
16
+ spec.description = 'Chain collection actions easily'
17
+ spec.homepage = 'https://github.com/alebian/firuta'
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec)/}) }
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec)/})
21
+ spec.require_paths = ['lib']
22
+ spec.license = 'Apache-2.0'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.15'
25
+ spec.add_development_dependency 'rake', '~> 12.0'
26
+ spec.add_development_dependency 'byebug', '~> 9.0' if RUBY_VERSION >= '2.0.0'
27
+ spec.add_development_dependency 'rubocop', '~> 0.49'
28
+ end
@@ -0,0 +1,59 @@
1
+ require_relative 'firuta/commands/filter'
2
+ require_relative 'firuta/commands/map'
3
+ require_relative 'firuta/commands/sort'
4
+ require_relative 'firuta/commands/reduce'
5
+ require_relative 'firuta/commands/paginate'
6
+ require_relative 'firuta/commands/uniq'
7
+ require_relative 'firuta/commands/find'
8
+ require_relative 'firuta/errors/more_than_one_terminal_command'
9
+
10
+ module Firuta
11
+ def self.new(collection = nil)
12
+ Base.new(collection)
13
+ end
14
+
15
+ class Base
16
+ def initialize(collection = nil)
17
+ @commands = []
18
+ @collection = collection
19
+ @terminal_command_added = false
20
+ end
21
+
22
+ COMMANDS = [
23
+ [:filter, Commands::Filter],
24
+ [:map, Commands::Map],
25
+ [:sort, Commands::Sort],
26
+ [:reduce, Commands::Reduce],
27
+ [:paginate, Commands::Paginate],
28
+ [:uniq, Commands::Uniq],
29
+ [:find, Commands::Find]
30
+ ].freeze
31
+
32
+ COMMANDS.each do |array|
33
+ define_method(array[0]) do |proc = nil, params = nil, function: nil, with: [], &block|
34
+ add(
35
+ command: array[1],
36
+ proc: proc || function || block,
37
+ params: params || with
38
+ )
39
+ end
40
+ end
41
+
42
+ def apply(collection = nil)
43
+ result = collection || @collection
44
+ @commands.each do |command|
45
+ result = command.apply_to(result)
46
+ end
47
+ result
48
+ end
49
+
50
+ private
51
+
52
+ def add(command:, proc:, params:)
53
+ raise Errors::MoreThanOneTerminalCommand if @terminal_command_added
54
+ @commands << command.new(proc, params)
55
+ @terminal_command_added = true if command.terminal?
56
+ self
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,25 @@
1
+ require_relative '../errors/subclass_must_implement'
2
+
3
+ module Firuta
4
+ module Commands
5
+ class Base
6
+ def initialize(function, params = nil)
7
+ @proc = function
8
+ @params = [params].flatten.compact
9
+ end
10
+
11
+ def call(element)
12
+ return @proc.call(element) if @params.empty?
13
+ @proc.call(element, *@params)
14
+ end
15
+
16
+ def apply_to(*_params)
17
+ raise Errors::SubclassMustImplement
18
+ end
19
+
20
+ def self.terminal?
21
+ false
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'base'
2
+
3
+ module Firuta
4
+ module Commands
5
+ class Filter < Base
6
+ def apply_to(collection)
7
+ collection.select { |element| call(element) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'base'
2
+
3
+ module Firuta
4
+ module Commands
5
+ class Find < Base
6
+ def apply_to(collection)
7
+ collection.find(&@proc)
8
+ end
9
+
10
+ def self.terminal?
11
+ true
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'base'
2
+
3
+ module Firuta
4
+ module Commands
5
+ class Map < Base
6
+ def apply_to(collection)
7
+ collection.map { |element| call(element) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'base'
2
+
3
+ module Firuta
4
+ module Commands
5
+ class Paginate < Base
6
+ DEFAULT_PER_PAGE = 10
7
+ DEFAULT_PAGE = 0
8
+
9
+ def apply_to(collection)
10
+ collection
11
+ .each_slice(@params.first[:page_size] || DEFAULT_PER_PAGE)
12
+ .to_a[@params.first[:page] || DEFAULT_PAGE]
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ require_relative 'base'
2
+
3
+ module Firuta
4
+ module Commands
5
+ class Reduce < Base
6
+ def call(element)
7
+ @proc.call(*@params, element)
8
+ end
9
+
10
+ def apply_to(collection)
11
+ collection.reduce(*@params, &@proc)
12
+ end
13
+
14
+ def self.terminal?
15
+ true
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'base'
2
+
3
+ module Firuta
4
+ module Commands
5
+ class Sort < Base
6
+ def apply_to(collection)
7
+ collection.sort(&@proc)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'base'
2
+
3
+ module Firuta
4
+ module Commands
5
+ class Uniq < Base
6
+ def apply_to(collection)
7
+ collection.uniq(&@proc)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ module Firuta
2
+ module Errors
3
+ class MoreThanOneTerminalCommand < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Firuta
2
+ module Errors
3
+ class SubclassMustImplement < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Firuta
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: firuta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - alebian
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '9.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '9.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.49'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.49'
69
+ description: Chain collection actions easily
70
+ email:
71
+ - alebezdjian@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE
82
+ - README.md
83
+ - Rakefile
84
+ - firuta.gemspec
85
+ - lib/firuta.rb
86
+ - lib/firuta/commands/base.rb
87
+ - lib/firuta/commands/filter.rb
88
+ - lib/firuta/commands/find.rb
89
+ - lib/firuta/commands/map.rb
90
+ - lib/firuta/commands/paginate.rb
91
+ - lib/firuta/commands/reduce.rb
92
+ - lib/firuta/commands/sort.rb
93
+ - lib/firuta/commands/uniq.rb
94
+ - lib/firuta/errors/more_than_one_terminal_command.rb
95
+ - lib/firuta/errors/subclass_must_implement.rb
96
+ - lib/firuta/version.rb
97
+ homepage: https://github.com/alebian/firuta
98
+ licenses:
99
+ - Apache-2.0
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.5.1
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Chain collection actions easily
121
+ test_files: []