enumy 0.1.2 → 0.1.4
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/enumy/base.rb +12 -9
- data/lib/enumy/model.rb +31 -6
- data/lib/enumy/version.rb +1 -1
- data/lib/generators/enumy/install/templates/app/enums/application_enum.rb +0 -7
- 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: 1a689cb6b3ba13db003f6153785b947d8b383a85f4fb87f74ed82040f719ea27
|
4
|
+
data.tar.gz: 4b92c048f83b60fe675f90ca1821f01d66ca7b776685da87b236c8038104243f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1ad2c5b6c85515fdb9b89732a35c395349dff98dd64bd841b03cf9cf27416a33337f5972a4ecbff46674a6a9806ae748307eb4e4313aa6312de1e328c593b14
|
7
|
+
data.tar.gz: 60612de7768fa499577575acc8e60b1f0d23351a66eb9f9618cb006938e0820ce7247a4e742bdd9aca36adb3c197260446efdc7633dc2b2771720e46babe1585
|
data/lib/enumy/base.rb
CHANGED
@@ -3,13 +3,16 @@ require_relative 'errors'
|
|
3
3
|
module Enumy
|
4
4
|
class Base
|
5
5
|
include ActiveModel::Model
|
6
|
+
include ActiveModel::Attributes
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
attribute :id, :integer
|
9
|
+
attribute :name
|
9
10
|
|
10
|
-
def initialize(id
|
11
|
-
|
12
|
-
|
11
|
+
def initialize(id, name, **attrs)
|
12
|
+
raise ArgumentError, "#{self.class.name.upcase} enum requires an `id`" if id.nil?
|
13
|
+
raise ArgumentError, "#{self.class.name.upcase} enum requires a `name`" if name.nil?
|
14
|
+
|
15
|
+
super(id: id, name: name, **attrs)
|
13
16
|
end
|
14
17
|
|
15
18
|
def human_attribute_name(attribute)
|
@@ -20,7 +23,7 @@ module Enumy
|
|
20
23
|
|
21
24
|
def register(enum)
|
22
25
|
if instances[enum.id]
|
23
|
-
|
26
|
+
raise ArgumentError, "Enum `#{enum.name.to_s.upcase}` is using a duplicate id `#{enum.id}` for Enum '#{enum.class.name}'."
|
24
27
|
else
|
25
28
|
instances[enum.id] = enum
|
26
29
|
end
|
@@ -31,7 +34,7 @@ module Enumy
|
|
31
34
|
end
|
32
35
|
|
33
36
|
def find(id)
|
34
|
-
find_by_id(id) || raise(Errors::EnumNotFoundError.new)
|
37
|
+
find_by_id(id) || raise(Errors::EnumNotFoundError.new("Could not find a `#{name}` enum with an `id` of #{id}"))
|
35
38
|
end
|
36
39
|
|
37
40
|
def find_by_id(id)
|
@@ -45,7 +48,7 @@ module Enumy
|
|
45
48
|
end
|
46
49
|
|
47
50
|
def model_name
|
48
|
-
@model_name ||= ActiveModel::Name.new(self
|
51
|
+
@model_name ||= ActiveModel::Name.new(self, nil, name.to_s)
|
49
52
|
end
|
50
53
|
|
51
54
|
def i18n_scope
|
@@ -61,4 +64,4 @@ module Enumy
|
|
61
64
|
end
|
62
65
|
|
63
66
|
end
|
64
|
-
end
|
67
|
+
end
|
data/lib/enumy/model.rb
CHANGED
@@ -5,21 +5,46 @@ module Enumy
|
|
5
5
|
module Model
|
6
6
|
def belongs_to_enum(name, class_name: name, default: nil)
|
7
7
|
enum_class = class_name.to_s.classify.safe_constantize
|
8
|
-
|
8
|
+
|
9
|
+
if enum_class.nil?
|
10
|
+
raise Errors::InvalidEnumClassError, "Could not resolve class `#{class_name.to_s.classify}`"
|
11
|
+
elsif !enum_class.ancestors.include?(Enumy::Base)
|
12
|
+
raise Errors::InvalidEnumClassError, "`#{enum_class.name}` does not inherit from `Enumy::Base`"
|
13
|
+
end
|
9
14
|
|
10
15
|
define_method "#{name}=" do |enum|
|
11
|
-
|
12
|
-
|
16
|
+
raise NameError, "Enum `#{enum_class.name}` expected #{self.class.name} to have attribute `#{name}_id`." unless self.has_attribute?("#{name}_id")
|
17
|
+
|
18
|
+
if enum
|
19
|
+
raise Errors::InvalidEnumClassError, "Expected an enum of type `#{enum_class.name}`, got `#{enum.class.name}`" if enum_class != enum.class
|
20
|
+
self.send("#{name}_id=", enum.id)
|
21
|
+
else
|
22
|
+
self.send("#{name}_id=", nil)
|
23
|
+
end
|
24
|
+
|
25
|
+
instance_variable_set("@#{name}", enum)
|
13
26
|
end
|
14
27
|
|
15
28
|
define_method "#{name}" do
|
16
29
|
enum = instance_variable_get("@#{name}")
|
17
30
|
return enum if enum
|
18
|
-
|
31
|
+
|
32
|
+
raise NameError, "Enum `#{enum_class.name}` expected #{self.class.name} to have attribute `#{name}_id`." unless self.has_attribute?("#{name}_id")
|
33
|
+
return nil unless self.send("#{name}_id")
|
19
34
|
instance_variable_set("@#{name}", enum_class.find(self.send("#{name}_id")))
|
20
35
|
end
|
21
36
|
|
22
|
-
|
37
|
+
define_method "#{name}_id=" do |new_id|
|
38
|
+
super(new_id)
|
39
|
+
|
40
|
+
if new_id
|
41
|
+
instance_variable_set("@#{name}", enum_class.find(new_id))
|
42
|
+
else
|
43
|
+
instance_variable_set("@#{name}", nil)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
after_initialize -> { self.send("#{name}=", default) if self.has_attribute?("#{name}_id") && self.send("#{name}").nil? && default.present? }
|
23
48
|
end
|
24
49
|
end
|
25
|
-
end
|
50
|
+
end
|
data/lib/enumy/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bevan Holborn
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-01-
|
10
|
+
date: 2025-01-29 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: rake
|