yaml_enumeration 0.2.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d42b540fe754657e4e982b3324efcc67aaf7726019311dc574ec0035ee7b76ea
4
- data.tar.gz: c046de0c21e60c0f0bed6be8f276b60988182ecb793893ce4433d8cfbccfa1af
3
+ metadata.gz: 789056e38408fc39c5203d9b27347d67eac1223a005c4e97d6c3fbb0b50e52c9
4
+ data.tar.gz: f687bbcc331fa6959225a5701721b156ae652dc5c51540f4a85d2b8c75d66ab0
5
5
  SHA512:
6
- metadata.gz: d4b52ee6c9b97391eb0d3aea409595c2d3ef253a7b364222d47020da7fa4e017802029556b50d4e0d997ea08a14f8b2c6d6287d334f0a56d94f874c87fe37974
7
- data.tar.gz: ec4e04994a577354f8fd360601a5f4b07ba3153193a356dac69c8a89b162431451f75dd4addc7477be5cff26fe7cad968481f84ef1b374dd9649b8df744f3fb1
6
+ metadata.gz: 937de680ad7a668d42faa880eac779d53d0bfb9833bfdf981f50c97cf3703d113de49e550b3290ae27c9a7ca7173d44b0630cbf5922a07e60464b98886cb20e5
7
+ data.tar.gz: bc24da29146aac8453d17badffaf83176dc8c937d54eff3660ead43b8261237445741c1db95fff163bdf8f0fddeb80adc6e2f925573f3aa2d71604a4c05b3b8d
data/.travis.yml CHANGED
@@ -4,4 +4,6 @@ rvm:
4
4
  - 2.5.3
5
5
  - 2.6.5
6
6
  - 2.7.1
7
+ - 3.0.5
8
+ - 3.1.4
7
9
  before_install: gem install bundler
data/CHANGELOG.md CHANGED
@@ -1,14 +1,20 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.4.0
4
+ * add support for Ruby `3.1` [#7](https://github.com/alto/yaml_enumeration/pull/7)
5
+
6
+ ## 0.3.0
7
+ * enable exclusion of blocks of YAML from the enumeration (support YAML anchors) [#6](https://github.com/alto/yaml_enumeration/pull/6)
8
+
3
9
  ## 0.2.2
4
- * enhance find-by methods [#4])(https://github.com/alto/yaml_enumeration/pull/4)
5
- * add support for Ruby `2.6` and `2.7` [#3])(https://github.com/alto/yaml_enumeration/pull/3)
10
+ * enhance find-by methods [#4](https://github.com/alto/yaml_enumeration/pull/4)
11
+ * add support for Ruby `2.6` and `2.7` [#3](https://github.com/alto/yaml_enumeration/pull/3)
6
12
 
7
13
  ## 0.2.1
8
- * add support for Ruby `2.5.3` [#2])(https://github.com/alto/yaml_enumeration/pull/2)
14
+ * add support for Ruby `2.5.3` [#2](https://github.com/alto/yaml_enumeration/pull/2)
9
15
 
10
16
  ## 0.2.0
11
- * support _ActiveRecord_ methods `where`, `find_by` [#1])(https://github.com/alto/yaml_enumeration/pull/1), thanks to [@richdrich](https://github.com/richdrich)
17
+ * support _ActiveRecord_ methods `where`, `find_by` [#1](https://github.com/alto/yaml_enumeration/pull/1), thanks to [@richdrich](https://github.com/richdrich)
12
18
 
13
19
  ## 0.1.0
14
20
  * initial version
data/README.md CHANGED
@@ -80,6 +80,36 @@ class User < ActiveRecord::Base
80
80
  end
81
81
  ```
82
82
 
83
+ additional `yaml` syntax such as anchors can be removed from the enumeration by including `_exclude_from_enumeration`:
84
+
85
+ ```yaml
86
+ # countries.yml
87
+ ---
88
+ defaults_exclude_from_enumeration: &defaults
89
+ continent: Australia
90
+
91
+ nz:
92
+ <<: *defaults
93
+ id: 1 # has to be provided
94
+ type: new_zealand # has to be provided
95
+ name: New Zealand
96
+ code: nz
97
+ au:
98
+ <<: *defaults
99
+ id: 2
100
+ type: australia
101
+ name: Australia
102
+ code: au
103
+ uk:
104
+ <<: *defaults
105
+ id: 3
106
+ type: united_kingdom
107
+ name: United Kingdom
108
+ code: uk
109
+ continent: Europe
110
+ ...
111
+ ```
112
+
83
113
  ## Accessing members
84
114
 
85
115
  If you include a call to class method `with_named_items` you will get an item defined for each typed entry in the enumeration, e.g. `Country.NEW_ZEALAND` and `Country.AUSTRALIA`.
@@ -0,0 +1,14 @@
1
+ module YamlEnumeration
2
+ class Configuration
3
+
4
+ DEFAULT_PERMITTED_CLASSED = [].freeze
5
+
6
+ class << self
7
+ attr_writer :permitted_classes
8
+
9
+ def permitted_classes
10
+ @permitted_classes || DEFAULT_PERMITTED_CLASSED
11
+ end
12
+ end
13
+ end
14
+ end
@@ -13,7 +13,7 @@ module YamlEnumeration
13
13
 
14
14
  def self.load_values(filename)
15
15
  file = File.join(Rails.root, 'db', 'enumerations', "#{filename}.yml")
16
- YAML.load(ERB.new(File.read(file)).result).values.each do |data|
16
+ remove_exclusions(yaml_load(ERB.new(File.read(file)).result)).values.each do |data|
17
17
  value data.symbolize_keys
18
18
  end
19
19
  end
@@ -126,6 +126,14 @@ module YamlEnumeration
126
126
  end
127
127
  end
128
128
 
129
+ def yaml_load(source)
130
+ begin
131
+ YAML.load(source, aliases: true, permitted_classes: Configuration.permitted_classes)
132
+ rescue ArgumentError
133
+ YAML.load(source, permitted_classes: Configuration.permiitted_classes)
134
+ end
135
+ end
136
+
129
137
  # Define singleton methods .BLAH for each type
130
138
  def with_named_items(column = :type)
131
139
  all.each do |item|
@@ -144,5 +152,10 @@ module YamlEnumeration
144
152
  self.id = id
145
153
  end
146
154
 
155
+ def self.remove_exclusions(result)
156
+ keys_to_remove = result.keys.select {|key| key =~ /.*_exclude_from_enumeration$/ }
157
+ result.except(*keys_to_remove)
158
+ end
159
+
147
160
  end # Enumeration
148
161
  end
@@ -1,3 +1,3 @@
1
1
  module YamlEnumeration
2
- VERSION = "0.2.2"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require "yaml_enumeration/version"
2
2
  require "yaml_enumeration/association"
3
3
  require "yaml_enumeration/enumeration"
4
+ require "yaml_enumeration/configuration"
4
5
  require "yaml_enumeration/railtie"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml_enumeration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thorsten Boettger
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-28 00:00:00.000000000 Z
11
+ date: 2023-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -115,6 +115,7 @@ files:
115
115
  - bin/setup
116
116
  - lib/yaml_enumeration.rb
117
117
  - lib/yaml_enumeration/association.rb
118
+ - lib/yaml_enumeration/configuration.rb
118
119
  - lib/yaml_enumeration/enumeration.rb
119
120
  - lib/yaml_enumeration/railtie.rb
120
121
  - lib/yaml_enumeration/version.rb
@@ -138,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
139
  - !ruby/object:Gem::Version
139
140
  version: '0'
140
141
  requirements: []
141
- rubygems_version: 3.2.17
142
+ rubygems_version: 3.3.26
142
143
  signing_key:
143
144
  specification_version: 4
144
145
  summary: Create ActiveRecord enumerations based on YAML files.