enumy 0.7.0 → 0.8.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/lib/enumy/enum.rb +7 -2
- data/lib/enumy/model.rb +47 -0
- data/lib/enumy/rails/enum.rb +47 -0
- data/lib/enumy/rails/generators/enumy/install/templates/app/enums/application_enum.rb +1 -1
- data/lib/enumy/rails/model.rb +1 -1
- data/lib/enumy/version.rb +1 -1
- data/lib/enumy.rb +2 -1
- metadata +3 -2
- data/lib/enumy/rails_enum.rb +0 -41
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c5a41d5b566db44a73694cda348ebdafd92ba493df67811989767da5ded37a32
|
|
4
|
+
data.tar.gz: 650fea0c0613b094a3323122fc6e95b7b0df42e6ee16a748b8f062ab2fb3f712
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0c0bafe3cd2eec7e47741617d0455b208d168559d3a972ca9b800efa1a8edbd1e4a7b451b95ee949d218411696a2deef5a31bcd24029e0e08d138a4faa8c8ce0
|
|
7
|
+
data.tar.gz: 98f2c6f8d26fcfb6f2067c97ae205807e8cab6835b5fb992fa3ec7f8e0d3f3e63b04eb8417b7ebe5b12be52cf143852007304d0b8ed9c77edff3ff8363284116
|
data/lib/enumy/enum.rb
CHANGED
|
@@ -15,7 +15,12 @@ module Enumy
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def inspect
|
|
18
|
-
|
|
18
|
+
attributes = instance_variables.map do |name|
|
|
19
|
+
value = instance_variable_get(name)
|
|
20
|
+
"#{name}: #{value.inspect}"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
"#<#{self.class.name} #{attributes.join(', ')}>"
|
|
19
24
|
end
|
|
20
25
|
|
|
21
26
|
class << self
|
|
@@ -35,7 +40,7 @@ module Enumy
|
|
|
35
40
|
end
|
|
36
41
|
|
|
37
42
|
def find(key)
|
|
38
|
-
find_by(key: key) || raise(Errors::EnumNotFoundError.new("Could not find a `#{self.name}` enum with a `key` of #{key}"))
|
|
43
|
+
find_by(key: key.to_sym) || raise(Errors::EnumNotFoundError.new("Could not find a `#{self.name}` enum with a `key` of #{key}"))
|
|
39
44
|
end
|
|
40
45
|
|
|
41
46
|
def find_by(**args)
|
data/lib/enumy/model.rb
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
require 'enumy/enum'
|
|
2
|
+
require 'enumy/errors'
|
|
3
|
+
|
|
4
|
+
module Enumy
|
|
5
|
+
module Model
|
|
6
|
+
|
|
7
|
+
def has_enum(name, enum_class:, **options)
|
|
8
|
+
assert_valid_enum_type!(enum_class)
|
|
9
|
+
define_enum_methods(name, enum_class, default: options[:default])
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def assert_valid_enum_value_type!(enum_class, enum)
|
|
13
|
+
return if enum.nil?
|
|
14
|
+
raise Errors::InvalidEnumClassError, "Expected instance of `#{enum_class}`, got instance of `#{enum.class}`" unless enum.is_a?(enum_class)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def define_enum_methods(name, enum_class, default: nil)
|
|
20
|
+
assert_valid_enum_value_type!(enum_class, default)
|
|
21
|
+
|
|
22
|
+
define_method(name) do
|
|
23
|
+
instance_variable_set("@#{name}", default) unless instance_variable_defined?("@#{name}")
|
|
24
|
+
instance_variable_get("@#{name}")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
define_method("#{name}=") do |value|
|
|
28
|
+
case value
|
|
29
|
+
when Enumy::Enum, NilClass
|
|
30
|
+
self.class.assert_valid_enum_value_type!(enum_class, value)
|
|
31
|
+
instance_variable_set("@#{name}", value)
|
|
32
|
+
when String, Symbol
|
|
33
|
+
enum = enum_class.find(value)
|
|
34
|
+
instance_variable_set("@#{name}", enum)
|
|
35
|
+
else
|
|
36
|
+
raise ArgumentError
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def assert_valid_enum_type!(enum_class)
|
|
42
|
+
raise Errors::InvalidEnumClassError, "`#{enum_class}` is not a constant" unless enum_class.is_a?(Module)
|
|
43
|
+
raise Errors::InvalidEnumClassError, "`#{enum_class.name}` does not inherit from `#{Enumy::Enum.name}`" unless enum_class.ancestors.include?(Enumy::Enum)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
require 'enumy/rails/initializer_interceptor'
|
|
2
|
+
|
|
3
|
+
module Enumy
|
|
4
|
+
module Rails
|
|
5
|
+
class Enum < Enumy::Enum
|
|
6
|
+
include Enumy::Rails::InitializerInterceptor
|
|
7
|
+
include ::ActiveModel::Model
|
|
8
|
+
include ::ActiveModel::Attributes
|
|
9
|
+
|
|
10
|
+
attribute :key
|
|
11
|
+
|
|
12
|
+
def initialize(key, **attrs)
|
|
13
|
+
raise Errors::KeyMissingError.new(self) if key.nil?
|
|
14
|
+
super(key: key.to_sym, **attrs)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def titleize
|
|
18
|
+
key.to_s.titleize
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def human_attribute_name(attribute)
|
|
22
|
+
I18n.t("#{self.class.i18n_scope}.attributes.models.#{self.class.model_name.i18n_key}/#{self.key}.#{attribute}")
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def name
|
|
26
|
+
human_attribute_name(:key)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def inspect
|
|
30
|
+
"#<#{self.class.name} #{attributes.map { |name, value| "#{name}: #{value.inspect}" }.join(', ')}>"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class << self
|
|
34
|
+
|
|
35
|
+
def model_name
|
|
36
|
+
@model_name ||= ::ActiveModel::Name.new(self, nil, self.name.to_s)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def i18n_scope
|
|
40
|
+
:enum
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
data/lib/enumy/rails/model.rb
CHANGED
|
@@ -26,7 +26,7 @@ module Enumy
|
|
|
26
26
|
if enum_class.nil?
|
|
27
27
|
raise Errors::InvalidEnumClassError, "Could not resolve class `#{class_name.to_s.classify}`"
|
|
28
28
|
elsif !enum_class.ancestors.include?(Enumy::Enum)
|
|
29
|
-
raise Errors::InvalidEnumClassError, "`#{enum_class.name}` does not inherit from `#{Enumy::
|
|
29
|
+
raise Errors::InvalidEnumClassError, "`#{enum_class.name}` does not inherit from `#{Enumy::Enum.name}`"
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
end
|
data/lib/enumy/version.rb
CHANGED
data/lib/enumy.rb
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
require 'enumy/version'
|
|
2
2
|
require 'enumy/enum'
|
|
3
|
+
require 'enumy/model'
|
|
3
4
|
|
|
4
5
|
if defined?(Rails)
|
|
5
|
-
require 'enumy/
|
|
6
|
+
require 'enumy/rails/enum'
|
|
6
7
|
require 'enumy/rails/model'
|
|
7
8
|
require 'enumy/rails/generators/enumy/install/install_generator'
|
|
8
9
|
require 'enumy/rails/active_model/type/enum'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: enumy
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.8.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bevan Holborn
|
|
@@ -59,14 +59,15 @@ files:
|
|
|
59
59
|
- lib/enumy.rb
|
|
60
60
|
- lib/enumy/enum.rb
|
|
61
61
|
- lib/enumy/errors.rb
|
|
62
|
+
- lib/enumy/model.rb
|
|
62
63
|
- lib/enumy/rails/active_job/serializers/enum_serializer.rb
|
|
63
64
|
- lib/enumy/rails/active_model/type/enum.rb
|
|
65
|
+
- lib/enumy/rails/enum.rb
|
|
64
66
|
- lib/enumy/rails/generators/enumy/install/install_generator.rb
|
|
65
67
|
- lib/enumy/rails/generators/enumy/install/templates/app/enums/application_enum.rb
|
|
66
68
|
- lib/enumy/rails/initializer_interceptor.rb
|
|
67
69
|
- lib/enumy/rails/model.rb
|
|
68
70
|
- lib/enumy/rails/railtie.rb
|
|
69
|
-
- lib/enumy/rails_enum.rb
|
|
70
71
|
- lib/enumy/version.rb
|
|
71
72
|
homepage: https://rubygems.org/gems/enumy
|
|
72
73
|
licenses:
|
data/lib/enumy/rails_enum.rb
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
require 'enumy/rails/initializer_interceptor'
|
|
2
|
-
|
|
3
|
-
module Enumy
|
|
4
|
-
class RailsEnum < Enumy::Enum
|
|
5
|
-
include Enumy::Rails::InitializerInterceptor
|
|
6
|
-
include ::ActiveModel::Model
|
|
7
|
-
include ::ActiveModel::Attributes
|
|
8
|
-
|
|
9
|
-
attribute :key
|
|
10
|
-
|
|
11
|
-
def initialize(key, **attrs)
|
|
12
|
-
raise Errors::KeyMissingError.new(self) if key.nil?
|
|
13
|
-
super(key: key.to_sym, **attrs)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def titleize
|
|
17
|
-
key.to_s.titleize
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def human_attribute_name(attribute)
|
|
21
|
-
I18n.t("#{self.class.i18n_scope}.attributes.models.#{self.class.model_name.i18n_key}/#{self.key}.#{attribute}")
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def name
|
|
25
|
-
human_attribute_name(:key)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
class << self
|
|
29
|
-
|
|
30
|
-
def model_name
|
|
31
|
-
@model_name ||= ::ActiveModel::Name.new(self, nil, self.name.to_s)
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def i18n_scope
|
|
35
|
-
:enum
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
end
|
|
41
|
-
end
|