smart_enum 2.2.0 → 2.2.1
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/lib/smart_enum/version.rb +1 -1
- data/lib/smart_enum/yaml_store.rb +23 -4
- data/lib/smart_enum.rb +45 -5
- 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: 7b509495447ff0f5652c258b272ce0ee0f37613b2c3e245a320677268cb4cb39
|
4
|
+
data.tar.gz: 934e89507c312f351ae5ad1d4fed972e83477314b42a2e426f7c5c79b1de5f44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f55bf375b72f8fa189f3c9772300aafd571b458e96c40bc48647ab1c65f2ed7b4758c41082668c25ddcda3caf6812163a6bfa09da9128fa9f15dc2b28bb2c816
|
7
|
+
data.tar.gz: 289a41a759595796b31f08f224bdd844d68e085e55b261ad4b9033ac72852e1d39a1d47cfbf48988e090939560c65a4e70ab4a130ddb492bccd6914c82af835c
|
data/lib/smart_enum/version.rb
CHANGED
@@ -4,6 +4,12 @@ require 'yaml'
|
|
4
4
|
# Methods for registering values from YAML files
|
5
5
|
class SmartEnum
|
6
6
|
module YamlStore
|
7
|
+
# Loads values from a YAML file or files
|
8
|
+
#
|
9
|
+
# Looks for a file or directory named after the enum type in the data root.
|
10
|
+
# If a directory is found, values from all of the YAML files in that directory
|
11
|
+
# are loaded.
|
12
|
+
# Otherwise, values are loaded from the file named after the enum.
|
7
13
|
def register_values_from_file!
|
8
14
|
unless SmartEnum::YamlStore.data_root
|
9
15
|
raise "Must set SmartEnum::YamlStore.data_root before using `register_values_from_file!`"
|
@@ -12,10 +18,21 @@ class SmartEnum
|
|
12
18
|
raise "Cannot infer data file for anonymous class"
|
13
19
|
end
|
14
20
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
21
|
+
basename = SmartEnum::Utilities.tableize(self.name)
|
22
|
+
dirname = File.join(SmartEnum::YamlStore.data_root, basename)
|
23
|
+
inferred_file = File.join(SmartEnum::YamlStore.data_root, "#{basename}.yml")
|
24
|
+
files = if Dir.exists?(dirname)
|
25
|
+
if File.exists?(inferred_file)
|
26
|
+
raise AmbiguousSource, "#{self} values should be defined in inferred file or directory, not both"
|
27
|
+
end
|
28
|
+
Dir[File.join(dirname, "*.yml")]
|
29
|
+
else
|
30
|
+
[inferred_file]
|
31
|
+
end
|
32
|
+
files.each do |file_path|
|
33
|
+
values = YAML.load_file(file_path)
|
34
|
+
register_values(values, self, detect_sti_types: true)
|
35
|
+
end
|
19
36
|
end
|
20
37
|
|
21
38
|
def self.data_root
|
@@ -25,6 +42,8 @@ class SmartEnum
|
|
25
42
|
def self.data_root=(val)
|
26
43
|
@data_root = val
|
27
44
|
end
|
45
|
+
|
46
|
+
class AmbiguousSource < RuntimeError; end
|
28
47
|
end
|
29
48
|
|
30
49
|
# automatically enable YAML store when this file is loaded
|
data/lib/smart_enum.rb
CHANGED
@@ -70,7 +70,7 @@ class SmartEnum
|
|
70
70
|
else
|
71
71
|
# No instance registration has been attempted, need to call
|
72
72
|
# register_values or register_value and lock_enum! first.
|
73
|
-
raise
|
73
|
+
raise EnumUnlocked, self
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
@@ -137,16 +137,20 @@ class SmartEnum
|
|
137
137
|
enum_type
|
138
138
|
end
|
139
139
|
unless (_descends_from_cache[klass] ||= (klass <= self))
|
140
|
-
raise "Specified class
|
140
|
+
raise RegistrationError.new("Specified class must derive from #{self}", type: klass, attributes: attrs)
|
141
141
|
end
|
142
142
|
if klass.abstract_class
|
143
|
-
raise "#{klass} is marked as abstract and may not be registered"
|
143
|
+
raise RegistrationError, "#{klass} is marked as abstract and may not be registered"
|
144
144
|
end
|
145
145
|
|
146
146
|
instance = klass.new(attrs)
|
147
147
|
id = instance.id
|
148
|
-
|
149
|
-
|
148
|
+
unless id
|
149
|
+
raise MissingIDError.new(type: klass, attributes: attrs)
|
150
|
+
end
|
151
|
+
if _enum_storage.has_key?(id)
|
152
|
+
raise DuplicateIDError.new(type: klass, attributes: attrs, id: id, existing: _enum_storage[id])
|
153
|
+
end
|
150
154
|
instance.freeze_attributes
|
151
155
|
_enum_storage[id] = instance
|
152
156
|
if klass != self
|
@@ -155,8 +159,44 @@ class SmartEnum
|
|
155
159
|
end
|
156
160
|
|
157
161
|
class EnumLocked < StandardError
|
162
|
+
attr_reader :type
|
163
|
+
|
158
164
|
def initialize(klass)
|
165
|
+
@type = klass
|
159
166
|
super("#{klass} has been locked and can not be written to")
|
160
167
|
end
|
161
168
|
end
|
169
|
+
|
170
|
+
class EnumUnlocked < StandardError
|
171
|
+
attr_reader :type
|
172
|
+
|
173
|
+
def initialize(klass)
|
174
|
+
@type = klass
|
175
|
+
super("Cannot use unlocked enum #{klass}.")
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
class RegistrationError < StandardError
|
180
|
+
attr_reader :type, :attributes
|
181
|
+
|
182
|
+
def initialize(info=nil, type: nil, attributes:{})
|
183
|
+
@info = info
|
184
|
+
@type = type
|
185
|
+
@attributes = attributes
|
186
|
+
super("Failed to register enum #{@type} with attributes: #{@attributes}. #{@info}")
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
class MissingIDError < RegistrationError
|
191
|
+
end
|
192
|
+
|
193
|
+
class DuplicateIDError < RegistrationError
|
194
|
+
attr_reader :id, :existing
|
195
|
+
|
196
|
+
def initialize(type:,id:,attributes:,existing:)
|
197
|
+
@id = id
|
198
|
+
@existing = existing
|
199
|
+
super("The ID #{@id} has already been registered with #{existing}", type: type, attributes: attributes)
|
200
|
+
end
|
201
|
+
end
|
162
202
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carl Brasic
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-04-
|
12
|
+
date: 2019-04-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|