enum_from_file 0.1.0 → 0.2.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 +4 -4
- data/README.md +20 -5
- data/lib/enum_from_file/core_ext.rb +1 -1
- data/lib/enum_from_file/storage.rb +15 -1
- data/lib/enum_from_file/version.rb +1 -1
- data/lib/enum_from_file.rb +2 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0627a56758f812b87ea4fd6322f74b153b9c51f7d60e8a629a2eaddbafb8bfb
|
4
|
+
data.tar.gz: 55b2bdb4b59e98a03835a394c4cc21e2a70f1fdab67b9ba01f66ea775d45e922
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a1001bd834c85335d87be800c0ebd07c0489d71eb60f824279e3d7e791d2127706c41f641c790b3b334196dc459b4b94aa39cd9b3587af6849332d3d5ad30cd
|
7
|
+
data.tar.gz: a4a7024f4d05f2cfd6fa191549c8ae0ea5c34a5829b12732f9b4ce948225c10b0d7291047e5ea7ef3913d6532bed6fe8e87a48617c80c6d72bb49485d608dc6c
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[](https://semaphoreci.com/andrewr224/enum-from-file)
|
4
4
|
|
5
|
-
If you've ever found yourself in a situation where you feel like you should use enum, but it has 50
|
5
|
+
If you've ever found yourself in a situation where you feel like you should use enum, but it has 50 positions so you don't know where to put it, the answer is here!
|
6
6
|
|
7
7
|
Just store your enums in a .yml file!
|
8
8
|
|
@@ -10,12 +10,11 @@ This makes little sense when you have a small enum with just a couple of options
|
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
1. Create a folder `/config/enums` to store your enum files in .yml format.
|
13
|
-
2.
|
14
|
-
3. Make sure your enum column has a type `string`.
|
13
|
+
2. Make sure your enum column has a type `string`.
|
15
14
|
|
16
15
|
Et voila!
|
17
16
|
|
18
|
-
Given a file `/config/enums/eye_colors.yml` you add eye_color enum to a Cat model with the following line:
|
17
|
+
Given a file `/config/enums/eye_colors.yml` you can add eye_color enum to a Cat model with the following line:
|
19
18
|
```ruby
|
20
19
|
enum :eye_color, from_file: :eye_colors
|
21
20
|
```
|
@@ -25,7 +24,23 @@ You can continue using regular syntax for regular cases:
|
|
25
24
|
enum status: { active: active, archived: archived }
|
26
25
|
```
|
27
26
|
|
28
|
-
All the options will be passed to native enum implementation as expected
|
27
|
+
**All the options will be passed to native enum implementation as expected.**
|
28
|
+
|
29
|
+
|
30
|
+
You can structure your .yml file either as a hash
|
31
|
+
```
|
32
|
+
blue: blue
|
33
|
+
copper: copper
|
34
|
+
yellow: yellow
|
35
|
+
```
|
36
|
+
Or as a list
|
37
|
+
```
|
38
|
+
gold
|
39
|
+
orange
|
40
|
+
green
|
41
|
+
red
|
42
|
+
```
|
43
|
+
The list will be converted automatically, so both structures yield the same result.
|
29
44
|
|
30
45
|
## Installation
|
31
46
|
Add this line to your application's Gemfile:
|
@@ -8,6 +8,12 @@ module EnumFromFile
|
|
8
8
|
enum_files.each(&method(:define_enum))
|
9
9
|
end
|
10
10
|
|
11
|
+
def values_from(file:)
|
12
|
+
send(file)
|
13
|
+
rescue NoMethodError
|
14
|
+
raise StandardError, "Cannot read 'config/enums/#{method_name}.yml'"
|
15
|
+
end
|
16
|
+
|
11
17
|
private
|
12
18
|
|
13
19
|
def enum_files
|
@@ -24,12 +30,20 @@ module EnumFromFile
|
|
24
30
|
def define_enum(file)
|
25
31
|
define_singleton_method File.basename(file.path, ".yml") do
|
26
32
|
instance_variable_get(instance_name(file)) ||
|
27
|
-
instance_variable_set(instance_name(file),
|
33
|
+
instance_variable_set(instance_name(file), instance_value(file))
|
28
34
|
end
|
29
35
|
end
|
30
36
|
|
31
37
|
def instance_name(file)
|
32
38
|
"@#{File.basename(file.path, '.yml').to_sym}"
|
33
39
|
end
|
40
|
+
|
41
|
+
def instance_value(file)
|
42
|
+
values = YAML.safe_load(file)
|
43
|
+
|
44
|
+
return values.to_h unless values.is_a?(String)
|
45
|
+
|
46
|
+
Hash[values.split.map { |val| [val.to_sym, val.to_sym] }]
|
47
|
+
end
|
34
48
|
end
|
35
49
|
end
|
data/lib/enum_from_file.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enum_from_file
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- andrew_r
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-03-
|
11
|
+
date: 2019-03-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|