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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e0d0d43830b1a096657784831ff7e7c4c8488a740149a01095c14e3e8d0d130
4
- data.tar.gz: 7d6cbec972d2c7c8d82371f1c597eead62d87e9803d46a942c01e6bed6d753f4
3
+ metadata.gz: 5af4343b0d3b40fd54476b33e0eeedce2f9f1f16160a0fb1e20cdc9b39eaf228
4
+ data.tar.gz: ff556b56b6543e5f06152e1215f7e49b96b0b51faac250e4e90f51f4d8adcffe
5
5
  SHA512:
6
- metadata.gz: f71a7ade2dae5d5ef9fb608c0dfa0efda5f56216edea131dcb1808afed879d1c729c6648cb6892290f71474f0495e8cd1874ed19bfd1940c16d876e0ed919c67
7
- data.tar.gz: e5174fd6f3b26e9695393662f3e14e7a6531a16b899a9af1035a576419fed9f91c2797e68f17d437060859dfa1250ca8d7c8f63f78540603764cd7584c441ec0
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.5"
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
@@ -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 ||= ModulesIndex.new(@possible_modules)
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
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveModule
4
+ class Enum
5
+ end
6
+ 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.possible_names.map do |name|
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveModule
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  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.5.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-29 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
@@ -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