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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea76f67ea1318023b84e54b22e344e6efa214524bde46bc929dd8e02b5003783
4
- data.tar.gz: b9640350d7207500b1634c0c172e5bf724b415298cf7dfe6744e012acf7ff3f3
3
+ metadata.gz: d0627a56758f812b87ea4fd6322f74b153b9c51f7d60e8a629a2eaddbafb8bfb
4
+ data.tar.gz: 55b2bdb4b59e98a03835a394c4cc21e2a70f1fdab67b9ba01f66ea775d45e922
5
5
  SHA512:
6
- metadata.gz: 60b23f83a28e3d9acc095c49e8d55d9be4b4cecbf347f92e768e5dde36affbf6be70ea8319596a03eb5a56fb33ee77688e713aebc5968a59f9be745bdb321ff5
7
- data.tar.gz: f3384394473e2647b373311eb695a2ebcb80cc4c5c7ad9c078674a4400cfa389f52534f005ec834ac15590c64d4d4a4ab1766ab68871deff9de4032683310331
6
+ metadata.gz: 7a1001bd834c85335d87be800c0ebd07c0489d71eb60f824279e3d7e791d2127706c41f641c790b3b334196dc459b4b94aa39cd9b3587af6849332d3d5ad30cd
7
+ data.tar.gz: a4a7024f4d05f2cfd6fa191549c8ae0ea5c34a5829b12732f9b4ce948225c10b0d7291047e5ea7ef3913d6532bed6fe8e87a48617c80c6d72bb49485d608dc6c
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Build Status](https://semaphoreci.com/api/v1/andrewr224/enum-from-file/branches/master/badge.svg)](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 potions so you don't know where to put it, the answer is here!
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. Add `include EnumFromFile` to the `ApplicationRecord` or your target model.
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:
@@ -18,7 +18,7 @@ module EnumFromFile
18
18
  private
19
19
 
20
20
  def enum_from_file(file:)
21
- EnumFromFile::Storage.instance.send(file)
21
+ EnumFromFile::Storage.instance.values_from(file: file)
22
22
  end
23
23
  end
24
24
  end
@@ -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), YAML.safe_load(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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EnumFromFile
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -3,10 +3,6 @@
3
3
  require "enum_from_file/railtie"
4
4
  require "enum_from_file/core_ext"
5
5
 
6
- module EnumFromFile
7
- extend ActiveSupport::Concern
8
-
9
- included do
10
- include EnumFromFile::CoreExt
11
- end
6
+ class ActiveRecord::Base
7
+ include EnumFromFile::CoreExt
12
8
  end
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.1.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-12 00:00:00.000000000 Z
11
+ date: 2019-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails