active_module 0.4.0 → 0.6.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: cc1cc99fafb5c1eb50bf09cc6ff985637a752804288cf5c249bfcf17a7a73935
4
- data.tar.gz: b630d84b3330c936fd6507dd44ece2018300e8a3bfad51c0838518d9830fc742
3
+ metadata.gz: 5af4343b0d3b40fd54476b33e0eeedce2f9f1f16160a0fb1e20cdc9b39eaf228
4
+ data.tar.gz: ff556b56b6543e5f06152e1215f7e49b96b0b51faac250e4e90f51f4d8adcffe
5
5
  SHA512:
6
- metadata.gz: 61a28ee14fe72430c7df4c00c8ea4bab825fd2765690f09dafcf5d9c4ec9bb00ff11521d8e016b41633b7ed1685ebadd751681a9615db677f6af9d9813eafe49
7
- data.tar.gz: 8ceb742165332160adea6bd32818300a719066469ce9bcaa09a799798854577874105e203cb63cded2cfebbddba8a43961568172f13011f6619622e04943e2ae
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
  [![Gem Version](https://img.shields.io/gem/v/active_module)](https://rubygems.org/gems/active_module)
4
- [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://opensource.org/licenses/MIT)
4
+ [![License: MIT](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT)
5
5
  [![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/pedrorolo/active_module/main.yml)](https://github.com/pedrorolo/active_module/blob/main/Rakefile)
6
6
  [![100% Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)](https://github.com/pedrorolo/active_module/blob/main/spec/spec_helper.rb)
7
7
  [![Gem Total Downloads](https://img.shields.io/gem/dt/active_module?style=flat)](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.4"
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
@@ -1,15 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_model"
4
- require_relative "modules_index"
5
-
6
3
  module ActiveModule
7
4
  class Base < ActiveModel::Type::Value
8
5
  attr_reader :possible_modules, :mapping
9
6
 
10
7
  def initialize(possible_modules_or_mapping = [],
11
8
  possible_modules: [],
12
- mapping: {})
9
+ mapping: {},
10
+ enum_compatibility: false)
11
+ @enum_compatibility = enum_compatibility
13
12
  if possible_modules_or_mapping.is_a?(Array)
14
13
  @possible_modules =
15
14
  (possible_modules_or_mapping + possible_modules + mapping.keys).uniq
@@ -94,7 +93,9 @@ module ActiveModule
94
93
  end
95
94
 
96
95
  def modules_index
97
- @modules_index ||= ModulesIndex.new(@possible_modules)
96
+ @modules_index ||=
97
+ (@enum_compatibility ? Enum::ModulesIndex : ModulesIndex)
98
+ .new(@possible_modules)
98
99
  end
99
100
 
100
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
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveModule
4
+ class Enum
5
+ end
6
+ end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "module_refinement"
4
-
5
3
  # Indexes modules by symbols of their qualified and unqualified names.
6
4
  module ActiveModule
7
5
  class ModulesIndex
@@ -14,12 +12,18 @@ module ActiveModule
14
12
  @modules = modules
15
13
  end
16
14
 
15
+ protected
16
+
17
+ def possible_names(module_instance)
18
+ module_instance.possible_names
19
+ end
20
+
17
21
  private
18
22
 
19
23
  def index
20
24
  @index ||=
21
25
  @modules.flat_map do |module_instance|
22
- module_instance.possible_names.map do |name|
26
+ possible_names(module_instance).map do |name|
23
27
  [name.to_sym, module_instance]
24
28
  end
25
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveModule
4
- VERSION = "0.4.0"
4
+ VERSION = "0.6.0"
5
5
  end
data/lib/active_module.rb CHANGED
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "active_module/version"
4
- require_relative "active_module/base"
5
- require_relative "active_module/invalid_module_value"
6
- require_relative "active_module/register"
7
- require_relative "active_module/comparison"
3
+ require "zeitwerk"
4
+ loader = Zeitwerk::Loader.for_gem
5
+ loader.setup
8
6
  require "active_module/railtie" if defined?(Rails::Railtie)
9
7
 
10
8
  module ActiveModule
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.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: 2024-12-27 00:00:00.000000000 Z
11
+ date: 2025-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -30,6 +30,20 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: '7.1'
33
+ - !ruby/object:Gem::Dependency
34
+ name: zeitwerk
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.7'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.7'
33
47
  - !ruby/object:Gem::Dependency
34
48
  name: bundler
35
49
  requirement: !ruby/object:Gem::Requirement
@@ -59,6 +73,10 @@ files:
59
73
  - lib/active_module.rb
60
74
  - lib/active_module/base.rb
61
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
62
80
  - lib/active_module/invalid_module_value.rb
63
81
  - lib/active_module/module_refinement.rb
64
82
  - lib/active_module/modules_index.rb