active_module 0.1.8 → 0.2.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 +48 -5
- data/lib/active_module/base.rb +32 -6
- data/lib/active_module/version.rb +1 -1
- 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: 743570de05b5e510d10408fe3b95eaab2789e4432311538bda978bf52d893355
|
4
|
+
data.tar.gz: 72057e516212b80260b45fe9b2bd5c97530fc5f6ceaab42c0578d48581bf3b50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c4ee2bc7ae24fb246f9744f161088222af6c02d85c99d62e39f196257b90d24abcea21de0217ea578d8755acfd61bf4d078d9f7290f3cc0cc28110bca893cd9
|
7
|
+
data.tar.gz: '02582a7cbe08a616e35828e0537bacf41714cd4cfae0d5272188fb9feb8aa970c44670391b1cef6926781ebe53f28ce966a0eeda46d32e2149be22370cf19daf'
|
data/README.md
CHANGED
@@ -17,7 +17,7 @@ This is a very generic mechanism that enables many possible utilizations, for in
|
|
17
17
|
- **Static configuration management**
|
18
18
|
- **Rich Java/C#-like enums**
|
19
19
|
|
20
|
-
You can find examples of these in [Usage -> Examples
|
20
|
+
You can find examples of these in [Usage -> Examples](#Examples).
|
21
21
|
|
22
22
|
## TL;DR
|
23
23
|
|
@@ -54,8 +54,8 @@ object.module_field == Nested::Module
|
|
54
54
|
module MyNameSpace
|
55
55
|
using ActiveModule::Comparison
|
56
56
|
|
57
|
-
object.module_field =~ :
|
58
|
-
object.module_field =~ "
|
57
|
+
object.module_field =~ :Module
|
58
|
+
object.module_field =~ "Module"
|
59
59
|
end
|
60
60
|
```
|
61
61
|
|
@@ -64,7 +64,7 @@ end
|
|
64
64
|
Add to your gemfile - and if you are using rails - that's all you need:
|
65
65
|
|
66
66
|
```ruby
|
67
|
-
gem 'active_module', "~> 0.
|
67
|
+
gem 'active_module', "~> 0.2"
|
68
68
|
```
|
69
69
|
|
70
70
|
If you are not using rails, just issue this command after loading active record
|
@@ -86,7 +86,7 @@ ActiveRecord::Type.register(:active_module, ActiveModule::Base)
|
|
86
86
|
Add a string field to the table you want to hold a module attribute in your migrations
|
87
87
|
```ruby
|
88
88
|
create_table :my_ar_objects do |t|
|
89
|
-
t.string :module_field
|
89
|
+
t.string :module_field, index: true
|
90
90
|
end
|
91
91
|
```
|
92
92
|
|
@@ -108,6 +108,16 @@ class MyARObject < ActiveRecord::Base
|
|
108
108
|
possible_modules: [MyModule1, MyModule2, MyClass, MyClass::MyModule1]
|
109
109
|
end
|
110
110
|
```
|
111
|
+
|
112
|
+
Optionally, you can specify how to map your modules into the database
|
113
|
+
(the default is the module's fully qualified name):
|
114
|
+
```ruby
|
115
|
+
attribute :module_field,
|
116
|
+
:active_module,
|
117
|
+
possible_modules: [MyModule1, MyModule2, MyClass, MyClass::MyModule1]
|
118
|
+
mapping: {MyModule1 => "this is the db representation of module1"}
|
119
|
+
```
|
120
|
+
|
111
121
|
And this is it! Easy!<br>
|
112
122
|
|
113
123
|
### Assigning and querying module attributes
|
@@ -232,6 +242,39 @@ MyARObject.create!(module_field: :Strategy1).run_strategy! #=> "strategy1 called
|
|
232
242
|
MyARObject.create!(module_field: :Strategy2).run_strategy! #=> "strategy2 called"
|
233
243
|
```
|
234
244
|
|
245
|
+
You can later easily promote these modules to classes if you need instance variables:
|
246
|
+
|
247
|
+
```ruby
|
248
|
+
class MyARObject < ActiveRecord::Base
|
249
|
+
class Strategy1
|
250
|
+
def self.call
|
251
|
+
self.new.call
|
252
|
+
end
|
253
|
+
|
254
|
+
def call
|
255
|
+
"strategy1 called"
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
module Strategy2
|
260
|
+
def self.call
|
261
|
+
"strategy2 called"
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
attribute :strategy,
|
266
|
+
:active_module,
|
267
|
+
possible_modules: [Strategy1, Strategy2]
|
268
|
+
|
269
|
+
def run_strategy!(some_args)
|
270
|
+
strategy.call(some_args, other_args)
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
MyARObject.create!(module_field: :Strategy1).run_strategy! #=> "strategy1 called"
|
275
|
+
MyARObject.create!(module_field: :Strategy2).run_strategy! #=> "strategy2 called"
|
276
|
+
```
|
277
|
+
|
235
278
|
|
236
279
|
### Rapid prototyping static domain objects
|
237
280
|
|
data/lib/active_module/base.rb
CHANGED
@@ -5,11 +5,35 @@ require_relative "modules_index"
|
|
5
5
|
|
6
6
|
module ActiveModule
|
7
7
|
class Base < ActiveModel::Type::Value
|
8
|
-
|
9
|
-
|
8
|
+
attr_reader :possible_modules, :mapping
|
9
|
+
|
10
|
+
def initialize(possible_modules_or_mapping = nil,
|
11
|
+
possible_modules: [],
|
12
|
+
mapping: {})
|
13
|
+
@possible_modules =
|
14
|
+
if possible_modules_or_mapping.is_a?(Array)
|
15
|
+
possible_modules_or_mapping + possible_modules
|
16
|
+
else
|
17
|
+
possible_modules
|
18
|
+
end
|
19
|
+
mapping_arg = if possible_modules_or_mapping.is_a?(Hash)
|
20
|
+
possible_modules_or_mapping.merge(mapping)
|
21
|
+
else
|
22
|
+
mapping
|
23
|
+
end
|
24
|
+
@mapping =
|
25
|
+
@possible_modules.each_with_object(mapping_arg.dup) do |mod, result|
|
26
|
+
result[mod] ||= mod.name
|
27
|
+
end
|
10
28
|
super()
|
11
29
|
end
|
12
30
|
|
31
|
+
def ==(other)
|
32
|
+
other.is_a?(Base) &&
|
33
|
+
possible_modules == other.possible_modules &&
|
34
|
+
mapping == other.mapping
|
35
|
+
end
|
36
|
+
|
13
37
|
def type
|
14
38
|
:active_module
|
15
39
|
end
|
@@ -38,13 +62,11 @@ module ActiveModule
|
|
38
62
|
end
|
39
63
|
|
40
64
|
def serialize(module_instance)
|
41
|
-
|
65
|
+
mapping[cast(module_instance)]
|
42
66
|
end
|
43
67
|
|
44
68
|
def deserialize(str)
|
45
|
-
str
|
46
|
-
rescue NameError
|
47
|
-
nil
|
69
|
+
from_db[str]
|
48
70
|
end
|
49
71
|
|
50
72
|
private
|
@@ -78,5 +100,9 @@ module ActiveModule
|
|
78
100
|
def modules_index
|
79
101
|
@modules_index ||= ModulesIndex.new(@possible_modules)
|
80
102
|
end
|
103
|
+
|
104
|
+
def from_db
|
105
|
+
@from_db ||= mapping.invert
|
106
|
+
end
|
81
107
|
end
|
82
108
|
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.2.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-
|
11
|
+
date: 2024-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|