enum_from_file 0.1.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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +51 -0
- data/Rakefile +27 -0
- data/lib/enum_from_file.rb +12 -0
- data/lib/enum_from_file/core_ext.rb +25 -0
- data/lib/enum_from_file/railtie.rb +6 -0
- data/lib/enum_from_file/storage.rb +35 -0
- data/lib/enum_from_file/version.rb +5 -0
- data/lib/tasks/enum_from_file_tasks.rake +6 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ea76f67ea1318023b84e54b22e344e6efa214524bde46bc929dd8e02b5003783
|
4
|
+
data.tar.gz: b9640350d7207500b1634c0c172e5bf724b415298cf7dfe6744e012acf7ff3f3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 60b23f83a28e3d9acc095c49e8d55d9be4b4cecbf347f92e768e5dde36affbf6be70ea8319596a03eb5a56fb33ee77688e713aebc5968a59f9be745bdb321ff5
|
7
|
+
data.tar.gz: f3384394473e2647b373311eb695a2ebcb80cc4c5c7ad9c078674a4400cfa389f52534f005ec834ac15590c64d4d4a4ab1766ab68871deff9de4032683310331
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2019 andrew_r
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# EnumFromFile
|
2
|
+
|
3
|
+
[](https://semaphoreci.com/andrewr224/enum-from-file)
|
4
|
+
|
5
|
+
If you've ever found yourself in a situation where you feel like you should use enum, but it has 50 potions so you don't know where to put it, the answer is here!
|
6
|
+
|
7
|
+
Just store your enums in a .yml file!
|
8
|
+
|
9
|
+
This makes little sense when you have a small enum with just a couple of options, but the more options you have, the easier it is to see the benefits of using it.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
1. Create a folder `/config/enums` to store your enum files in .yml format.
|
13
|
+
2. Add `include EnumFromFile` to the `ApplicationRecord` or your target model.
|
14
|
+
3. Make sure your enum column has a type `string`.
|
15
|
+
|
16
|
+
Et voila!
|
17
|
+
|
18
|
+
Given a file `/config/enums/eye_colors.yml` you add eye_color enum to a Cat model with the following line:
|
19
|
+
```ruby
|
20
|
+
enum :eye_color, from_file: :eye_colors
|
21
|
+
```
|
22
|
+
|
23
|
+
You can continue using regular syntax for regular cases:
|
24
|
+
```ruby
|
25
|
+
enum status: { active: active, archived: archived }
|
26
|
+
```
|
27
|
+
|
28
|
+
All the options will be passed to native enum implementation as expected.
|
29
|
+
|
30
|
+
## Installation
|
31
|
+
Add this line to your application's Gemfile:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
gem 'enum_from_file'
|
35
|
+
```
|
36
|
+
|
37
|
+
And then execute:
|
38
|
+
```bash
|
39
|
+
$ bundle
|
40
|
+
```
|
41
|
+
|
42
|
+
Or install it yourself as:
|
43
|
+
```bash
|
44
|
+
$ gem install enum_from_file
|
45
|
+
```
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
Bug reports and pull requests are welcome on project's [page](https://github.com/andrewr224/enum-from-file) at GitHub
|
49
|
+
|
50
|
+
## License
|
51
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'EnumFromFile'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'bundler/gem_tasks'
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
|
21
|
+
Rake::TestTask.new(:test) do |t|
|
22
|
+
t.libs << 'test'
|
23
|
+
t.pattern = 'test/**/*_test.rb'
|
24
|
+
t.verbose = false
|
25
|
+
end
|
26
|
+
|
27
|
+
task default: :test
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "enum_from_file/storage"
|
4
|
+
|
5
|
+
module EnumFromFile
|
6
|
+
module CoreExt
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
class_methods do
|
10
|
+
def enum(enum_name = {}, **options)
|
11
|
+
if options.key?(:from_file)
|
12
|
+
options[enum_name] = enum_from_file(file: options.delete(:from_file))
|
13
|
+
end
|
14
|
+
|
15
|
+
super(options)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def enum_from_file(file:)
|
21
|
+
EnumFromFile::Storage.instance.send(file)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EnumFromFile
|
4
|
+
class Storage
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
enum_files.each(&method(:define_enum))
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def enum_files
|
14
|
+
Dir["#{project_root}/config/enums/*.yml"].map { |path| File.open(path) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def project_root
|
18
|
+
return Rails.root if defined?(Rails)
|
19
|
+
return Bundler.root if defined?(Bundler)
|
20
|
+
|
21
|
+
Dir.pwd
|
22
|
+
end
|
23
|
+
|
24
|
+
def define_enum(file)
|
25
|
+
define_singleton_method File.basename(file.path, ".yml") do
|
26
|
+
instance_variable_get(instance_name(file)) ||
|
27
|
+
instance_variable_set(instance_name(file), YAML.safe_load(file))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def instance_name(file)
|
32
|
+
"@#{File.basename(file.path, '.yml').to_sym}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enum_from_file
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- andrew_r
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry-rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec-rails
|
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: rubocop
|
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: rubocop-rspec
|
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: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.3.6
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.3.6
|
97
|
+
description: Add `from_file` option to Rails enum.
|
98
|
+
email:
|
99
|
+
- andrew.r224@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- MIT-LICENSE
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- lib/enum_from_file.rb
|
108
|
+
- lib/enum_from_file/core_ext.rb
|
109
|
+
- lib/enum_from_file/railtie.rb
|
110
|
+
- lib/enum_from_file/storage.rb
|
111
|
+
- lib/enum_from_file/version.rb
|
112
|
+
- lib/tasks/enum_from_file_tasks.rake
|
113
|
+
homepage: https://github.com/andrewr224/enum-from-file
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata: {}
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 2.7.3
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: Load Rails enums from files!
|
137
|
+
test_files: []
|