array_enum 1.3.0 → 1.5.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 +4 -4
- data/.github/workflows/ci-on-merge.yml +59 -0
- data/.github/workflows/ci.yml +37 -17
- data/.gitignore +2 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +26 -16
- data/README.md +37 -4
- data/array_enum.gemspec +23 -24
- data/lib/array_enum/version.rb +1 -1
- data/lib/array_enum.rb +17 -11
- metadata +17 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 532d9c63da0d5670890af1f3236821651e5df2939a360086138af660363f7917
|
4
|
+
data.tar.gz: 296725907a06887551117c12a554bc1b3b1c0d6852bd7ab714be2cbcda6cbbae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: adb8025e3538639f748d2752d5cf05facb3494ced82c550a40d6a400f5569fffe3e977a7159d0e3a0c685b76c887c7b14158d854863761ec3f444310552aa45b
|
7
|
+
data.tar.gz: d9d7f665c33d61254bdbc758f0b4ccd998f9b81cd974e1d17b4345184ce41f1a29d137a070c2f9bdc5bf9ea118222d68cd0c4e7006ef3d3d55820326592b5a1f
|
@@ -0,0 +1,59 @@
|
|
1
|
+
name: BUILD
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
|
8
|
+
jobs:
|
9
|
+
test:
|
10
|
+
runs-on: ubuntu-latest
|
11
|
+
strategy:
|
12
|
+
matrix:
|
13
|
+
ruby-version: [3.0, 3.1, 3.3]
|
14
|
+
|
15
|
+
services:
|
16
|
+
postgres:
|
17
|
+
image: postgres
|
18
|
+
ports:
|
19
|
+
- 5432:5432
|
20
|
+
env:
|
21
|
+
POSTGRES_PASSWORD: postgres
|
22
|
+
options: >-
|
23
|
+
--health-cmd pg_isready
|
24
|
+
--health-interval 10s
|
25
|
+
--health-timeout 5s
|
26
|
+
--health-retries 5
|
27
|
+
|
28
|
+
steps:
|
29
|
+
- name: Install libraries
|
30
|
+
run: |
|
31
|
+
sudo apt-get install -y libpq-dev postgresql-client
|
32
|
+
|
33
|
+
- name: Configure database
|
34
|
+
env:
|
35
|
+
PGPASSWORD: postgres
|
36
|
+
PGUSER: postgres
|
37
|
+
PGHOST: localhost
|
38
|
+
run: |
|
39
|
+
echo "Configuring PostgresSQL"
|
40
|
+
psql -c 'create database "array_enum_test";'
|
41
|
+
|
42
|
+
- name: Checkout code
|
43
|
+
uses: actions/checkout@v4.1.1
|
44
|
+
|
45
|
+
- name : Ruby Setup
|
46
|
+
uses: ruby/setup-ruby@v1.173.0
|
47
|
+
with:
|
48
|
+
ruby-version: ${{ matrix.ruby-version }}
|
49
|
+
bundler-cache: true
|
50
|
+
|
51
|
+
- name: Install dependencies
|
52
|
+
run: bundle install
|
53
|
+
|
54
|
+
- name: Run test
|
55
|
+
env:
|
56
|
+
PGPASSWORD: postgres
|
57
|
+
PGUSER: postgres
|
58
|
+
PGHOST: localhost
|
59
|
+
run: bundle exec rake -s test
|
data/.github/workflows/ci.yml
CHANGED
@@ -1,38 +1,58 @@
|
|
1
1
|
name: CI
|
2
2
|
|
3
|
-
on:
|
3
|
+
on:
|
4
|
+
- pull_request
|
5
|
+
- push
|
4
6
|
|
5
7
|
jobs:
|
6
|
-
|
8
|
+
test:
|
7
9
|
runs-on: ubuntu-latest
|
10
|
+
strategy:
|
11
|
+
matrix:
|
12
|
+
ruby-version: [3.0, 3.1, 3.3]
|
13
|
+
|
8
14
|
services:
|
9
15
|
postgres:
|
10
|
-
image: postgres
|
16
|
+
image: postgres
|
11
17
|
ports:
|
12
|
-
|
18
|
+
- 5432:5432
|
13
19
|
env:
|
14
|
-
POSTGRES_PASSWORD:
|
20
|
+
POSTGRES_PASSWORD: postgres
|
21
|
+
options: >-
|
22
|
+
--health-cmd pg_isready
|
23
|
+
--health-interval 10s
|
24
|
+
--health-timeout 5s
|
25
|
+
--health-retries 5
|
15
26
|
|
16
27
|
steps:
|
17
28
|
- name: Install libraries
|
18
29
|
run: |
|
19
|
-
sudo apt-get update
|
20
30
|
sudo apt-get install -y libpq-dev postgresql-client
|
21
31
|
|
22
32
|
- name: Configure database
|
33
|
+
env:
|
34
|
+
PGPASSWORD: postgres
|
35
|
+
PGUSER: postgres
|
36
|
+
PGHOST: localhost
|
23
37
|
run: |
|
24
|
-
echo "
|
25
|
-
psql -
|
38
|
+
echo "Configuring PostgresSQL"
|
39
|
+
psql -c 'create database "array_enum_test";'
|
26
40
|
|
27
|
-
-
|
41
|
+
- name: Checkout code
|
42
|
+
uses: actions/checkout@v4.1.1
|
28
43
|
|
29
|
-
- name:
|
30
|
-
uses:
|
44
|
+
- name : Ruby Setup
|
45
|
+
uses: ruby/setup-ruby@v1.173.0
|
31
46
|
with:
|
32
|
-
ruby-version:
|
47
|
+
ruby-version: ${{ matrix.ruby-version }}
|
48
|
+
bundler-cache: true
|
33
49
|
|
34
|
-
- name:
|
35
|
-
run:
|
36
|
-
|
37
|
-
|
38
|
-
|
50
|
+
- name: Install dependencies
|
51
|
+
run: bundle install
|
52
|
+
|
53
|
+
- name: Run test
|
54
|
+
env:
|
55
|
+
PGPASSWORD: postgres
|
56
|
+
PGUSER: postgres
|
57
|
+
PGHOST: localhost
|
58
|
+
run: bundle exec rake -s test
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,32 +1,42 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
array_enum (1.
|
4
|
+
array_enum (1.5.0)
|
5
5
|
activemodel
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (
|
11
|
-
activesupport (=
|
12
|
-
activerecord (
|
13
|
-
activemodel (=
|
14
|
-
activesupport (=
|
15
|
-
|
10
|
+
activemodel (7.1.3.2)
|
11
|
+
activesupport (= 7.1.3.2)
|
12
|
+
activerecord (7.1.3.2)
|
13
|
+
activemodel (= 7.1.3.2)
|
14
|
+
activesupport (= 7.1.3.2)
|
15
|
+
timeout (>= 0.4.0)
|
16
|
+
activesupport (7.1.3.2)
|
17
|
+
base64
|
18
|
+
bigdecimal
|
16
19
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
20
|
+
connection_pool (>= 2.2.5)
|
21
|
+
drb
|
17
22
|
i18n (>= 1.6, < 2)
|
18
23
|
minitest (>= 5.1)
|
24
|
+
mutex_m
|
19
25
|
tzinfo (~> 2.0)
|
20
|
-
|
21
|
-
|
22
|
-
|
26
|
+
base64 (0.2.0)
|
27
|
+
bigdecimal (3.1.7)
|
28
|
+
concurrent-ruby (1.2.3)
|
29
|
+
connection_pool (2.4.1)
|
30
|
+
drb (2.2.1)
|
31
|
+
i18n (1.14.4)
|
23
32
|
concurrent-ruby (~> 1.0)
|
24
|
-
minitest (5.
|
25
|
-
|
26
|
-
|
27
|
-
|
33
|
+
minitest (5.22.3)
|
34
|
+
mutex_m (0.2.0)
|
35
|
+
pg (1.5.6)
|
36
|
+
rake (13.2.0)
|
37
|
+
timeout (0.4.1)
|
38
|
+
tzinfo (2.0.6)
|
28
39
|
concurrent-ruby (~> 1.0)
|
29
|
-
zeitwerk (2.4.2)
|
30
40
|
|
31
41
|
PLATFORMS
|
32
42
|
ruby
|
@@ -40,4 +50,4 @@ DEPENDENCIES
|
|
40
50
|
rake (>= 10.0)
|
41
51
|
|
42
52
|
BUNDLED WITH
|
43
|
-
2.
|
53
|
+
2.5.6
|
data/README.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
[](https://badge.fury.io/rb/array_enum)
|
2
|
+
[](https://github.com/freeletics/array_enum/actions/workflows/ci-on-merge.yml)
|
3
|
+
|
4
|
+
|
1
5
|
# ArrayEnum
|
2
6
|
|
3
7
|
Extension for `ActiveRecord` that adds support for `PostgreSQL` array columns, mapping string values to integers.
|
@@ -11,7 +15,6 @@ Extension for `ActiveRecord` that adds support for `PostgreSQL` array columns, m
|
|
11
15
|
### ActiveRecord extension
|
12
16
|
|
13
17
|
Database will store integers that after reading will map to string values.
|
14
|
-
Additionally scope is generated with `with_` prefix that will query database for any matching passed value.
|
15
18
|
|
16
19
|
```ruby
|
17
20
|
ActiveRecord::Schema.define do
|
@@ -23,13 +26,43 @@ end
|
|
23
26
|
class User < ActiveRecord::Base
|
24
27
|
extend ArrayEnum
|
25
28
|
|
26
|
-
array_enum favourite_colors: {"red" => 1, "blue" => 2}
|
29
|
+
array_enum favourite_colors: {"red" => 1, "blue" => 2, "green" => 3}
|
27
30
|
end
|
28
31
|
|
29
32
|
user = User.create!(favourite_colors: ["red", "green"])
|
30
33
|
user.favourite_colors # => ["red", "green"]
|
31
|
-
User.favourite_colors # => {"red" => 1, "blue" => 2}
|
32
|
-
|
34
|
+
User.favourite_colors # => {"red" => 1, "blue" => 2, "green" => 3}
|
35
|
+
```
|
36
|
+
|
37
|
+
Several scopes are made available on your model to find records based on a value or an array of values:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
user1 = User.create!(favourite_colors: ["red", "green"])
|
41
|
+
user2 = User.create!(favourite_colors: ["red"])
|
42
|
+
|
43
|
+
# Find a record that has _all_ the provided values in the array enum attribute
|
44
|
+
User.with_favourite_colors("red") # => [user1, user2]
|
45
|
+
User.with_favourite_colors(%w[red green]) # => [user1]
|
46
|
+
User.with_favourite_colors(%w[red blue]) # => []
|
47
|
+
User.with_favourite_colors(%w[green blue]) # => []
|
48
|
+
|
49
|
+
# Find a record that has the provided values, and _only those values_, in the array enum attribute
|
50
|
+
User.only_with_favourite_colors("red") # => [user2]
|
51
|
+
User.only_with_favourite_colors(%w[red green]) # => [user1]
|
52
|
+
User.only_with_favourite_colors(%w[red blue]) # => []
|
53
|
+
User.only_with_favourite_colors(%w[green blue]) # => []
|
54
|
+
|
55
|
+
# Find a record that has _at least one_ of the provided values in the array enum attribute
|
56
|
+
User.with_any_of_favourite_colors("red") # => [user1, user2]
|
57
|
+
User.with_any_of_favourite_colors(%w[red green]) # => [user1, user2]
|
58
|
+
User.with_any_of_favourite_colors(%w[red blue]) # => [user1, user2]
|
59
|
+
User.with_any_of_favourite_colors(%w[green blue]) # => [user1]
|
60
|
+
```
|
61
|
+
|
62
|
+
Attempting to find a record with a value that is not in the enum will fail:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
User.with_favourite_colors("yellow") # => ArgumentError["yellow is not a valid value for favourite_colors"]
|
33
66
|
```
|
34
67
|
|
35
68
|
### Subset Validator
|
data/array_enum.gemspec
CHANGED
@@ -1,45 +1,44 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
3
|
+
require 'array_enum/version'
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
6
|
+
spec.name = 'array_enum'
|
8
7
|
spec.version = ArrayEnum::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
8
|
+
spec.authors = ['Wojciech Wnętrzak']
|
9
|
+
spec.email = ['w.wnetrzak@gmail.com', 'eng@freeletics.com']
|
11
10
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
11
|
+
spec.summary = 'String to integer mapping for PostgreSQL array columns.'
|
12
|
+
spec.description = 'Extension for ActiveRecord that adds support for PostgreSQL array columns, mapping string values to integers.'
|
13
|
+
spec.homepage = 'https://github.com/freeletics/array_enum'
|
14
|
+
spec.license = 'MIT'
|
16
15
|
|
17
16
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
17
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
18
|
if spec.respond_to?(:metadata)
|
20
|
-
spec.metadata[
|
21
|
-
spec.metadata[
|
19
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
20
|
+
spec.metadata['source_code_uri'] = 'https://github.com/freeletics/array_enum'
|
22
21
|
else
|
23
|
-
raise
|
24
|
-
|
22
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
23
|
+
'public gem pushes.'
|
25
24
|
end
|
26
25
|
|
27
26
|
# Specify which files should be added to the gem when it is released.
|
28
27
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
29
|
-
spec.files
|
28
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
30
29
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
31
30
|
end
|
32
|
-
spec.bindir =
|
31
|
+
spec.bindir = 'exe'
|
33
32
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
34
|
-
spec.require_paths = [
|
33
|
+
spec.require_paths = ['lib']
|
35
34
|
|
36
|
-
spec.required_ruby_version =
|
35
|
+
spec.required_ruby_version = '>= 3.0'
|
37
36
|
|
38
|
-
spec.add_dependency
|
37
|
+
spec.add_dependency 'activemodel'
|
39
38
|
|
40
|
-
spec.add_development_dependency
|
41
|
-
spec.add_development_dependency
|
42
|
-
spec.add_development_dependency
|
43
|
-
spec.add_development_dependency
|
44
|
-
spec.add_development_dependency
|
39
|
+
spec.add_development_dependency 'activerecord'
|
40
|
+
spec.add_development_dependency 'bundler', '>= 1.17'
|
41
|
+
spec.add_development_dependency 'minitest', '>= 5.0'
|
42
|
+
spec.add_development_dependency 'pg'
|
43
|
+
spec.add_development_dependency 'rake', '>= 10.0'
|
45
44
|
end
|
data/lib/array_enum/version.rb
CHANGED
data/lib/array_enum.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
1
|
+
require 'array_enum/version'
|
2
|
+
require 'array_enum/subset_validator'
|
3
|
+
require 'array_enum/railtie' if defined?(Rails::Railtie)
|
4
|
+
require 'active_support/hash_with_indifferent_access'
|
5
|
+
require 'active_support/core_ext/string/inflections'
|
6
6
|
|
7
7
|
module ArrayEnum
|
8
|
-
MISSING_VALUE_MESSAGE =
|
8
|
+
MISSING_VALUE_MESSAGE = '%<value>s is not a valid value for %<attr>s'.freeze
|
9
9
|
private_constant :MISSING_VALUE_MESSAGE
|
10
10
|
|
11
11
|
def array_enum(definitions)
|
@@ -17,11 +17,17 @@ module ArrayEnum
|
|
17
17
|
mapping_hash
|
18
18
|
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
{
|
21
|
+
"with_#{attr_name}" => '@>',
|
22
|
+
"only_with_#{attr_name}" => '=',
|
23
|
+
"with_any_of_#{attr_name}" => '&&'
|
24
|
+
}.each do |method_name, comparison_operator|
|
25
|
+
define_singleton_method(method_name.to_sym) do |values|
|
26
|
+
db_values = Array(values).map do |value|
|
27
|
+
mapping_hash[value] || raise(ArgumentError, format(MISSING_VALUE_MESSAGE, value: value, attr: attr_name))
|
28
|
+
end
|
29
|
+
where("#{table_name}.#{attr_name} #{comparison_operator} ARRAY[:db_values]", db_values: db_values)
|
23
30
|
end
|
24
|
-
where("#{attr_name} @> ARRAY[:db_values]", db_values: db_values)
|
25
31
|
end
|
26
32
|
|
27
33
|
define_method(attr_symbol) do
|
@@ -30,7 +36,7 @@ module ArrayEnum
|
|
30
36
|
|
31
37
|
define_method("#{attr_name}=".to_sym) do |values|
|
32
38
|
self[attr_symbol] = Array(values).map do |value|
|
33
|
-
mapping_hash[value] || raise(ArgumentError, MISSING_VALUE_MESSAGE
|
39
|
+
mapping_hash[value] || raise(ArgumentError, format(MISSING_VALUE_MESSAGE, value: value, attr: attr_name))
|
34
40
|
end.uniq
|
35
41
|
end
|
36
42
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: array_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wojciech Wnętrzak
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -39,61 +39,61 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '1.17'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '1.17'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: minitest
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '5.0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '5.0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: pg
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: rake
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
89
|
+
version: '10.0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
96
|
+
version: '10.0'
|
97
97
|
description: Extension for ActiveRecord that adds support for PostgreSQL array columns,
|
98
98
|
mapping string values to integers.
|
99
99
|
email:
|
@@ -103,6 +103,7 @@ executables: []
|
|
103
103
|
extensions: []
|
104
104
|
extra_rdoc_files: []
|
105
105
|
files:
|
106
|
+
- ".github/workflows/ci-on-merge.yml"
|
106
107
|
- ".github/workflows/ci.yml"
|
107
108
|
- ".gitignore"
|
108
109
|
- Gemfile
|
@@ -131,14 +132,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
131
132
|
requirements:
|
132
133
|
- - ">="
|
133
134
|
- !ruby/object:Gem::Version
|
134
|
-
version: '
|
135
|
+
version: '3.0'
|
135
136
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
137
|
requirements:
|
137
138
|
- - ">="
|
138
139
|
- !ruby/object:Gem::Version
|
139
140
|
version: '0'
|
140
141
|
requirements: []
|
141
|
-
rubygems_version: 3.
|
142
|
+
rubygems_version: 3.5.3
|
142
143
|
signing_key:
|
143
144
|
specification_version: 4
|
144
145
|
summary: String to integer mapping for PostgreSQL array columns.
|