merginator 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1d2fea30ed75731053fb8a668dfde25de0b0bfa56b091020d68a0878dbc74ac4
4
+ data.tar.gz: 478433d62ff8943e02dcb1affa790868d0d93bc45292c374c10fbc84e6bf44a5
5
+ SHA512:
6
+ metadata.gz: 91709f61f93ae9b2296a935d06fdf69d6f8c8722ee91546d87329faa20a95e56be5b3d8fe4d62c6fcf8f7d55df41dfc5740d743a04097332d71cae1779f3e3f8
7
+ data.tar.gz: 04004fb039e34a98ee2803f7d33da2d3654d280929299fd2121c11455992441183da5c6f55be3e8e51d5a9494e35158313ff9c57ed1a3231ef3af3b5971632c6
data/.rubocop.yml ADDED
@@ -0,0 +1,49 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-minitest
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.7.8
7
+ NewCops: enable
8
+
9
+ Style/StringLiterals:
10
+ Enabled: true
11
+ EnforcedStyle: single_quotes
12
+
13
+ Style/StringLiteralsInInterpolation:
14
+ Enabled: true
15
+ EnforcedStyle: double_quotes
16
+
17
+ Layout/LineLength:
18
+ Max: 120
19
+
20
+ Minitest/EmptyLineBeforeAssertionMethods:
21
+ Enabled: false
22
+
23
+ Metrics/BlockLength:
24
+ CountAsOne:
25
+ - array
26
+ - heredoc
27
+ - method_call
28
+ - hash
29
+
30
+ Metrics/ClassLength:
31
+ CountAsOne:
32
+ - array
33
+ - heredoc
34
+ - method_call
35
+ - hash
36
+
37
+ Metrics/MethodLength:
38
+ CountAsOne:
39
+ - array
40
+ - heredoc
41
+ - method_call
42
+ - hash
43
+
44
+ Metrics/ModuleLength:
45
+ CountAsOne:
46
+ - array
47
+ - heredoc
48
+ - method_call
49
+ - hash
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 2.7.8
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ ## [0.1.1] - 2023-06-26
2
+
3
+ - Updating README
4
+ - Configuration
5
+
6
+ ## [0.1.0] - 2023-06-26
7
+
8
+ - Initial release with all tests passing
9
+
10
+ ## [0.0.1] - 2023-06-21
11
+
12
+ - Pre-release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at RyanVerhey@users.noreply.github.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in merginator.gemspec
6
+ gemspec
7
+
8
+ gem 'rake'
9
+
10
+ gem 'minitest'
11
+
12
+ gem 'rubocop'
13
+ gem 'rubocop-minitest', require: false
14
+ gem 'rubocop-rake', require: false
15
+ gem 'rbs', require: false
data/Gemfile.lock ADDED
@@ -0,0 +1,54 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ merginator (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ json (2.6.3)
11
+ minitest (5.18.1)
12
+ parallel (1.23.0)
13
+ parser (3.2.2.3)
14
+ ast (~> 2.4.1)
15
+ racc
16
+ racc (1.7.1)
17
+ rainbow (3.1.1)
18
+ rake (13.0.6)
19
+ rbs (3.1.0)
20
+ regexp_parser (2.8.1)
21
+ rexml (3.2.5)
22
+ rubocop (1.52.1)
23
+ json (~> 2.3)
24
+ parallel (~> 1.10)
25
+ parser (>= 3.2.2.3)
26
+ rainbow (>= 2.2.2, < 4.0)
27
+ regexp_parser (>= 1.8, < 3.0)
28
+ rexml (>= 3.2.5, < 4.0)
29
+ rubocop-ast (>= 1.28.0, < 2.0)
30
+ ruby-progressbar (~> 1.7)
31
+ unicode-display_width (>= 2.4.0, < 3.0)
32
+ rubocop-ast (1.29.0)
33
+ parser (>= 3.2.1.0)
34
+ rubocop-minitest (0.31.0)
35
+ rubocop (>= 1.39, < 2.0)
36
+ rubocop-rake (0.6.0)
37
+ rubocop (~> 1.0)
38
+ ruby-progressbar (1.13.0)
39
+ unicode-display_width (2.4.2)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ merginator!
46
+ minitest
47
+ rake
48
+ rbs
49
+ rubocop
50
+ rubocop-minitest
51
+ rubocop-rake
52
+
53
+ BUNDLED WITH
54
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Ryan Verhey
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,55 @@
1
+ # Merginator
2
+
3
+ Easily merge collections with a simple API!
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add Merginator
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install Merginator
14
+
15
+ ## Usage
16
+
17
+ ### Pattern Merge
18
+
19
+ Merge collections based on a defined pattern. Adding an optional total limits the number returned values.
20
+ ```ruby
21
+ arrays = [
22
+ Array.new(10, 'One'),
23
+ Array.new(10, 'Two'),
24
+ Array.new(10, 'Three'),
25
+ ]
26
+ mergifier = Merginator.pattern_merge(4, 2, 1, total: 10)
27
+ mergifier.merge(arrays[0], arrays[1], arrays[2])
28
+ # => ['One', 'One', 'One', 'One', 'Two', 'Two', 'Three', 'One', 'One', 'One']
29
+ ```
30
+
31
+ Need to merge the results of multiple queries, in Rails for example? Need to paginate your results, but you don't want to instantiate more results than you need? Merginator will tell you how many records you need to query:
32
+ ```ruby
33
+ mergifier = Merginator.pattern_merge(4, 2, 1, total: 10)
34
+ counts = mergifier.counts # => [7, 2, 1]
35
+ mergifier.merge(Post.limit(counts[0]), Comment.limit(counts[1]), User.limit(counts[2]))
36
+ # => [#<Post>, #<Post>, #<Post>, #<Post>, #<Comment>, #<Comment>, #<User>, #<Post>, #<Post>, #<Post>]
37
+ ```
38
+
39
+ ## Development
40
+
41
+ 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.
42
+
43
+ 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).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/RyanVerhey/merginator. 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/RyanVerhey/merginator/blob/main/CODE_OF_CONDUCT.md).
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
52
+
53
+ ## Code of Conduct
54
+
55
+ Everyone interacting in the Merginator project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/RyanVerhey/merginator/blob/main/CODE_OF_CONDUCT.md).
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,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Merginator
4
+ # Merging based on a given pattern.
5
+ # ex.
6
+ # Merginator::PatternMerge.new(4, 2, 1, total: 25) # Means 3 collections, four from the
7
+ # first, 2 from the second, and one from the third. Also passing in the total,
8
+ # or, an amount for a page
9
+ class PatternMerge
10
+ attr_reader :pattern, :total, :counts
11
+
12
+ def initialize(*pattern, total: nil)
13
+ @pattern = pattern
14
+ validate_pattern
15
+
16
+ @total = total
17
+ validate_total
18
+
19
+ # Counts are helpful when you're looking for a particular length end result
20
+ # and querying a database or index. If you're not, they're not needed.
21
+ @counts = generate_pattern_counts(@total) if @total
22
+ end
23
+
24
+ def merge(*collections)
25
+ validate_merge_collections(collections)
26
+
27
+ total = @total || collections.sum(&:size)
28
+
29
+ slices_for_collections = collections.map.with_index do |collection, index|
30
+ collection.each_slice(@pattern[index]).to_a
31
+ end
32
+
33
+ slices_for_collections.map(&:size).max.times.each_with_object([]) do |index, arr|
34
+ arr << slices_for_collections.map { |slices| slices[index] || [] }.flatten
35
+ break(arr) if arr.flatten.size >= total
36
+ end.flatten.take(total)
37
+ end
38
+
39
+ private
40
+
41
+ # Outputs the counts needed for each collection in the pattern to reach the
42
+ # provided total
43
+ # ex:
44
+ # Merginator::Mergifier::PatternMerge.new(4, 2, 1, total: 25)
45
+ # => [16, 6, 3]
46
+ def generate_pattern_counts(total)
47
+ full_repetitions = total / @pattern.sum
48
+ counts = @pattern.map { |num| num * full_repetitions }
49
+
50
+ @pattern.each_with_index do |num, index|
51
+ # Add the difference between the total and the current total, up to
52
+ # the pattern number
53
+
54
+ counts[index] += (total - counts.sum).clamp(0, num)
55
+ end
56
+
57
+ counts
58
+ end
59
+
60
+ def validate_pattern
61
+ raise ArgumentError, 'there must be more than one collection in the pattern' if @pattern.count <= 1
62
+ raise ArgumentError, 'pattern must be all integers' unless @pattern.all? { |n| n.is_a? Integer }
63
+ end
64
+
65
+ def validate_total
66
+ raise ArgumentError, 'total must be at least 1' if @total && @total < 1
67
+ end
68
+
69
+ def validate_merge_collections(collections)
70
+ unless @pattern.count == collections.count
71
+ message = 'number of collections must match pattern; '
72
+ message += "expected #{@pattern.count} collections, actual: #{collections.count}"
73
+ raise ArgumentError, message
74
+ end
75
+
76
+ if @total && collections.flatten.count < @total
77
+ message = 'total number of elements in collections must be >= provided total; '
78
+ message += "expected #{@total} elements, actual: #{collections.flatten.count}"
79
+ raise ArgumentError, message
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Merginator
4
+ VERSION = '0.1.1'
5
+ end
data/lib/merginator.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'merginator/version'
4
+ require_relative 'merginator/pattern_merge'
5
+
6
+ # Contains top-level namespace code
7
+ module Merginator
8
+ class Error < StandardError; end
9
+
10
+ def self.pattern_merge(...)
11
+ PatternMerge.new(...)
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
2
+ class Merginator::PatternMerge
3
+ @pattern: Array[Integer]
4
+ @total: Integer
5
+ @counts: Array[Integer]?
6
+
7
+ def merge: (*Enumerable[untyped]) -> Array[untyped]
8
+
9
+ private
10
+
11
+ def generate_pattern_counts: (Integer) -> Array[Integer]
12
+ def validate_pattern: () -> void
13
+ def validate_total: () -> void
14
+ def validate_merge_collections: (*Enumerable[untyped]) -> void
15
+ end
@@ -0,0 +1,6 @@
1
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
2
+ module Merginator
3
+ VERSION: String
4
+
5
+ def self.pattern_merge: (*untyped) -> Merginator::PatternMerge
6
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: merginator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Verhey
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-06-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Merge 2 or more collections based on a repeating pattern.
14
+ email:
15
+ - ryan@ryanverhey.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rubocop.yml"
21
+ - ".tool-versions"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - lib/merginator.rb
30
+ - lib/merginator/pattern_merge.rb
31
+ - lib/merginator/version.rb
32
+ - sig/mergifier/pattern_merge.rbs
33
+ - sig/merginator.rbs
34
+ homepage: https://github.com/RyanVerhey/merginator
35
+ licenses:
36
+ - MIT
37
+ metadata:
38
+ homepage_uri: https://github.com/RyanVerhey/merginator
39
+ source_code_uri: https://github.com/RyanVerhey/merginator
40
+ changelog_uri: https://github.com/RyanVerhey/merginator/blob/main/CHANGELOG.md
41
+ rubygems_mfa_required: 'true'
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.7.8
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.1.6
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Merging collections based on a pattern.
61
+ test_files: []