active_module 0.5.0 → 0.6.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/README.md +2 -2
- data/lib/active_module/base.rb +6 -2
- data/lib/active_module/enum/comparison.rb +28 -0
- data/lib/active_module/enum/module_refinement.rb +32 -0
- data/lib/active_module/enum/modules_index.rb +19 -0
- data/lib/active_module/enum.rb +6 -0
- data/lib/active_module/modules_index.rb +7 -1
- data/lib/active_module/register.rb +3 -0
- data/lib/active_module/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5af4343b0d3b40fd54476b33e0eeedce2f9f1f16160a0fb1e20cdc9b39eaf228
|
4
|
+
data.tar.gz: ff556b56b6543e5f06152e1215f7e49b96b0b51faac250e4e90f51f4d8adcffe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30e7dbf45a6e89390b1487595e7e6398d58179b6508d861c14143c8b09246b5935918d3036388aeb783940f6273635d372ad722072ac00b515cf801ba68268b9
|
7
|
+
data.tar.gz: 8704366414cf42e448093a91dd118f798e9138a0c975c4487cb9af002d7e24bc221b4443bf5a6c144b30e0e537e21f98c308c23f1e8c4ccdd398e6a443402e1a
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
|
2
2
|
# active_module
|
3
3
|
[](https://rubygems.org/gems/active_module)
|
4
|
-
[](https://opensource.org/licenses/MIT)
|
5
5
|
[](https://github.com/pedrorolo/active_module/blob/main/Rakefile)
|
6
6
|
[](https://github.com/pedrorolo/active_module/blob/main/spec/spec_helper.rb)
|
7
7
|
[](https://bestgems.org/gems/active_module)
|
@@ -70,7 +70,7 @@ end
|
|
70
70
|
Add to your gemfile - and if you are using rails - that's all you need:
|
71
71
|
|
72
72
|
```ruby
|
73
|
-
gem 'active_module', "~> 0.
|
73
|
+
gem 'active_module', "~> 0.6"
|
74
74
|
```
|
75
75
|
|
76
76
|
If you are not using rails, just issue this command after loading active record
|
data/lib/active_module/base.rb
CHANGED
@@ -6,7 +6,9 @@ module ActiveModule
|
|
6
6
|
|
7
7
|
def initialize(possible_modules_or_mapping = [],
|
8
8
|
possible_modules: [],
|
9
|
-
mapping: {}
|
9
|
+
mapping: {},
|
10
|
+
enum_compatibility: false)
|
11
|
+
@enum_compatibility = enum_compatibility
|
10
12
|
if possible_modules_or_mapping.is_a?(Array)
|
11
13
|
@possible_modules =
|
12
14
|
(possible_modules_or_mapping + possible_modules + mapping.keys).uniq
|
@@ -91,7 +93,9 @@ module ActiveModule
|
|
91
93
|
end
|
92
94
|
|
93
95
|
def modules_index
|
94
|
-
@modules_index ||=
|
96
|
+
@modules_index ||=
|
97
|
+
(@enum_compatibility ? Enum::ModulesIndex : ModulesIndex)
|
98
|
+
.new(@possible_modules)
|
95
99
|
end
|
96
100
|
|
97
101
|
def from_db
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModule
|
4
|
+
class Enum
|
5
|
+
module Comparison
|
6
|
+
refine ::Module do
|
7
|
+
using ActiveModule::Enum::ModuleRefinement
|
8
|
+
|
9
|
+
def =~(other)
|
10
|
+
case other
|
11
|
+
when ::Symbol
|
12
|
+
possible_symbol_names_set.include?(other)
|
13
|
+
when ::String
|
14
|
+
possible_symbol_names_set.include?(other.to_sym)
|
15
|
+
else
|
16
|
+
self == other
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
using self
|
22
|
+
|
23
|
+
def self.compare(module1, module2)
|
24
|
+
module1 =~ module2
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModule
|
4
|
+
class Enum
|
5
|
+
module ModuleRefinement
|
6
|
+
refine ::Module do
|
7
|
+
using ActiveModule::ModuleRefinement
|
8
|
+
|
9
|
+
def possible_names
|
10
|
+
overriden_possible_names
|
11
|
+
end
|
12
|
+
|
13
|
+
def enum_symbol
|
14
|
+
name.demodulize.underscore.to_sym
|
15
|
+
end
|
16
|
+
|
17
|
+
def possible_symbol_names_set
|
18
|
+
@possible_symbol_names_set ||=
|
19
|
+
Set.new(overriden_possible_names.map(&:to_sym))
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def overriden_possible_names
|
25
|
+
[enum_symbol.to_s] + possible_names
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
using self
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "module_refinement"
|
4
|
+
|
5
|
+
# Indexes modules by symbols of their qualified and unqualified names.
|
6
|
+
module ActiveModule
|
7
|
+
class Enum
|
8
|
+
class ModulesIndex < ActiveModule::ModulesIndex
|
9
|
+
using ActiveModule::Enum::ModuleRefinement
|
10
|
+
|
11
|
+
protected
|
12
|
+
|
13
|
+
# so that this is using the enum refinement
|
14
|
+
def possible_names(module_instance)
|
15
|
+
module_instance.possible_names
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -12,12 +12,18 @@ module ActiveModule
|
|
12
12
|
@modules = modules
|
13
13
|
end
|
14
14
|
|
15
|
+
protected
|
16
|
+
|
17
|
+
def possible_names(module_instance)
|
18
|
+
module_instance.possible_names
|
19
|
+
end
|
20
|
+
|
15
21
|
private
|
16
22
|
|
17
23
|
def index
|
18
24
|
@index ||=
|
19
25
|
@modules.flat_map do |module_instance|
|
20
|
-
module_instance.
|
26
|
+
possible_names(module_instance).map do |name|
|
21
27
|
[name.to_sym, module_instance]
|
22
28
|
end
|
23
29
|
end.to_h.freeze
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "active_model"
|
4
|
+
|
3
5
|
module ActiveModule
|
4
6
|
module Register
|
5
7
|
module_function
|
@@ -8,6 +10,7 @@ module ActiveModule
|
|
8
10
|
ActiveModel::Type.register(type_symbol, ActiveModule::Base)
|
9
11
|
return unless defined?(ActiveRecord::Type)
|
10
12
|
|
13
|
+
require "active_record"
|
11
14
|
ActiveRecord::Type.register(type_symbol, ActiveModule::Base)
|
12
15
|
end
|
13
16
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_module
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pedro Rolo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -73,6 +73,10 @@ files:
|
|
73
73
|
- lib/active_module.rb
|
74
74
|
- lib/active_module/base.rb
|
75
75
|
- lib/active_module/comparison.rb
|
76
|
+
- lib/active_module/enum.rb
|
77
|
+
- lib/active_module/enum/comparison.rb
|
78
|
+
- lib/active_module/enum/module_refinement.rb
|
79
|
+
- lib/active_module/enum/modules_index.rb
|
76
80
|
- lib/active_module/invalid_module_value.rb
|
77
81
|
- lib/active_module/module_refinement.rb
|
78
82
|
- lib/active_module/modules_index.rb
|