match_arrays 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3750a65b4fc6889ec619f3a626c43d3b57575127
4
+ data.tar.gz: 589f315e5610264cbfb958d6b2c8c57984e1136d
5
+ SHA512:
6
+ metadata.gz: 89ff40c70b0553edb79f2ba06bf9a510908ff2c1785f6432844bffa908203fe52cdda6bd3f95fe165782a2d42c94a654431aa90049c8db0b652dfcba4a192c75
7
+ data.tar.gz: c036739fa1fe54f4d21c094a9cdab983ff18e0a4716d3a3c75063325ddb5133b984ee5867d5e0a02f2e255413d6bc29bfdffe7baadbbc37d99eb48d46b4f42c0
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ /vendor/bundle
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in match_arrays.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ match_arrays (1.0.0)
5
+ activerecord
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.4.4)
11
+ activesupport (= 5.2.4.4)
12
+ activerecord (5.2.4.4)
13
+ activemodel (= 5.2.4.4)
14
+ activesupport (= 5.2.4.4)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.4.4)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (9.0.0)
22
+ concurrent-ruby (1.1.7)
23
+ diff-lcs (1.4.4)
24
+ i18n (1.8.5)
25
+ concurrent-ruby (~> 1.0)
26
+ minitest (5.14.2)
27
+ rake (12.3.3)
28
+ rspec (3.10.0)
29
+ rspec-core (~> 3.10.0)
30
+ rspec-expectations (~> 3.10.0)
31
+ rspec-mocks (~> 3.10.0)
32
+ rspec-core (3.10.0)
33
+ rspec-support (~> 3.10.0)
34
+ rspec-expectations (3.10.0)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.10.0)
37
+ rspec-mocks (3.10.0)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.10.0)
40
+ rspec-support (3.10.0)
41
+ thread_safe (0.3.6)
42
+ tzinfo (1.2.9)
43
+ thread_safe (~> 0.1)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ bundler (~> 2.0)
50
+ match_arrays!
51
+ rake (~> 12.0)
52
+ rspec (~> 3.0)
53
+
54
+ BUNDLED WITH
55
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 s-saku
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,102 @@
1
+ # MatchArrays
2
+
3
+ A ruby gem that matches two arrays and executes a callback based on the match result.
4
+
5
+ This gem is especially useful in the process of DB updating with values posted via API.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'match_arrays'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install match_arrays
22
+
23
+ ## Usage
24
+
25
+ ### Prepare two arrays (masters, transactions) with elements of Hash or Rails ActiveRecord.
26
+
27
+ ```Ruby
28
+ masters = [{ k: "a", v: 1 }, { k: "b", v:2 }]
29
+ transactions = [{ k: "b", v: 3 }, { k: "c", v:4 }]
30
+ ```
31
+
32
+ masters means the base array and transactions means the dependent array.
33
+ For example, in the context of updating the DB via the API, masters means the DB values and transactions means the values sent from the API.
34
+
35
+ In this gem, the element of masters is represented as MA, and the element of transactions as TR.
36
+
37
+ ### Define a Proc that specifies the matching key for each array element.
38
+
39
+ ```Ruby
40
+ p_m_key = proc { |ma| ma[:k] }
41
+ p_t_key = proc { |tr| tr[:k] }
42
+ ```
43
+
44
+ ### Define the Proc you want to call in each case: Matching, TR only, MA only.
45
+
46
+ ```Ruby
47
+ p_match = proc do |ma, tr|
48
+ # something to do when matching
49
+ # e.g. fetch MA data from DB, and update DB by TR value.
50
+ end
51
+
52
+ p_tr_only = proc do |t|
53
+ # something to do when TR only
54
+ # e.g. create a new record by TR value
55
+ end
56
+
57
+ p_ma_only = proc do |m|
58
+ # something to do when MA only
59
+ # e.g. delete MA record
60
+ end
61
+ ```
62
+
63
+ ### Call the Match method to execute the specified Proc while matching two arrays.
64
+
65
+ ```Ruby
66
+ MatchArrays.match(
67
+ masters: masters,
68
+ transactions: transactions,
69
+ p_m_key: p_m_key,
70
+ p_t_key: p_t_key,
71
+ p_match: p_match,
72
+ p_tr_only: p_tr_only,
73
+ p_ma_only: p_ma_only
74
+ )
75
+ ```
76
+
77
+ ### (Optional) You can give freely available arguments.
78
+
79
+ ```
80
+ tr_only_records = []
81
+
82
+ p_tr_only = proc do |t, tr_only_records|
83
+ tr_only_records.push(t)
84
+ end
85
+
86
+ MatchArrays.match(
87
+ (the same as above),
88
+ attr_obj: tr_only_records )
89
+
90
+ puts tr_only_records
91
+ #=> [{ k: "c", v:4 }]
92
+ ```
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/s-saku/match_arrays.
97
+
98
+ ## Change Log
99
+
100
+ ### v1.0.0
101
+
102
+ First release
@@ -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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "match_arrays"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,59 @@
1
+ require "match_arrays/version"
2
+ require "active_record"
3
+
4
+ module MatchArrays
5
+ class Error < StandardError; end
6
+
7
+ # A function that matches two arrays (masters, transactions) containing hash or ActiveRecord with the specified key.
8
+ # It judges "matching", "master only", or "transaction only", and then calls the callback Proc specified for each.
9
+ #
10
+ # * In this gem, the elements of the masters and transactions arrays are called "MA" and "TR" respectively.
11
+ # * Callbacks are accepted only for Proc, not for lambda, because lambda does not fit the strict error checking of block arguments.
12
+ # * Masters and transactions are replaced with an empty array only when nil is input.
13
+ # This is because it is expected to work as intended in many cases.
14
+ def self.match(masters:, transactions:, p_m_key:, p_t_key:, p_match:, p_tr_only:, p_ma_only:, attr_obj: nil)
15
+ nil_to_array_masters = masters.nil? ? [] : masters
16
+ nil_to_array_transactions = transactions.nil? ? [] : transactions
17
+ raise ArgumentError, "Only an Array is accepted as the :masters argument." unless nil_to_array_masters.is_a?(Array) || nil_to_array_masters.is_a?(ActiveRecord::Associations::CollectionProxy)
18
+ raise ArgumentError, "Only an Array is accepted as the :transactions argument." unless nil_to_array_transactions.is_a?(Array) || nil_to_array_transactions.is_a?(ActiveRecord::Associations::CollectionProxy)
19
+ raise ArgumentError, "Only an Proc is accepted as the :p_m_key argument." unless p_m_key.is_a?(Proc)
20
+ raise ArgumentError, "Only an Proc is accepted as the :p_t_key argument." unless p_t_key.is_a?(Proc)
21
+ raise ArgumentError, "Only an Proc is accepted as the :p_match argument." unless p_match.is_a?(Proc)
22
+ raise ArgumentError, "Only an Proc is accepted as the :p_tr_only argument." unless p_tr_only.is_a?(Proc)
23
+ raise ArgumentError, "Only an Proc is accepted as the :p_ma_only argument." unless p_ma_only.is_a?(Proc)
24
+
25
+ # Generate a hash that can access MA with the key defined in p_m_key
26
+ ma_hash = nil_to_array_masters.each_with_object({}) do |ma, hash|
27
+ key = p_m_key.call(ma).to_s # Force to_s to avoid matching type errors
28
+ hash[key] = ma
29
+ end
30
+
31
+ # Create a hash with the same content as ma_hash.
32
+ # Deleting a MA that matches a TR from this hash, and determine that the remaining elements are "MA only"
33
+ ma_only_hash = nil_to_array_masters.each_with_object({}) do |ma, hash|
34
+ key = p_m_key.call(ma).to_s # Force to_s to avoid matching type errors
35
+ hash[key] = ma
36
+ end
37
+
38
+ # Create a hash that can access TR with the key defined in p_t_key
39
+ tr_hash = nil_to_array_transactions.each_with_object({}) do |tr, hash|
40
+ key = p_t_key.call(tr).to_s # Force to_s to avoid matching type errors
41
+ hash[key] = tr
42
+ end
43
+
44
+ # Read TRs one at a time and determine whether it is "matching" or "TR only"
45
+ tr_hash.each do |key, tr|
46
+ if ma_hash[key].present?
47
+ p_match.call(ma_hash[key], tr, attr_obj, nil_to_array_masters, nil_to_array_transactions)
48
+ ma_only_hash.delete(key)
49
+ else
50
+ p_tr_only.call(tr, attr_obj, nil_to_array_masters, nil_to_array_transactions)
51
+ end
52
+ end
53
+
54
+ # Call the "MA only" callback.
55
+ ma_only_hash.each_value do |ma|
56
+ p_ma_only.call(ma, attr_obj, nil_to_array_masters, nil_to_array_transactions)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module MatchArrays
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/match_arrays/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "match_arrays"
5
+ spec.version = MatchArrays::VERSION
6
+ spec.authors = ["s-saku"]
7
+ spec.email = ["t.nishi7@gmail.com"]
8
+
9
+ spec.summary = %q{A ruby gem that matches two arrays and executes a callback based on the match result.}
10
+ spec.homepage = "https://github.com/s-saku/match_arrays"
11
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
12
+
13
+ spec.metadata["homepage_uri"] = spec.homepage
14
+ spec.metadata["source_code_uri"] = "https://github.com/s-saku/match_arrays"
15
+ spec.metadata["changelog_uri"] = "https://github.com/s-saku/match_arrays/blob/main/README.md"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.required_ruby_version = '>= 2.4.4'
27
+ spec.add_dependency "activerecord"
28
+ spec.add_development_dependency "bundler", "~> 2.0"
29
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: match_arrays
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - s-saku
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-12-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ description:
42
+ email:
43
+ - t.nishi7@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - lib/match_arrays.rb
58
+ - lib/match_arrays/version.rb
59
+ - match_arrays.gemspec
60
+ homepage: https://github.com/s-saku/match_arrays
61
+ licenses: []
62
+ metadata:
63
+ homepage_uri: https://github.com/s-saku/match_arrays
64
+ source_code_uri: https://github.com/s-saku/match_arrays
65
+ changelog_uri: https://github.com/s-saku/match_arrays/blob/main/README.md
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 2.4.4
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.6.14.1
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: A ruby gem that matches two arrays and executes a callback based on the match
86
+ result.
87
+ test_files: []