exnum 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.circleci/config.yml +36 -0
- data/README.md +57 -31
- data/exnum.gemspec +1 -1
- data/lib/exnum/active_record/exnum.rb +105 -59
- data/lib/exnum/version.rb +1 -1
- metadata +5 -6
- data/.travis.yml +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 295aa28e2fb1dcfbfeddd68ac9feac4bcab8fbb746d7c594a52d6f2f5ec1cc66
|
4
|
+
data.tar.gz: 195d6394fbaddb4a56a3c306a7de22d44e3890da2c9f5b6f2e55ea192e6d5082
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd5dacea7a74dbd63ae7fe1de7b42d1f75be5123250a0b1bfe28f1e98623fb12ceadeedd93479b653a0322026dd75322f66faea045fb60e397486fdfc7dd2460
|
7
|
+
data.tar.gz: 0c940ca68a4711848a2afed0b027f9a242895cc535f0d9b7c1efbd16a82610939fac2339940fb6c537999bf9716df1d16a5642d1d68d0fe70bb8d3c440d3f231
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# Ruby CircleCI 2.0 configuration file
|
2
|
+
version: 2
|
3
|
+
jobs:
|
4
|
+
build:
|
5
|
+
docker:
|
6
|
+
- image: circleci/ruby:2.6.3
|
7
|
+
|
8
|
+
working_directory: ~/repo
|
9
|
+
|
10
|
+
steps:
|
11
|
+
- checkout
|
12
|
+
|
13
|
+
- run:
|
14
|
+
name: Setup Code Climate test-reporter
|
15
|
+
command: |
|
16
|
+
# download test reporter as a static binary
|
17
|
+
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
18
|
+
chmod +x ./cc-test-reporter
|
19
|
+
|
20
|
+
- run:
|
21
|
+
name: install dependencies
|
22
|
+
command: |
|
23
|
+
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
24
|
+
|
25
|
+
- run:
|
26
|
+
name: Run backend tests
|
27
|
+
command: |
|
28
|
+
./cc-test-reporter before-build
|
29
|
+
bundle exec rake
|
30
|
+
./cc-test-reporter after-build --coverage-input-type simplecov --exit-code $?
|
31
|
+
|
32
|
+
workflows:
|
33
|
+
version: 2
|
34
|
+
workflow:
|
35
|
+
jobs:
|
36
|
+
- build
|
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# Exnum
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/exnum.svg)](https://badge.fury.io/rb/exnum)
|
4
|
-
[![
|
5
|
-
[![
|
6
|
-
[![Test Coverage](https://codeclimate.com/
|
4
|
+
[![CircleCI](https://circleci.com/gh/Kta-M/exnum.svg?style=svg)](https://circleci.com/gh/Kta-M/exnum)
|
5
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/d8d694f943658d8a5506/maintainability)](https://codeclimate.com/github/Kta-M/exnum/maintainability)
|
6
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/d8d694f943658d8a5506/test_coverage)](https://codeclimate.com/github/Kta-M/exnum/test_coverage)
|
7
7
|
|
8
8
|
Exnum is enum extention for Rails.
|
9
|
-
This gem extends enum about i18n and
|
9
|
+
This gem extends enum about i18n, associated parameters, and array for select box.
|
10
10
|
|
11
11
|
## Installation
|
12
12
|
|
@@ -18,30 +18,51 @@ gem 'exnum'
|
|
18
18
|
|
19
19
|
And then execute:
|
20
20
|
|
21
|
-
|
21
|
+
```
|
22
|
+
$ bundle
|
23
|
+
```
|
22
24
|
|
23
25
|
Or install it yourself as:
|
24
26
|
|
25
|
-
|
27
|
+
```
|
28
|
+
$ gem install exnum
|
29
|
+
```
|
26
30
|
|
27
31
|
## Usage
|
28
32
|
|
29
|
-
|
30
|
-
```
|
33
|
+
```ruby
|
31
34
|
class User < ActiveRecord::Base
|
32
35
|
# the value of the key "val" defines enum values
|
33
36
|
# and the others defines associated parameters
|
34
37
|
exnum role: {
|
35
|
-
guest: {val: 10, label: :red },
|
36
|
-
general: {val: 20, label: :green, permission: false
|
37
|
-
admin: {val: 30, label: :blue, permission: true},
|
38
|
+
guest: {val: 10, label: :red, selectable: true, },
|
39
|
+
general: {val: 20, label: :green, selectable: true, permission: false},
|
40
|
+
admin: {val: 30, label: :blue, selectable: false, permission: true},
|
38
41
|
}
|
39
42
|
end
|
43
|
+
```
|
40
44
|
|
45
|
+
### Assosiated parameter extention
|
46
|
+
```ruby
|
47
|
+
User.role_labels
|
48
|
+
# => {"guest" => :red, "general" => :green, "admin" => :blue}
|
49
|
+
User.role_labels{|p| p[:selectable]}
|
50
|
+
# => {"guest" => :red, "general" => :green}
|
51
|
+
User.role_permissions
|
52
|
+
# => {"guest" => nil, "general" => false, "admin" => true}
|
41
53
|
```
|
42
54
|
|
43
|
-
|
55
|
+
```ruby
|
56
|
+
user = User.new(role: :general)
|
57
|
+
user.role_label
|
58
|
+
# => :green
|
59
|
+
user.role_permission
|
60
|
+
# => false
|
44
61
|
```
|
62
|
+
|
63
|
+
### I18n extention
|
64
|
+
|
65
|
+
```yaml
|
45
66
|
ja:
|
46
67
|
activerecord:
|
47
68
|
enum:
|
@@ -50,47 +71,51 @@ ja:
|
|
50
71
|
guest: ゲスト
|
51
72
|
general: 一般ユーザー
|
52
73
|
admin: 管理者
|
53
|
-
|
54
74
|
```
|
55
75
|
|
56
|
-
|
57
|
-
```
|
76
|
+
```ruby
|
58
77
|
User.roles_i18n
|
59
78
|
# => {"guest" => "ゲスト", "general" => "一般ユーザー", "admin" => "管理者"}
|
79
|
+
User.roles_i18n{|p| p[:selectable]}
|
80
|
+
# => {"guest" => "ゲスト", "general" => "一般ユーザー"}
|
81
|
+
```
|
60
82
|
|
83
|
+
```ruby
|
61
84
|
user = User.new(role: :guest)
|
62
85
|
user.role_i18n
|
63
86
|
# => "ゲスト"
|
64
87
|
```
|
65
88
|
|
66
|
-
|
89
|
+
### Array for select box extention
|
90
|
+
```ruby
|
91
|
+
User.roles_for_select
|
92
|
+
# => [["ゲスト", "guest"], ["一般ユーザー", "general"], ["管理者", "admin"]]
|
93
|
+
User.roles_for_select{|p| p[:selectable]}
|
94
|
+
# => [["ゲスト", "guest"], ["一般ユーザー", "general"]]
|
67
95
|
```
|
68
|
-
User.role_labels
|
69
|
-
# => {"guest" => :red, "general" => :green, "admin" => :blue}
|
70
|
-
User.role_permissions
|
71
|
-
# => {"guest" => nil, "general" => false, "admin" => true}
|
72
96
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
user.role_permission
|
77
|
-
# => false
|
97
|
+
You can use it like this:
|
98
|
+
```ruby
|
99
|
+
<%= f.select :role, User.roles_for_select %>
|
78
100
|
```
|
79
101
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
Then you can use extention only about i18n.
|
84
|
-
```
|
102
|
+
### enum
|
103
|
+
Of cause, you can use methods provided by `enum`.
|
104
|
+
```ruby
|
85
105
|
class User < ActiveRecord::Base
|
86
106
|
exnum role: [:guest, :general, :admin]
|
87
107
|
end
|
88
108
|
```
|
89
|
-
```
|
109
|
+
```ruby
|
90
110
|
class User < ActiveRecord::Base
|
91
111
|
exnum role: {guest: 10, general: 20, admin: 30}
|
92
112
|
end
|
93
113
|
```
|
114
|
+
```ruby
|
115
|
+
class User < ActiveRecord::Base
|
116
|
+
exnum role: {guest: 10, general: 20, admin: 30}, _prefix: true
|
117
|
+
end
|
118
|
+
```
|
94
119
|
|
95
120
|
## Contributing
|
96
121
|
|
@@ -104,3 +129,4 @@ end
|
|
104
129
|
|
105
130
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
106
131
|
|
132
|
+
|
data/exnum.gemspec
CHANGED
@@ -25,5 +25,5 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.add_development_dependency "rspec", "~> 3.0"
|
26
26
|
spec.add_development_dependency "sqlite3", "~> 1.0"
|
27
27
|
spec.add_development_dependency "pry-rails"
|
28
|
-
spec.add_development_dependency "
|
28
|
+
spec.add_development_dependency "simplecov"
|
29
29
|
end
|
@@ -1,8 +1,10 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module Exnum
|
3
|
+
|
3
4
|
def self.prepended(base)
|
4
5
|
base.singleton_class.class_eval { self.prepend(ClassMethods) }
|
5
6
|
end
|
7
|
+
|
6
8
|
module ClassMethods
|
7
9
|
def extended(base)
|
8
10
|
base.class_attribute(:defined_params, instance_writer: false)
|
@@ -19,65 +21,77 @@ module ActiveRecord
|
|
19
21
|
#--------------------------------------------------------------------------
|
20
22
|
|
21
23
|
def exnum(definitions)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
enum_definitions = definitions.each_with_object({}) do |(name, values), ret|
|
25
|
+
ret[name] = extract_enums(values)
|
26
|
+
end
|
27
|
+
enum(enum_definitions)
|
28
|
+
|
29
|
+
pram_definitions = definitions.each_with_object({}) do |(name, values), ret|
|
30
|
+
next if %i[_prefix _suffix].include?(name)
|
31
|
+
|
32
|
+
ret[name] = extract_params(values)
|
33
|
+
self.send(name.to_s.pluralize).each do |k, v|
|
34
|
+
ret[name][k.to_sym] ||= {}
|
35
|
+
ret[name][k.to_sym][:val] = v
|
28
36
|
end
|
29
|
-
ret
|
30
37
|
end
|
31
38
|
|
32
|
-
|
33
|
-
|
34
|
-
|
39
|
+
enum_i18n(pram_definitions)
|
40
|
+
enum_param(pram_definitions)
|
41
|
+
enum_for_select(pram_definitions)
|
35
42
|
end
|
36
43
|
|
37
|
-
|
44
|
+
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
38
45
|
|
39
46
|
private
|
40
|
-
def extract_enums(
|
41
|
-
values
|
47
|
+
def extract_enums(values)
|
48
|
+
return values unless values.kind_of?(Hash)
|
49
|
+
|
50
|
+
values.each_with_object({}) do |(field, value), ret|
|
42
51
|
ret[field.to_sym] = (value.kind_of?(Hash) ? value[:val] : value)
|
43
|
-
ret
|
44
52
|
end
|
45
53
|
end
|
46
54
|
|
47
|
-
def extract_params(
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
ret
|
55
|
+
def extract_params(values)
|
56
|
+
return {} unless values.kind_of?(Hash)
|
57
|
+
|
58
|
+
target_values = values.select{|_field, value| value.kind_of?(Hash)}
|
59
|
+
target_values.each_with_object({}) do |(field, value), ret|
|
60
|
+
ret[field.to_sym] = value
|
53
61
|
end
|
54
62
|
end
|
55
63
|
|
56
|
-
|
57
|
-
klass = self
|
58
|
-
definitions.delete(:_prefix)
|
59
|
-
definitions.delete(:_suffix)
|
64
|
+
#--------------------------------------------------------------------------
|
60
65
|
|
66
|
+
def enum_i18n(definitions)
|
61
67
|
definitions.each do |name, values|
|
68
|
+
enum_i18n_class_method(name, values)
|
69
|
+
enum_i18n_instance_method(name)
|
70
|
+
end
|
71
|
+
end
|
62
72
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
73
|
+
# define class method which returns i18n string of each satus
|
74
|
+
def enum_i18n_class_method(name, values)
|
75
|
+
klass = self
|
76
|
+
method_name = "#{name.to_s.pluralize}_i18n"
|
77
|
+
detect_enum_conflict!(name, method_name, true)
|
78
|
+
klass.singleton_class.send(:define_method, method_name) do |&block|
|
79
|
+
i18n_hash = ActiveSupport::HashWithIndifferentAccess.new
|
80
|
+
values.each_with_object(i18n_hash) do |(enum_name, value), ret|
|
81
|
+
next if block.present? && !block.call(value)
|
82
|
+
ret[enum_name] = i18n_string(klass, name, enum_name)
|
72
83
|
end
|
84
|
+
end
|
85
|
+
end
|
73
86
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
87
|
+
# define instance method which returns current i18n string of the instance
|
88
|
+
def enum_i18n_instance_method(name)
|
89
|
+
klass = self
|
90
|
+
method_name = "#{name}_i18n"
|
91
|
+
detect_enum_conflict!(name, method_name, false)
|
92
|
+
klass.send(:define_method, method_name) do
|
93
|
+
status = self.send(name)
|
94
|
+
status.nil? ? nil : klass.send(:i18n_string, klass, name, status)
|
81
95
|
end
|
82
96
|
end
|
83
97
|
|
@@ -85,6 +99,8 @@ module ActiveRecord
|
|
85
99
|
I18n.t("#{klass.i18n_scope}.enum.#{klass.model_name.i18n_key}.#{name}.#{enum_name}")
|
86
100
|
end
|
87
101
|
|
102
|
+
#--------------------------------------------------------------------------
|
103
|
+
|
88
104
|
def enum_param(definitions)
|
89
105
|
klass = self
|
90
106
|
klass.defined_params.merge!(definitions)
|
@@ -92,29 +108,59 @@ module ActiveRecord
|
|
92
108
|
definitions.each do |name, values|
|
93
109
|
param_names = values.values.map(&:keys).flatten.uniq
|
94
110
|
param_names.each do |param_name|
|
111
|
+
enum_param_class_method(name, values, param_name)
|
112
|
+
enum_param_instance_method(name, values, param_name)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
95
116
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
# define instance methods which returns the parameter's current value of the instance
|
108
|
-
method_name = "#{name}_#{param_name}"
|
109
|
-
detect_enum_conflict!(name, method_name, false)
|
110
|
-
define_method(method_name) do
|
111
|
-
status = self.send(name)
|
112
|
-
return if status.nil?
|
113
|
-
defined_params[name][status.to_sym][param_name]
|
114
|
-
end
|
117
|
+
# define class methods which returns the parameter's values of each status
|
118
|
+
def enum_param_class_method(name, values, param_name)
|
119
|
+
klass = self
|
120
|
+
method_name = "#{name}_#{param_name.to_s.pluralize}"
|
121
|
+
detect_enum_conflict!(name, method_name, true)
|
122
|
+
klass.singleton_class.send(:define_method, method_name) do |&block|
|
123
|
+
param_hash = ActiveSupport::HashWithIndifferentAccess.new
|
124
|
+
values.each_with_object(param_hash) do |(enum_name, params), ret|
|
125
|
+
next if block.present? && !block.call(params)
|
126
|
+
ret[enum_name] = params[param_name]
|
115
127
|
end
|
116
128
|
end
|
117
129
|
end
|
130
|
+
|
131
|
+
# define instance methods which returns the parameter's current value of the instance
|
132
|
+
def enum_param_instance_method(name, values, param_name)
|
133
|
+
method_name = "#{name}_#{param_name}"
|
134
|
+
detect_enum_conflict!(name, method_name, false)
|
135
|
+
define_method(method_name) do
|
136
|
+
status = self.send(name)
|
137
|
+
return if status.nil?
|
138
|
+
defined_params[name][status.to_sym][param_name]
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
#--------------------------------------------------------------------------
|
143
|
+
|
144
|
+
def enum_for_select(definitions)
|
145
|
+
definitions.each do |name, values|
|
146
|
+
param_names = values.values.map(&:keys).flatten.uniq
|
147
|
+
param_names.each do |param_name|
|
148
|
+
enum_for_select_class_method(name, values, param_name)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# define class methods which returns the array for select box
|
154
|
+
def enum_for_select_class_method(name, values, param_name)
|
155
|
+
klass = self
|
156
|
+
method_name = "#{name.to_s.pluralize}_for_select"
|
157
|
+
detect_enum_conflict!(name, method_name, true)
|
158
|
+
klass.singleton_class.send(:define_method, method_name) do |&block|
|
159
|
+
i18n_hash = klass.send("#{name.to_s.pluralize}_i18n", &block)
|
160
|
+
i18n_hash.to_a.map(&:reverse)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
118
164
|
end
|
119
165
|
end
|
120
166
|
|
data/lib/exnum/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: exnum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kta-M
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-08-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -95,7 +95,7 @@ dependencies:
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
98
|
+
name: simplecov
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - ">="
|
@@ -116,9 +116,9 @@ executables: []
|
|
116
116
|
extensions: []
|
117
117
|
extra_rdoc_files: []
|
118
118
|
files:
|
119
|
+
- ".circleci/config.yml"
|
119
120
|
- ".gitignore"
|
120
121
|
- ".rspec"
|
121
|
-
- ".travis.yml"
|
122
122
|
- CODE_OF_CONDUCT.md
|
123
123
|
- Gemfile
|
124
124
|
- LICENSE.txt
|
@@ -149,8 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
149
|
- !ruby/object:Gem::Version
|
150
150
|
version: '0'
|
151
151
|
requirements: []
|
152
|
-
|
153
|
-
rubygems_version: 2.5.1
|
152
|
+
rubygems_version: 3.0.1
|
154
153
|
signing_key:
|
155
154
|
specification_version: 4
|
156
155
|
summary: Enum extention for Rails
|