enum_ish 1.2.2 → 1.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +47 -0
- data/CHANGELOG.md +20 -0
- data/gemfiles/rails61.gemfile +5 -0
- data/lib/enum_ish.rb +6 -3
- data/lib/enum_ish/active_record_definer.rb +7 -1
- data/lib/enum_ish/definer.rb +17 -13
- data/lib/enum_ish/dictionary.rb +19 -6
- data/lib/enum_ish/enum.rb +17 -0
- data/lib/enum_ish/version.rb +1 -1
- metadata +5 -4
- data/.travis.yml +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aad3607cc8b6c21e15582ee8ec3240d664c9c145c270f322f2e9cda3966ef920
|
4
|
+
data.tar.gz: 4dc14db67c76e0d7e23b8ce287605b1a10c965824b19aa45dc53f3f855a1b092
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d4f274982727f7aa04b6b71b3a6017ba598fa27287065a1703412f94a58332eb478cb4ea7da0c218fbb2fd0f7cc08f5439c1520986ec7107a6fbd2b093a2133
|
7
|
+
data.tar.gz: a2038ffa1f1ab5ce99d5166565339f84b6711698537dda7c918d1db9873780bf60bfcdd2e7db38f68282ff58410d866f9b2a02a60d05c6d7e771b13c0dcbc82b
|
@@ -0,0 +1,47 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
runs-on: ubuntu-16.04
|
8
|
+
strategy:
|
9
|
+
fail-fast: false
|
10
|
+
matrix:
|
11
|
+
ruby: [2.3, 2.4, 2.5, 2.6, 2.7, 3.0]
|
12
|
+
gemfile: ['rails50', 'rails51', 'rails52', 'rails60', 'rails61']
|
13
|
+
exclude:
|
14
|
+
- ruby: 2.3
|
15
|
+
gemfile: rails60
|
16
|
+
- ruby: 2.3
|
17
|
+
gemfile: rails61
|
18
|
+
- ruby: 2.4
|
19
|
+
gemfile: rails60
|
20
|
+
- ruby: 2.4
|
21
|
+
gemfile: rails61
|
22
|
+
- ruby: 3.0
|
23
|
+
gemfile: rails50
|
24
|
+
- ruby: 3.0
|
25
|
+
gemfile: rails51
|
26
|
+
- ruby: 3.0
|
27
|
+
gemfile: rails52
|
28
|
+
|
29
|
+
name: ruby ${{ matrix.ruby }}, ${{ matrix.gemfile }}
|
30
|
+
|
31
|
+
env:
|
32
|
+
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
|
33
|
+
|
34
|
+
steps:
|
35
|
+
- uses: actions/checkout@v2
|
36
|
+
- uses: ruby/setup-ruby@v1
|
37
|
+
with:
|
38
|
+
ruby-version: ${{ matrix.ruby }}
|
39
|
+
bundler-cache: true
|
40
|
+
- name: Prepare test
|
41
|
+
run: |
|
42
|
+
cd spec/dummy
|
43
|
+
BUNDLE_GEMFILE=../../${{ env.BUNDLE_GEMFILE }} RAILS_ENV=test bundle exec rake db:create db:migrate db:seed
|
44
|
+
cd ../..
|
45
|
+
- name: Run test
|
46
|
+
run: |
|
47
|
+
bundle exec rspec
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,25 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 1.3.3
|
4
|
+
|
5
|
+
* Support rails 6.1.
|
6
|
+
|
7
|
+
## 1.3.2
|
8
|
+
|
9
|
+
* Fix deprecation warning for ruby 2.7.
|
10
|
+
|
11
|
+
## 1.3.1
|
12
|
+
|
13
|
+
* Fix default value for model included ActiveModel.
|
14
|
+
|
15
|
+
## 1.3.0
|
16
|
+
|
17
|
+
* Support array field.
|
18
|
+
|
19
|
+
## 1.2.3
|
20
|
+
|
21
|
+
* Keep enum definitions as a hash internally.
|
22
|
+
|
3
23
|
## 1.2.2
|
4
24
|
|
5
25
|
* Support assignment of string in addition to symbol.
|
data/lib/enum_ish.rb
CHANGED
@@ -11,9 +11,8 @@ module EnumIsh
|
|
11
11
|
def enum_ish(name, map, config = {})
|
12
12
|
enum = Enum.new(name, map, config)
|
13
13
|
|
14
|
-
self.
|
15
|
-
self._enum_ish_enums
|
16
|
-
self._enum_ish_enums << enum
|
14
|
+
self._enum_ish_enums ||= {}
|
15
|
+
self._enum_ish_enums[name.to_sym] = enum
|
17
16
|
|
18
17
|
if defined?(ActiveRecord::Base) && self.ancestors.include?(ActiveRecord::Base)
|
19
18
|
ActiveRecordDefiner.new(self).define(enum)
|
@@ -21,4 +20,8 @@ module EnumIsh
|
|
21
20
|
Definer.new(self).define(enum)
|
22
21
|
end
|
23
22
|
end
|
23
|
+
|
24
|
+
def self.extended(klass)
|
25
|
+
klass.class_attribute :_enum_ish_enums
|
26
|
+
end
|
24
27
|
end
|
@@ -24,7 +24,13 @@ module EnumIsh
|
|
24
24
|
enum.mapping.fetch(value, value)
|
25
25
|
end
|
26
26
|
|
27
|
-
|
27
|
+
args =
|
28
|
+
if ActiveRecord.version > Gem::Version.new('6.1.0.a')
|
29
|
+
[enum.name]
|
30
|
+
else
|
31
|
+
[enum.name, :enum]
|
32
|
+
end
|
33
|
+
decorate_attribute_type(*args) do |subtype|
|
28
34
|
EnumIsh::ActiveRecordEnumType.new(enum.name, enum.mapping, subtype)
|
29
35
|
end
|
30
36
|
end
|
data/lib/enum_ish/definer.rb
CHANGED
@@ -15,27 +15,31 @@ module EnumIsh
|
|
15
15
|
def define_text(enum)
|
16
16
|
@klass.class_eval do
|
17
17
|
define_method "#{Config.text_prefix}#{enum.name}#{Config.text_suffix}" do |options = {}|
|
18
|
-
|
19
|
-
|
20
|
-
dic[value] || value
|
18
|
+
dict = Dictionary.new(self.class, enum, options)
|
19
|
+
dict.translate_value(public_send(enum.name))
|
21
20
|
end
|
22
|
-
end
|
21
|
+
end
|
23
22
|
end
|
24
23
|
|
25
24
|
def define_options(enum)
|
26
25
|
@klass.class_eval do
|
27
26
|
define_singleton_method "#{Config.options_prefix}#{enum.name}#{Config.options_suffix}" do |options = {}|
|
28
|
-
|
29
|
-
|
27
|
+
dict = Dictionary.new(self, enum, options)
|
28
|
+
dict.translate_options
|
30
29
|
end
|
31
30
|
end
|
32
31
|
end
|
33
32
|
|
34
33
|
def define_predicate(enum)
|
35
|
-
enum.mapping.each do |
|
34
|
+
enum.mapping.each do |enum_key, enum_value|
|
36
35
|
@klass.class_eval do
|
37
|
-
define_method "#{enum.name}_#{
|
38
|
-
public_send(enum.name)
|
36
|
+
define_method "#{enum.name}_#{enum_key}?".tr('.', '_') do
|
37
|
+
value = public_send(enum.name)
|
38
|
+
if value.is_a?(Array)
|
39
|
+
value == [enum_value]
|
40
|
+
else
|
41
|
+
value == enum_value
|
42
|
+
end
|
39
43
|
end
|
40
44
|
end
|
41
45
|
end
|
@@ -45,15 +49,15 @@ module EnumIsh
|
|
45
49
|
mod = Module.new
|
46
50
|
mod.module_eval do
|
47
51
|
define_method :initialize do |*args|
|
52
|
+
super(*args)
|
48
53
|
if respond_to?(enum.name) && public_send(enum.name).nil?
|
49
54
|
default = enum.setting[:default]
|
50
55
|
default = instance_exec(&default) if default.kind_of?(Proc)
|
51
56
|
public_send("#{enum.name}=", default)
|
52
57
|
end
|
53
|
-
super(*args)
|
54
58
|
end
|
55
59
|
end
|
56
|
-
@klass.
|
60
|
+
@klass.send(:include, mod)
|
57
61
|
end
|
58
62
|
|
59
63
|
def define_accessor(enum)
|
@@ -62,10 +66,10 @@ module EnumIsh
|
|
62
66
|
instance_variable_get("@#{enum.name}")
|
63
67
|
end
|
64
68
|
define_method "#{enum.name}" do
|
65
|
-
enum.
|
69
|
+
enum.to_sym(instance_variable_get("@#{enum.name}"))
|
66
70
|
end
|
67
71
|
define_method "#{enum.name}=" do |value|
|
68
|
-
instance_variable_set("@#{enum.name}", enum.
|
72
|
+
instance_variable_set("@#{enum.name}", enum.to_raw(value))
|
69
73
|
end
|
70
74
|
end
|
71
75
|
end
|
data/lib/enum_ish/dictionary.rb
CHANGED
@@ -1,17 +1,30 @@
|
|
1
1
|
module EnumIsh
|
2
2
|
class Dictionary
|
3
|
-
def initialize(klass)
|
3
|
+
def initialize(klass, enum, options = {})
|
4
4
|
@klass = klass
|
5
|
+
@dict = load(enum, options)
|
5
6
|
end
|
6
7
|
|
7
|
-
def
|
8
|
-
|
9
|
-
|
8
|
+
def translate_value(value)
|
9
|
+
if value.is_a?(Array)
|
10
|
+
value.map { |v| @dict[v] || v }
|
11
|
+
else
|
12
|
+
@dict[value] || value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def translate_options
|
17
|
+
@dict.to_a.map { |value, label| [label, value] }
|
10
18
|
end
|
11
19
|
|
12
20
|
private
|
13
21
|
|
14
|
-
def
|
22
|
+
def load(enum, options)
|
23
|
+
dict = translate_dict(enum, options)
|
24
|
+
filter(dict, options)
|
25
|
+
end
|
26
|
+
|
27
|
+
def translate_dict(enum, options)
|
15
28
|
dict = load_dict(enum, options)
|
16
29
|
translated = enum.mapping.map { |k, v| dict[k] ? [k, dict[k]] : [k, v.to_s] }.to_h
|
17
30
|
translated = translated.map { |k, v| [enum.mapping[k], v] }.to_h unless enum.setting[:accessor]
|
@@ -20,7 +33,7 @@ module EnumIsh
|
|
20
33
|
|
21
34
|
def load_dict(enum, options)
|
22
35
|
key = i18n_key(enum, options)
|
23
|
-
dict = I18n.t("enum_ish.#{@klass.name.underscore}.#{key}", i18n_options(enum, options))
|
36
|
+
dict = I18n.t("enum_ish.#{@klass.name.underscore}.#{key}", **i18n_options(enum, options))
|
24
37
|
dict.map { |k, v| [k.to_s.to_sym, v.to_s] }.to_h
|
25
38
|
end
|
26
39
|
|
data/lib/enum_ish/enum.rb
CHANGED
@@ -10,6 +10,23 @@ module EnumIsh
|
|
10
10
|
@setting = init_setting(setting)
|
11
11
|
end
|
12
12
|
|
13
|
+
def to_raw(value)
|
14
|
+
if value.is_a?(Array)
|
15
|
+
value.map { |v| @mapping.fetch(v, v) }
|
16
|
+
else
|
17
|
+
@mapping.fetch(value, value)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_sym(value)
|
22
|
+
inverted = @mapping.invert
|
23
|
+
if value.is_a?(Array)
|
24
|
+
value.map { |v| inverted[v] }
|
25
|
+
else
|
26
|
+
inverted[value]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
13
30
|
private
|
14
31
|
|
15
32
|
def init_mapping(mapping)
|
data/lib/enum_ish/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enum_ish
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yoshikazu Kaneta
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -101,9 +101,9 @@ executables: []
|
|
101
101
|
extensions: []
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
|
+
- ".github/workflows/ci.yml"
|
104
105
|
- ".gitignore"
|
105
106
|
- ".rspec"
|
106
|
-
- ".travis.yml"
|
107
107
|
- CHANGELOG.md
|
108
108
|
- Gemfile
|
109
109
|
- LICENSE
|
@@ -114,6 +114,7 @@ files:
|
|
114
114
|
- gemfiles/rails51.gemfile
|
115
115
|
- gemfiles/rails52.gemfile
|
116
116
|
- gemfiles/rails60.gemfile
|
117
|
+
- gemfiles/rails61.gemfile
|
117
118
|
- lib/enum_ish.rb
|
118
119
|
- lib/enum_ish/active_record_definer.rb
|
119
120
|
- lib/enum_ish/active_record_enum_type.rb
|
@@ -141,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
142
|
- !ruby/object:Gem::Version
|
142
143
|
version: '0'
|
143
144
|
requirements: []
|
144
|
-
rubygems_version: 3.
|
145
|
+
rubygems_version: 3.2.3
|
145
146
|
signing_key:
|
146
147
|
specification_version: 4
|
147
148
|
summary: A ruby and rails extension to generate enum-like methods
|
data/.travis.yml
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
rvm:
|
3
|
-
- 2.3
|
4
|
-
- 2.4
|
5
|
-
- 2.5
|
6
|
-
- 2.6
|
7
|
-
gemfile:
|
8
|
-
- gemfiles/rails50.gemfile
|
9
|
-
- gemfiles/rails51.gemfile
|
10
|
-
- gemfiles/rails52.gemfile
|
11
|
-
- gemfiles/rails60.gemfile
|
12
|
-
matrix:
|
13
|
-
exclude:
|
14
|
-
- rvm: 2.3
|
15
|
-
gemfile: gemfiles/rails60.gemfile
|
16
|
-
- rvm: 2.4
|
17
|
-
gemfile: gemfiles/rails60.gemfile
|
18
|
-
before_script:
|
19
|
-
- cd spec/dummy
|
20
|
-
- bundle exec rake db:create db:migrate db:seed RAILS_ENV=test
|
21
|
-
- cd ../..
|
22
|
-
script:
|
23
|
-
- bundle exec rspec
|