sql_enum 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ba5a5a0695bc9c11496ca90509646d98e2e005e7c89664aba78acb1de45d270
4
- data.tar.gz: 1cb90fe77fcc9b7e2d84c0fe9b9b41ddee264de6e488d5de8dbc9462371a8ecd
3
+ metadata.gz: 2db9bae7e87154948d42ff23710df268a6f942c66aa52b42dd12c78606141c32
4
+ data.tar.gz: 4ad6e4b93c4ff9bfeaa2b143a96b2b5327c0c2e774320c6e9b2e2c7f569e7155
5
5
  SHA512:
6
- metadata.gz: 795a28c30fc3fddb87865fc82325243c67a009694c409ffe880dace43c0d601cdcc32b4d9806a9d9c3220616dcc82be9b0e57ca8ccb3bb14c2d6562457c588a5
7
- data.tar.gz: 67835417b809b63b7d97deb3531d059f3df63d1450406b0216cb81540a7276c691635126da7afdbf1dcf87fb128f7fe63cc8e196073fac55254e321ca1b66b58
6
+ metadata.gz: 55105be1538e1f81803acd39b5e202ee0ed685d583fa5c7e99d650edabbe43bf6e13dfdac59a134c5511a00e767a97546e33fd6340f69f0bdc9a0791749bcb12
7
+ data.tar.gz: f63345f9b4478d15c3138f405c7ab6d0b7952bfa6294761d01db50bf164a231fb74dd8f5f2c86f2cae9f04a348539c02d6f236d991b66e4a30bb37f3736677ec
@@ -7,10 +7,20 @@ on:
7
7
 
8
8
  jobs:
9
9
  rspec:
10
+ strategy:
11
+ matrix:
12
+ rails:
13
+ - '5.1'
14
+ - '5.2'
15
+ - '6.0'
16
+ - '6.1'
17
+
10
18
  runs-on: ubuntu-latest
11
19
 
20
+ name: RSpec (Rails ${{ matrix.rails }})
21
+
12
22
  steps:
13
23
  - uses: actions/checkout@v2
14
24
 
15
25
  - name: Test
16
- run: docker-compose run --rm rspec
26
+ run: docker-compose run --rm rspec bundle exec appraisal rails-${{ matrix.rails }} rspec
data/.gitignore CHANGED
@@ -9,3 +9,5 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+
13
+ gemfiles/*.lock
data/Appraisals ADDED
@@ -0,0 +1,15 @@
1
+ appraise "rails-5.1" do
2
+ gem "rails", "~> 5.1.0"
3
+ end
4
+
5
+ appraise "rails-5.2" do
6
+ gem "rails", "~> 5.2.0"
7
+ end
8
+
9
+ appraise "rails-6.0" do
10
+ gem "rails", "~> 6.0.0"
11
+ end
12
+
13
+ appraise "rails-6.1" do
14
+ gem "rails", "~> 6.1.0"
15
+ end
data/Dockerfile CHANGED
@@ -1,12 +1,9 @@
1
- FROM ruby:3.0
2
-
3
- # throw errors if Gemfile has been modified since Gemfile.lock
4
- RUN bundle config --global frozen 1
1
+ FROM ruby:2.7
5
2
 
6
3
  WORKDIR /usr/src/app
7
4
 
8
- # Bundler caching
9
- RUN mkdir -p lib/sql_enum
5
+ RUN mkdir -p lib/sql_enum gemfiles
10
6
  COPY lib/sql_enum/version.rb ./lib/sql_enum/
11
- COPY sql_enum.gemspec Gemfile Gemfile.lock ./
12
- RUN bundle install
7
+ COPY gemfiles/*.gemfile gemfiles/
8
+ COPY sql_enum.gemspec Gemfile Gemfile.lock Appraisals ./
9
+ RUN bundle install && exec appraisal install
data/Gemfile.lock CHANGED
@@ -1,9 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sql_enum (0.3.0)
5
- activerecord (>= 5.0)
6
- activesupport (>= 5.0)
4
+ sql_enum (0.3.1)
5
+ activerecord (>= 5.1)
6
+ activesupport (>= 5.1)
7
7
  mysql2
8
8
 
9
9
  GEM
@@ -20,13 +20,25 @@ GEM
20
20
  minitest (>= 5.1)
21
21
  tzinfo (~> 2.0)
22
22
  zeitwerk (~> 2.3)
23
+ appraisal (2.4.1)
24
+ bundler
25
+ rake
26
+ thor (>= 0.14.0)
23
27
  concurrent-ruby (1.1.9)
28
+ debug (1.3.2)
29
+ irb (>= 1.3.6)
30
+ reline (>= 0.2.7)
24
31
  diff-lcs (1.3)
25
32
  i18n (1.8.10)
26
33
  concurrent-ruby (~> 1.0)
34
+ io-console (0.5.9)
35
+ irb (1.3.7)
36
+ reline (>= 0.2.7)
27
37
  minitest (5.14.4)
28
38
  mysql2 (0.5.3)
29
39
  rake (13.0.3)
40
+ reline (0.2.7)
41
+ io-console (~> 0.5)
30
42
  rspec (3.7.0)
31
43
  rspec-core (~> 3.7.0)
32
44
  rspec-expectations (~> 3.7.0)
@@ -40,15 +52,19 @@ GEM
40
52
  diff-lcs (>= 1.2.0, < 2.0)
41
53
  rspec-support (~> 3.7.0)
42
54
  rspec-support (3.7.1)
55
+ thor (1.1.0)
43
56
  tzinfo (2.0.4)
44
57
  concurrent-ruby (~> 1.0)
45
58
  zeitwerk (2.5.1)
46
59
 
47
60
  PLATFORMS
48
61
  ruby
62
+ x86_64-linux
49
63
 
50
64
  DEPENDENCIES
65
+ appraisal
51
66
  bundler
67
+ debug
52
68
  rake (~> 13.0)
53
69
  rspec (~> 3.0)
54
70
  sql_enum!
data/docker-compose.yml CHANGED
@@ -8,7 +8,7 @@ services:
8
8
  depends_on:
9
9
  mysql:
10
10
  condition: service_healthy
11
- command: bundle exec rspec
11
+ command: bundle exec appraisal rspec
12
12
 
13
13
  mysql:
14
14
  image: mysql:5.7
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://gem-proxy.chime.com"
4
+
5
+ gem "rails", "~> 5.1.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://gem-proxy.chime.com"
4
+
5
+ gem "rails", "~> 5.2.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://gem-proxy.chime.com"
4
+
5
+ gem "rails", "~> 6.0.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://gem-proxy.chime.com"
4
+
5
+ gem "rails", "~> 6.1.0"
6
+
7
+ gemspec path: "../"
@@ -3,18 +3,41 @@ module SqlEnum
3
3
  def sql_enum(column_name, options = {})
4
4
  # Query values
5
5
  enum_column = EnumColumn.new(table_name, column_name)
6
- values = enum_column.values.to_h { |value| [value.to_sym, value.to_s] }
6
+ values_map = enum_column.values.to_h { |value| [value.to_sym, value.to_s] }
7
7
 
8
8
  # Check option defaults
9
9
  prefix = options.fetch(:_prefix, !!SqlEnum.configuration&.default_prefix)
10
10
  suffix = options.fetch(:_suffix, !!SqlEnum.configuration&.default_suffix)
11
11
 
12
12
  # Define enum using Rails enum
13
- enum(column_name => values, _prefix: prefix, _suffix: suffix)
13
+ enum(column_name => values_map, _prefix: prefix, _suffix: suffix)
14
14
 
15
15
  # Override reader to return symbols
16
- concerning "SqlEnum#{column_name.to_s.camelize}" do
17
- define_method(column_name) { super()&.to_sym }
16
+ type_definition = ->(subtype) { EnumType.new(attr, send(column_name.to_s.pluralize), subtype) }
17
+ case method(:decorate_attribute_type).arity
18
+ when 2 # Rails 5.1, 5.2, 6.0
19
+ decorate_attribute_type(column_name, :enum, &type_definition)
20
+ else
21
+ decorate_attribute_type(column_name, &type_definition)
22
+ end
23
+
24
+ prefix_str = format_affix(column_name, prefix, suffix: '_')
25
+ suffix_str = format_affix(column_name, suffix, prefix: '_')
26
+
27
+ # Fix query methods to compare symbols to symbols
28
+ values_map.each_value do |value|
29
+ method_name = "#{prefix_str}#{value}#{suffix_str}"
30
+ define_method("#{method_name}?") { self[column_name] == value.to_sym }
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def format_affix(column_name, affix, prefix: '', suffix: '')
37
+ if affix == true
38
+ "#{prefix}#{column_name}#{suffix}"
39
+ elsif affix
40
+ "#{prefix}#{affix}#{suffix}"
18
41
  end
19
42
  end
20
43
  end
@@ -21,13 +21,13 @@ module SqlEnum
21
21
  end
22
22
 
23
23
  def schema_values_query
24
- <<~EOSQL
25
- SELECT column_type
26
- FROM information_schema.COLUMNS
27
- WHERE TABLE_SCHEMA = '#{database_name}'
28
- AND TABLE_NAME = '#{table_name}'
29
- AND COLUMN_NAME = '#{column_name}'
30
- EOSQL
24
+ <<~SQL
25
+ SELECT column_type
26
+ FROM information_schema.COLUMNS
27
+ WHERE TABLE_SCHEMA = '#{database_name}'
28
+ AND TABLE_NAME = '#{table_name}'
29
+ AND COLUMN_NAME = '#{column_name}'
30
+ SQL
31
31
  end
32
32
  end
33
33
  end
@@ -0,0 +1,12 @@
1
+ module SqlEnum
2
+ # EnumType that always returns a symbol
3
+ class EnumType < ActiveRecord::Enum::EnumType
4
+ def cast(arg)
5
+ super&.to_sym
6
+ end
7
+
8
+ def deserialize(arg)
9
+ super&.to_sym
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module SqlEnum
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
data/lib/sql_enum.rb CHANGED
@@ -20,5 +20,6 @@ require_relative 'active_record/connection_adapters/mysql/column_methods'
20
20
  require_relative 'sql_enum/version'
21
21
  require_relative 'sql_enum/configuration'
22
22
  require_relative 'sql_enum/enum_column'
23
+ require_relative 'sql_enum/enum_type'
23
24
  require_relative 'sql_enum/class_methods'
24
25
  require_relative 'sql_enum/active_record'
data/sql_enum.gemspec CHANGED
@@ -20,11 +20,13 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ["lib"]
22
22
 
23
- spec.add_dependency "activesupport", ">= 5.0"
24
- spec.add_dependency "activerecord", ">= 5.0"
23
+ spec.add_dependency "activesupport", ">= 5.1"
24
+ spec.add_dependency "activerecord", ">= 5.1"
25
25
  spec.add_dependency "mysql2"
26
26
 
27
27
  spec.add_development_dependency "bundler"
28
28
  spec.add_development_dependency "rake", "~> 13.0"
29
+ spec.add_development_dependency "debug"
29
30
  spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "appraisal"
30
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sql_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fletcher Fowler
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-28 00:00:00.000000000 Z
11
+ date: 2021-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.0'
19
+ version: '5.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '5.0'
26
+ version: '5.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '5.0'
33
+ version: '5.1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '5.0'
40
+ version: '5.1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mysql2
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '13.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: debug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rspec
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +108,20 @@ dependencies:
94
108
  - - "~>"
95
109
  - !ruby/object:Gem::Version
96
110
  version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: appraisal
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
97
125
  description:
98
126
  email:
99
127
  - fletch@fzf.me
@@ -106,6 +134,7 @@ files:
106
134
  - ".gitignore"
107
135
  - ".rspec"
108
136
  - ".travis.yml"
137
+ - Appraisals
109
138
  - CODE_OF_CONDUCT.md
110
139
  - Dockerfile
111
140
  - Gemfile
@@ -116,6 +145,10 @@ files:
116
145
  - bin/console
117
146
  - bin/setup
118
147
  - docker-compose.yml
148
+ - gemfiles/rails_5.1.gemfile
149
+ - gemfiles/rails_5.2.gemfile
150
+ - gemfiles/rails_6.0.gemfile
151
+ - gemfiles/rails_6.1.gemfile
119
152
  - lib/active_record/connection_adapters/abstract_mysql.rb
120
153
  - lib/active_record/connection_adapters/mysql/column_methods.rb
121
154
  - lib/active_record/connection_adapters/mysql2.rb
@@ -125,6 +158,7 @@ files:
125
158
  - lib/sql_enum/class_methods.rb
126
159
  - lib/sql_enum/configuration.rb
127
160
  - lib/sql_enum/enum_column.rb
161
+ - lib/sql_enum/enum_type.rb
128
162
  - lib/sql_enum/version.rb
129
163
  - sql_enum.gemspec
130
164
  homepage: https://github.com/1debit/sql_enum