sort_in 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7998cdbbd6965179b974c20e8e17433c352693d24d15446ef8cfaa6b839394db
4
+ data.tar.gz: 917c73ec148e589cab617c63ee11bc5805506d41962c3d3e87dbe8e8185c4a9a
5
+ SHA512:
6
+ metadata.gz: b83a218c80a7798106958a6ce03904f7a20f42e75b2cb10fb69d283cfd0ba9e78ac8b868682606a462b90b792adae5089ef6a486b079d6566724919690e3dadc
7
+ data.tar.gz: 4032249d33db643e5c4e220f2bd507537b8908b548f3027e3f6a25aa83b908d37325e01cc99e1b3aa7889ee19dabd1f2e4117fdc154da7dc3fa12225f2fb6410
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2023 Naoyuki Kojima
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # SortIn
2
+
3
+ This gem provides a method that allows you to sort and retrieve values in the order they were passed to the IN clause when the IN clause is issued from the where method of ActiveRecord.
4
+
5
+ ## Language
6
+
7
+ - [Japanese](https://github.com/KojimaNaoyuki/sort_in/tree/master/document/ja)
8
+ - [English](https://github.com/KojimaNaoyuki/sort_in/tree/master/document/en)
9
+
10
+ ## Installation
11
+
12
+ Add sort_in to your project's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'sort_in'
16
+ ```
17
+
18
+ Then, execute the installation:
19
+
20
+ ```
21
+ $ bundle
22
+ ```
23
+
24
+ You can also install sort_in standalone:
25
+
26
+ ```
27
+ $ gem install sort_in
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ You can use it in a natural flow by using ActiveRecord methods and method chaining. In the example below, the filtering conditions of the where clause specify id: 3, 2, 1. In a regular where clause, an IN clause is issued, and the results are returned in ascending order of id, filtered by 3, 2, 1. By using the where_sort_in provided by this gem, you can not only filter by id 3, 2, 1 but also retrieve records in the order specified by the IN clause.
33
+
34
+ ```ruby
35
+ Post.where_sort_in(id: [3, 2, 1])
36
+ => [#<Post:~~~ id: 3, ~~~>, #<Post:~~~ id: 2, ~~~>, #<Post:~~~ id: 1, ~~~>]
37
+ ```
38
+
39
+ If you want to sort by a column other than id, use the keyword argument sort_key. You can specify the column to be used for sorting.
40
+
41
+ ```ruby
42
+ Post.where_sort_in(sort_key: :content, content: ['third', 'second', 'first'])
43
+ => [#<Post:~~~ id: 3, content: 'third', ~~~>, #<Post:~~~ id: 2, content: 'second', ~~~>, #<Post:~~~ id: 1, content: 'first', ~~~>]
44
+ ```
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake ` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ 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).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sort_in.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,18 @@
1
+ version: "3"
2
+ services:
3
+ ruby:
4
+ build: ./spec/
5
+ volumes:
6
+ - ./:/app
7
+ depends_on:
8
+ - db
9
+ tty: true
10
+ stdin_open: true
11
+ db:
12
+ build: ./spec/db
13
+ command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
14
+ environment:
15
+ MYSQL_ROOT_PASSWORD: password
16
+ MYSQL_DATABASE: "sample"
17
+ ports:
18
+ - "3306:3306"
@@ -0,0 +1,54 @@
1
+ # SortIn
2
+
3
+ This gem provides a method that allows you to sort and retrieve values in the order they were passed to the IN clause when the IN clause is issued from the where method of ActiveRecord.
4
+
5
+ ## Language
6
+
7
+ - [Japanese](https://github.com/KojimaNaoyuki/sort_in/tree/master/document/ja)
8
+ - [English](https://github.com/KojimaNaoyuki/sort_in/tree/master/document/en)
9
+
10
+ ## Installation
11
+
12
+ Add sort_in to your project's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'sort_in'
16
+ ```
17
+
18
+ Then, execute the installation:
19
+
20
+ ```
21
+ $ bundle
22
+ ```
23
+
24
+ You can also install sort_in standalone:
25
+
26
+ ```
27
+ $ gem install sort_in
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ You can use it in a natural flow by using ActiveRecord methods and method chaining. In the example below, the filtering conditions of the where clause specify id: 3, 2, 1. In a regular where clause, an IN clause is issued, and the results are returned in ascending order of id, filtered by 3, 2, 1. By using the where_sort_in provided by this gem, you can not only filter by id 3, 2, 1 but also retrieve records in the order specified by the IN clause.
33
+
34
+ ```ruby
35
+ Post.where_sort_in(id: [3, 2, 1])
36
+ => [#<Post:~~~ id: 3, ~~~>, #<Post:~~~ id: 2, ~~~>, #<Post:~~~ id: 1, ~~~>]
37
+ ```
38
+
39
+ If you want to sort by a column other than id, use the keyword argument sort_key. You can specify the column to be used for sorting.
40
+
41
+ ```ruby
42
+ Post.where_sort_in(sort_key: :content, content: ['third', 'second', 'first'])
43
+ => [#<Post:~~~ id: 3, content: 'third', ~~~>, #<Post:~~~ id: 2, content: 'second', ~~~>, #<Post:~~~ id: 1, content: 'first', ~~~>]
44
+ ```
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake ` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ 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).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sort_in.
@@ -0,0 +1,57 @@
1
+ # SortIn
2
+
3
+ ActiveRecord の where メソッドから IN 句が発行された場合、その IN 句に渡された順番で値がソートされ取得することができるメソッドを提供します。
4
+
5
+ ## Language
6
+
7
+ - [Japanese](https://github.com/KojimaNaoyuki/sort_in/tree/master/document/ja)
8
+ - [English](https://github.com/KojimaNaoyuki/sort_in/tree/master/document/en)
9
+
10
+ ## Installation
11
+
12
+ 使用したいプロジェクトの Gemfile に sort_in を記載してください。
13
+
14
+ ```ruby
15
+ gem 'sort_in'
16
+ ```
17
+
18
+ インストールを実行してください。
19
+
20
+ ```
21
+ $ bundle
22
+ ```
23
+
24
+ 単体で sort_in をインストールすることもできます。
25
+
26
+ ```
27
+ $ gem install sort_in
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ ActiveRecord のメソッドとメソッドチェインをすることで自然な流れで使用することができます。
33
+ 下記の例では where 句の絞り込み条件に id: 3, 2, 1 を指定しています。通常の where 句では IN 句が発行され id の 3, 2, 1 で絞られた状態で id の昇順で返却されます。
34
+ この gem が提供する`where_sort_in`を使用することで、id の 3, 2, 1 で絞ると共に、レコードを取得する順番も IN 句で指定した通りの順番で取得することができます。
35
+
36
+ ```ruby
37
+ Post.where_sort_in(id: [3, 2, 1])
38
+ => [#<Post:~~~ id: 3, ~~~>, #<Post:~~~ id: 2, ~~~>, #<Post:~~~ id: 1, ~~~>]
39
+ ```
40
+
41
+ id 以外でソートしたい場合は、キーワード引数(sort_key)を利用してください。
42
+ sort_key に指定したカラムを利用して並び替えられます。
43
+
44
+ ```ruby
45
+ Post.where_sort_in(sort_key: :content, content: ['third', 'second', 'first'])
46
+ => [#<Post:~~~ id: 3, content: 'third', ~~~>, #<Post:~~~ id: 2, content: 'second', ~~~>, #<Post:~~~ id: 1, content: 'first', ~~~>]
47
+ ```
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake ` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ 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).
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sort_in.
@@ -0,0 +1,21 @@
1
+ require 'sort_in/model_extension'
2
+
3
+ module SortIn
4
+ module Extension
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def inherited(kls)
9
+ super
10
+ kls.public_send(:include, SortIn::ModelExtension) if kls.superclass == ActiveRecord::Base
11
+ end
12
+ end
13
+
14
+ included do
15
+ descendants.each do |kls|
16
+ kls.public_send(:include, SortIn::ModelExtension) if kls.superclass == ApplicationRecord
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ module SortIn
2
+ module ModelExtension
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def where_sort_in(sort_key: nil, **args)
7
+ targets = where(args)
8
+ sort_in(targets: targets, args: args, sort_key: sort_key)
9
+ end
10
+
11
+ private
12
+
13
+ def sort_in(targets:, args:, sort_key:)
14
+ unless sort_key
15
+ sorted_targets = args[:id].map do |id|
16
+ targets.find { |sort_target| sort_target.id.to_i == id.to_i }
17
+ end
18
+
19
+ return sorted_targets.compact
20
+ end
21
+
22
+ sorted_targets = args[sort_key].map do |value|
23
+ targets.find { |sort_target| sort_target[sort_key] == value }
24
+ end
25
+
26
+ sorted_targets.compact
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SortIn
4
+ VERSION = "0.1.0"
5
+ end
data/lib/sort_in.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+ require 'active_record'
3
+ require_relative "sort_in/version"
4
+
5
+ module SortIn
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
9
+
10
+ ActiveSupport.on_load(:active_record) do
11
+ require 'sort_in/extension'
12
+ ActiveRecord::Base.send(:include, SortIn::Extension)
13
+ end
data/sig/sort_in.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module SortIn
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sort_in
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - KojimaNaoyuki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-01-05 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: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mysql2
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: database_cleaner
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Gem to be sorted in the order specified in the in clause
84
+ email:
85
+ - programkoji213@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".rspec"
91
+ - LICENSE
92
+ - README.md
93
+ - Rakefile
94
+ - docker-compose.yml
95
+ - document/en/README.md
96
+ - document/ja/README.md
97
+ - lib/sort_in.rb
98
+ - lib/sort_in/extension.rb
99
+ - lib/sort_in/model_extension.rb
100
+ - lib/sort_in/version.rb
101
+ - sig/sort_in.rbs
102
+ homepage: https://github.com/KojimaNaoyuki/sort_in
103
+ licenses:
104
+ - MIT
105
+ metadata:
106
+ homepage_uri: https://github.com/KojimaNaoyuki/sort_in
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: 2.6.0
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubygems_version: 3.4.10
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Gem to be sorted in the order specified in the in clause
126
+ test_files: []