active_module 0.1.8 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- 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: fb059e4cacb00c0531503dbc9994110e4c735ba4e70dd926571998381f8977d4
|
4
|
+
data.tar.gz: 9182e410141940ad1beccbbb9556cca0fe0b801eaa6f9bb2b461005f59b75f12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b11253775eb6a2ddb1d20664784a62f9528ab0213d1670a9c8d31187719106fe27d0eb926f71bd4233da08e817820a52a7dbe74521c58b4b0b6f3d0a3ee19c8
|
7
|
+
data.tar.gz: 9ddad0a60a5afb1dc3e5b923bac3fb8800aa9af022e2312fbc44928e55a0b5eb5fd0c3b5941ed15d8965f747769fdc9aa280c8446085611131722c2360b2be5c
|
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,31 @@ 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 = [],
|
11
|
+
possible_modules: [],
|
12
|
+
mapping: {})
|
13
|
+
if possible_modules_or_mapping.is_a?(Array)
|
14
|
+
@possible_modules =
|
15
|
+
(possible_modules_or_mapping + possible_modules + mapping.keys).uniq
|
16
|
+
@mapping = default_mapping.merge(mapping)
|
17
|
+
else
|
18
|
+
@possible_modules =
|
19
|
+
(possible_modules_or_mapping.keys + possible_modules + mapping.keys)
|
20
|
+
.uniq
|
21
|
+
@mapping = default_mapping.merge(possible_modules_or_mapping)
|
22
|
+
.merge(mapping)
|
23
|
+
end
|
10
24
|
super()
|
11
25
|
end
|
12
26
|
|
27
|
+
def ==(other)
|
28
|
+
other.is_a?(Base) &&
|
29
|
+
possible_modules == other.possible_modules &&
|
30
|
+
mapping == other.mapping
|
31
|
+
end
|
32
|
+
|
13
33
|
def type
|
14
34
|
:active_module
|
15
35
|
end
|
@@ -38,13 +58,11 @@ module ActiveModule
|
|
38
58
|
end
|
39
59
|
|
40
60
|
def serialize(module_instance)
|
41
|
-
|
61
|
+
mapping[cast(module_instance)]
|
42
62
|
end
|
43
63
|
|
44
64
|
def deserialize(str)
|
45
|
-
str
|
46
|
-
rescue NameError
|
47
|
-
nil
|
65
|
+
from_db[str]
|
48
66
|
end
|
49
67
|
|
50
68
|
private
|
@@ -78,5 +96,13 @@ module ActiveModule
|
|
78
96
|
def modules_index
|
79
97
|
@modules_index ||= ModulesIndex.new(@possible_modules)
|
80
98
|
end
|
99
|
+
|
100
|
+
def from_db
|
101
|
+
@from_db ||= mapping.invert
|
102
|
+
end
|
103
|
+
|
104
|
+
def default_mapping
|
105
|
+
@possible_modules.index_by(&:name).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.1
|
4
|
+
version: 0.2.1
|
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
|