array_enum 1.3.0 → 1.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: decd290271c41c2193a1e63f98a0df618c9a95813570aa01a32f27bd68d25865
4
- data.tar.gz: da107aae087d6592ce0b33d8186170f41438b7be199737103a0b3bea4c13e53e
3
+ metadata.gz: 5d2df08c37b57f1ab526c97e96687aaa26d6f4cdbbd7cf189a1671b4fc2ad309
4
+ data.tar.gz: a4a87c06b773f5b8210d67e869372e99e838853962789a5d030bd8ec2fbc9a9e
5
5
  SHA512:
6
- metadata.gz: 8a4e5a2c6cf33ec8fa943d13d0b1b852f20671598556abd638c7fb72d9175e83b355cc122b615c3aa1e109511313b4e163f30808ec0a9d4cdb9f422571cdac49
7
- data.tar.gz: d984bfdf1addd82a03fc1098453c9141f6ca3fb3b13ba8f9b2e4cde5d9d7244379d1677a248dbae93c3f272fee6e679c15ada79136ca6c9ea71da9e55d1ef56f
6
+ metadata.gz: 153d73f33212247cba61d862b31948bd66c50c352f2984fbfbe00c71ce77a8086a8925e3256bfa03013a32050442a6be7014da888493a3c422256ac85d24108a
7
+ data.tar.gz: 273ababe94efae433b8577e872cc44f0a69e85b482b9deda86dc83b39f0440d973e2755ca2bc85b7e36ab90d2313b77e0ed0dc94e932a4b681a26eeaf592a908
@@ -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: [2.7, '3.0', 3.1]
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@v2
44
+
45
+ - name : Ruby Setup
46
+ uses: ruby/setup-ruby@v1
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
@@ -1,38 +1,56 @@
1
1
  name: CI
2
2
 
3
- on: [push, pull_request]
3
+ on: [pull_request]
4
4
 
5
5
  jobs:
6
- build:
6
+ test:
7
7
  runs-on: ubuntu-latest
8
+ strategy:
9
+ matrix:
10
+ ruby-version: [2.7, '3.0', 3.1]
11
+
8
12
  services:
9
13
  postgres:
10
- image: postgres:11
14
+ image: postgres
11
15
  ports:
12
- - 5432:5432
16
+ - 5432:5432
13
17
  env:
14
- POSTGRES_PASSWORD: ""
18
+ POSTGRES_PASSWORD: postgres
19
+ options: >-
20
+ --health-cmd pg_isready
21
+ --health-interval 10s
22
+ --health-timeout 5s
23
+ --health-retries 5
15
24
 
16
25
  steps:
17
26
  - name: Install libraries
18
27
  run: |
19
- sudo apt-get update
20
28
  sudo apt-get install -y libpq-dev postgresql-client
21
29
 
22
30
  - name: Configure database
31
+ env:
32
+ PGPASSWORD: postgres
33
+ PGUSER: postgres
34
+ PGHOST: localhost
23
35
  run: |
24
- echo "PostgreSQL"
25
- psql -h localhost -c 'create database "array_enum_test";' -U postgres
36
+ echo "Configuring PostgresSQL"
37
+ psql -c 'create database "array_enum_test";'
26
38
 
27
- - uses: actions/checkout@v1
39
+ - name: Checkout code
40
+ uses: actions/checkout@v2
28
41
 
29
- - name: Set up Ruby
30
- uses: actions/setup-ruby@v1
42
+ - name : Ruby Setup
43
+ uses: ruby/setup-ruby@v1
31
44
  with:
32
- ruby-version: 2.6.x
45
+ ruby-version: ${{ matrix.ruby-version }}
46
+ bundler-cache: true
33
47
 
34
- - name: Build and test
35
- run: |
36
- gem install bundler
37
- bundle install --jobs 4 --retry 3
38
- bundle exec rake test
48
+ - name: Install dependencies
49
+ run: bundle install
50
+
51
+ - name: Run test
52
+ env:
53
+ PGPASSWORD: postgres
54
+ PGUSER: postgres
55
+ PGHOST: localhost
56
+ run: bundle exec rake -s test
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- array_enum (1.3.0)
4
+ array_enum (1.4.0)
5
5
  activemodel
6
6
 
7
7
  GEM
@@ -40,4 +40,4 @@ DEPENDENCIES
40
40
  rake (>= 10.0)
41
41
 
42
42
  BUNDLED WITH
43
- 2.0.2
43
+ 2.2.3
data/README.md CHANGED
@@ -1,3 +1,7 @@
1
+ [![Gem Version](https://badge.fury.io/rb/array_enum.svg)](https://badge.fury.io/rb/array_enum)
2
+ [![BUILD](https://github.com/freeletics/array_enum/actions/workflows/ci-on-merge.yml/badge.svg)](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
- User.with_favourite_colors("red") # => [user]
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
@@ -1,3 +1,3 @@
1
1
  module ArrayEnum
2
- VERSION = "1.3.0"
2
+ VERSION = "1.4.0"
3
3
  end
data/lib/array_enum.rb CHANGED
@@ -17,11 +17,17 @@ module ArrayEnum
17
17
  mapping_hash
18
18
  end
19
19
 
20
- define_singleton_method("with_#{attr_name}".to_sym) do |values|
21
- db_values = Array(values).map do |value|
22
- mapping_hash[value] || raise(ArgumentError, MISSING_VALUE_MESSAGE % {value: value, attr: attr_name})
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, MISSING_VALUE_MESSAGE % {value: value, attr: attr_name})
28
+ end
29
+ where("#{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
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.3.0
4
+ version: 1.4.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: 2021-06-07 00:00:00.000000000 Z
11
+ date: 2022-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -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