enum_ish 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 72e9f45d9096f7b3cb71b41f0eec2db69993d4b6
4
+ data.tar.gz: b642520f713d95c7730d9096af0b69a11a9007ac
5
+ SHA512:
6
+ metadata.gz: df6c0a64caa6dfc6551c82f759dbb3697024d58d77c10ad9b552e9cf766680151db386b4fd733033e9dbda4205e7dbe5ae2e71c66ff5d17fdd382968a887b48a
7
+ data.tar.gz: f7ef88a3ebd6b5f13be8fb9d6ee7505d69eded7a4358279bf07196cb76238c258fd9afe7f9f99fb399f065793e50970e2266dbb8005fbd4511c5ad74cc1daa7b
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ .bundle/
2
+ .yardoc
3
+ .project
4
+ Gemfile.lock
5
+ _yardoc/
6
+ coverage/
7
+ doc/
8
+ pkg/
9
+ spec/reports/
10
+ spec/dummy/db/*.sqlite3
11
+ spec/dummy/log/*.log
12
+ spec/dummy/tmp/*
13
+ tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3
4
+ - 2.4
5
+ - 2.5
6
+ gemfile:
7
+ - gemfiles/rails50.gemfile
8
+ - gemfiles/rails51.gemfile
9
+ - gemfiles/rails52.gemfile
10
+ before_script:
11
+ - cd spec/dummy
12
+ - bundle exec rake db:migrate RAILS_ENV=test
13
+ - cd ../..
14
+ script: bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Yoshikazu Kaneta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,159 @@
1
+ # EnumIsh
2
+
3
+ A ruby and rails extension to generate enum-like methods.
4
+
5
+ ## Dependencies
6
+
7
+ * ruby 2.3+
8
+ * activesupport 5.0+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'enum_ish'
16
+ ```
17
+
18
+ Then execute:
19
+
20
+ $ bundle
21
+
22
+ ## Usage
23
+
24
+ Extend your class using EnumIsh and define an enum-like field:
25
+
26
+ ```ruby
27
+ class User
28
+ extend EnumIsh
29
+ attr_accessor :status # status is a string field
30
+ enum_ish :status, ['enable', 'disable'] # status has 'enable' or 'disable'
31
+ end
32
+ ```
33
+
34
+ Then define i18n translations (example below shows japanese translations):
35
+
36
+ ```yaml
37
+ ja:
38
+ enum_ish:
39
+ user:
40
+ enable: 有効
41
+ disable: 無効
42
+ ```
43
+
44
+ Enum-like methods are generated as follows:
45
+
46
+ ```ruby
47
+ user = User.new
48
+ user.status = 'enable'
49
+ user.status_text #=> "有効"
50
+ User.status_options #=> [["有効", "enable"], ["無効", "disable"]]
51
+ ```
52
+
53
+ ### Additional translations
54
+
55
+ Define additional translations:
56
+
57
+ ```yaml
58
+ ja:
59
+ enum_ish:
60
+ user:
61
+ status:
62
+ enable: 有効
63
+ disable: 無効
64
+ status/short:
65
+ enable: 有
66
+ disable: 無
67
+ ```
68
+
69
+ ```ruby
70
+ user = User.new
71
+ user.status = 'enable'
72
+ user.status_text(format: :short) #=> "有"
73
+ User.status_options(format: :short) #=> [["有", "enable"], ["無", "disable"]]
74
+ ```
75
+
76
+ ### Default value
77
+
78
+ Set default value:
79
+
80
+ ```ruby
81
+ class User
82
+ extend EnumIsh
83
+ attr_accessor :status
84
+ enum_ish :status, ['enable', 'disable'], default: 'enable'
85
+ end
86
+
87
+ user = User.new
88
+ user.status #=> "enable"
89
+ ```
90
+
91
+ ### Predicates
92
+
93
+ Generate predicate methods:
94
+
95
+ ```ruby
96
+ class User
97
+ extend EnumIsh
98
+ attr_accessor :status
99
+ enum_ish :status, ['enable', 'disable'], predicate: true
100
+ end
101
+
102
+ user = User.new
103
+ user.status = 'enable'
104
+ user.status_enable? #=> true
105
+ user.status_disable? #=> false
106
+ ```
107
+
108
+ ### Accessor
109
+
110
+ Generate getter and setter for aliased symbols instead of raw values:
111
+
112
+ ```ruby
113
+ class User
114
+ extend EnumIsh
115
+ attr_accessor :status
116
+ enum_ish :status, { _enable: 'enable', _disable: 'disable' }, accessor: true
117
+ end
118
+
119
+ user = User.new
120
+ user.status = :_enable
121
+ user.status #=> :_enable
122
+ user.status_raw #=> "enable"
123
+ ```
124
+
125
+ ### Scope (for ActiveRecord)
126
+
127
+ Generate scope:
128
+
129
+ ```ruby
130
+ class User < ActiveRecord::Base
131
+ extend EnumIsh
132
+ enum_ish :status, ['enable', 'disable'], scope: true
133
+ end
134
+
135
+ User.with_status(:enable) #=> SELECT "users".* FROM "users" WHERE "users"."status" = "enable"
136
+ ```
137
+
138
+ ### Validation (for ActiveRecord)
139
+
140
+ Generate validation:
141
+
142
+ ```ruby
143
+ class User < ActiveRecord::Base
144
+ extend EnumIsh
145
+ enum_ish :status, ['enable', 'disable'], validate: true
146
+ end
147
+
148
+ user = User.new
149
+ user.status = 'INVALID'
150
+ user.valid? #=> false
151
+ ```
152
+
153
+ ## Contributing
154
+
155
+ Bug reports and pull requests are welcome at https://github.com/kanety/enum_ish.
156
+
157
+ ## License
158
+
159
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/enum_ish.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'enum_ish/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "enum_ish"
8
+ spec.version = EnumIsh::VERSION
9
+ spec.authors = ["Yoshikazu Kaneta"]
10
+ spec.email = ["kaneta@sitebridge.co.jp"]
11
+ spec.summary = %q{A ruby and rails extension to generate enum-like methods}
12
+ spec.description = %q{A ruby and rails extension to generate enum-like methods}
13
+ spec.homepage = "https://github.com/kanety/enum_ish"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "activesupport", ">= 5.0"
21
+
22
+ spec.add_development_dependency "rails", ">= 5.0"
23
+ spec.add_development_dependency "sqlite3"
24
+ spec.add_development_dependency "rspec-rails"
25
+ spec.add_development_dependency "simplecov"
26
+ spec.add_development_dependency "pry-rails"
27
+ spec.add_development_dependency "pry-byebug"
28
+ end
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 5.0.0"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 5.1.0"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 5.2.0.rc1"
4
+
5
+ gemspec path: "../"
data/lib/enum_ish.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'active_support'
2
+ require 'enum_ish/version'
3
+ require 'enum_ish/builder'
4
+ require 'enum_ish/builder/active_record' if defined?(ActiveRecord::Base)
5
+
6
+ module EnumIsh
7
+ def enum_ish(attr, enum, config = {})
8
+ [:text, :options].each do |key|
9
+ config[key] = true unless config.key?(key)
10
+ end
11
+
12
+ if defined?(ActiveRecord::Base) && self.ancestors.include?(ActiveRecord::Base)
13
+ Builder::ActiveRecord.new(self).build(attr, enum, config)
14
+ else
15
+ Builder.new(self).build(attr, enum, config)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,119 @@
1
+ module EnumIsh
2
+ class Builder
3
+ def initialize(klass)
4
+ @klass = klass
5
+ end
6
+
7
+ def build(attr, enum, config = {})
8
+ if enum.is_a?(Array)
9
+ enum = enum.map { |v|
10
+ k = v.to_s.to_sym
11
+ v = v.to_s if v.is_a?(Symbol)
12
+ [k, v]
13
+ }.to_h
14
+ end
15
+
16
+ [:text, :options, :predicate, :accessor, :validate, :scope].each do |key|
17
+ if config[key]
18
+ send("define_#{key}", attr, enum, config)
19
+ end
20
+ end
21
+
22
+ if config.key?(:default)
23
+ define_default_value(attr, enum, config)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def define_text(attr, enum, config)
30
+ method = "#{attr}_text"
31
+
32
+ @klass.class_eval do
33
+ define_method method do |options = {}|
34
+ value = public_send(attr)
35
+ dic = EnumIsh::Dictionary.new(self.class).load(attr, enum, config, options)
36
+ dic[value] || value
37
+ end
38
+ end
39
+ end
40
+
41
+ def define_options(attr, enum, config)
42
+ method = "#{attr}_options"
43
+
44
+ @klass.class_eval do
45
+ define_singleton_method method do |options = {}|
46
+ dic = EnumIsh::Dictionary.new(self).load(attr, enum, config, options)
47
+ dic.invert.to_a
48
+ end
49
+ end
50
+ end
51
+
52
+ def define_predicate(attr, enum, config)
53
+ enum.each do |key, value|
54
+ method = "#{attr}_#{key}?".tr('.', '_')
55
+ @klass.class_eval do
56
+ define_method method do
57
+ public_send(attr) == value
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ def define_default_value(attr, enum, config)
64
+ mod = Module.new
65
+ mod.module_eval do
66
+ define_method :initialize do |*args|
67
+ public_send("#{attr}=", config[:default]) if respond_to?(attr) && public_send(attr).nil?
68
+ super(*args)
69
+ end
70
+ end
71
+ @klass.prepend mod
72
+ end
73
+
74
+ def define_accessor(attr, enum, config)
75
+ method = "#{attr}_raw"
76
+
77
+ @klass.class_eval do
78
+ define_method method do
79
+ instance_variable_get("@#{attr}")
80
+ end
81
+ define_method "#{attr}" do
82
+ enum.invert[instance_variable_get("@#{attr}")]
83
+ end
84
+ define_method "#{attr}=" do |value|
85
+ instance_variable_set("@#{attr}", enum[value])
86
+ end
87
+ end
88
+ end
89
+
90
+ def define_validate(attr, enum, config)
91
+ @klass.class_eval do
92
+ validates attr, inclusion: { in: enum.values }, allow_nil: true
93
+ end
94
+ end
95
+ end
96
+
97
+ class Dictionary
98
+ def initialize(klass)
99
+ @klass = klass
100
+ end
101
+
102
+ def load(attr, enum, config, options)
103
+ dict = load_i18n(attr, enum, options)
104
+
105
+ translated = enum.map { |k, v| dict[k] ? [k, dict[k]] : [k, v.to_s] }.to_h
106
+ translated = translated.map { |k, v| [enum[k], v] }.to_h unless config[:accessor]
107
+ translated
108
+ end
109
+
110
+ private
111
+
112
+ def load_i18n(attr, enum, options)
113
+ attr_key = [attr, options.delete(:format)].compact.join('/')
114
+ i18n_options = options.merge(default: [:"enum_ish.defaults.#{attr_key}", enum.invert])
115
+ dict = I18n.t("enum_ish.#{@klass.name.underscore}.#{attr_key}", i18n_options)
116
+ dict.map { |k, v| [k.to_s.to_sym, v.to_s] }.to_h
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,44 @@
1
+ module EnumIsh
2
+ class Builder
3
+ class ActiveRecord < Builder
4
+ private
5
+
6
+ def define_default_value(attr, enum, config)
7
+ method = "_enum_ish_init_#{attr}".to_sym
8
+
9
+ @klass.class_eval do
10
+ after_initialize method
11
+ define_method method do
12
+ public_send("#{attr}=", config[:default]) if respond_to?(attr) && public_send(attr).nil?
13
+ end
14
+ end
15
+ end
16
+
17
+ def define_accessor(attr, enum, config)
18
+ method = "#{attr}_raw"
19
+
20
+ @klass.class_eval do
21
+ define_method method do
22
+ read_attribute(attr)
23
+ end
24
+ define_method "#{attr}" do
25
+ enum.invert[read_attribute(attr)]
26
+ end
27
+ define_method "#{attr}=" do |value|
28
+ write_attribute(attr, enum[value])
29
+ end
30
+ end
31
+ end
32
+
33
+ def define_scope(attr, enum, config)
34
+ method = "with_#{attr}"
35
+
36
+ @klass.class_eval do
37
+ scope method, ->(value) {
38
+ where(attr => value)
39
+ }
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module EnumIsh
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enum_ish
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yoshikazu Kaneta
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-rails
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'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: A ruby and rails extension to generate enum-like methods
112
+ email:
113
+ - kaneta@sitebridge.co.jp
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE
123
+ - README.md
124
+ - Rakefile
125
+ - enum_ish.gemspec
126
+ - gemfiles/rails50.gemfile
127
+ - gemfiles/rails51.gemfile
128
+ - gemfiles/rails52.gemfile
129
+ - lib/enum_ish.rb
130
+ - lib/enum_ish/builder.rb
131
+ - lib/enum_ish/builder/active_record.rb
132
+ - lib/enum_ish/version.rb
133
+ homepage: https://github.com/kanety/enum_ish
134
+ licenses: []
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.5.2.2
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: A ruby and rails extension to generate enum-like methods
156
+ test_files: []