active_module 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +36 -18
- data/lib/active_module/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b80aaae788c312746df8dcaba68bfda8f881fc2c22d7642b509546bdab198bc0
|
4
|
+
data.tar.gz: 84674ab108e9b639327f7531f678a12b8dbf31ab25c01088f8839458b71d1cac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a89843336735a230e13691069fd3a2e919403a140712476bb5176862fd0bb72df6ff069fa9d3ca9635c2dadc1a602863122a2ec384f8414754a560da02561698
|
7
|
+
data.tar.gz: f7b03c281aafba2a1bf187f7ed9b049c9dd3798bafdf411c3710964e2cbc087dedd72937c897925bdcbb9f4887e625f5d893b69640df853f738324b12b955e7d
|
data/README.md
CHANGED
@@ -1,19 +1,21 @@
|
|
1
|
-
# ActiveModule
|
2
1
|
|
3
|
-
|
2
|
+
# active_module
|
3
|
+

|
4
|
+
|
5
|
+
#### *Modules and Classes as first-class active record values!*
|
4
6
|
|
5
7
|
ActiveModel/ActiveRecord implementation of the Module attribute type.
|
6
8
|
|
7
|
-
- Allows storing a reference to a `Module` or `Class`
|
8
|
-
|
9
|
-
-
|
10
|
-
-
|
9
|
+
- Allows storing a reference to a `Module` or `Class` in a `:string` database field
|
10
|
+
- Automatically casts strings and symbols into modules when creating and querying objects
|
11
|
+
- Symbols or strings refer to the modules using unqualified names
|
12
|
+
- It is safe and efficient
|
11
13
|
|
12
14
|
This is a very generic mechanism that enables many possible utilizations, for instance:
|
13
|
-
-
|
14
|
-
- Rapid prototyping static domain objects
|
15
|
-
- Static configuration management
|
16
|
-
- Rich Java/C#-like enums
|
15
|
+
- **Composition-based polymorphism (Strategy design pattern)**
|
16
|
+
- **Rapid prototyping static domain objects**
|
17
|
+
- **Static configuration management**
|
18
|
+
- **Rich Java/C#-like enums**
|
17
19
|
|
18
20
|
You can find examples of these in [Usage -> Examples :](#Examples)
|
19
21
|
|
@@ -36,8 +38,19 @@ object.module_field = "Module"
|
|
36
38
|
object.module_field #=> Nested::Module:Module
|
37
39
|
```
|
38
40
|
|
39
|
-
|
41
|
+
Query them like this:
|
40
42
|
```ruby
|
43
|
+
MyARObject.where(module_field: Nested::Module)
|
44
|
+
MyARObject.where(module_field: :Module)
|
45
|
+
MyARObject.where(module_field: "Module")
|
46
|
+
object.module_field #=> Nested::Module:Module
|
47
|
+
```
|
48
|
+
|
49
|
+
And compare them like this:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
object.module_field == Nested::Module
|
53
|
+
|
41
54
|
module MyNameSpace
|
42
55
|
using ActiveModule::Comparison
|
43
56
|
|
@@ -51,7 +64,7 @@ end
|
|
51
64
|
Add to your gemfile - and if you are using rails - that's all you need:
|
52
65
|
|
53
66
|
```ruby
|
54
|
-
gem 'active_module', "~>0.1"
|
67
|
+
gem 'active_module', "~> 0.1"
|
55
68
|
```
|
56
69
|
|
57
70
|
If you are not using rails, just issue this command after loading active record
|
@@ -77,7 +90,7 @@ create_table :my_ar_objects do |t|
|
|
77
90
|
end
|
78
91
|
```
|
79
92
|
|
80
|
-
Now given this module
|
93
|
+
Now given this random module hierarchy:
|
81
94
|
```ruby
|
82
95
|
class MyARObject < ActiveRecord::Base
|
83
96
|
module MyModule1; end
|
@@ -169,23 +182,28 @@ my_ar_object.module_field == MyClass::MyModule1
|
|
169
182
|
|
170
183
|
## Examples
|
171
184
|
|
172
|
-
###
|
185
|
+
### Composition-based polymorphism (Strategy design pattern)
|
173
186
|
|
174
187
|
[The Strategy design pattern](https://en.wikipedia.org/wiki/Strategy_pattern) allows composition based polymorphism. This enables runtime polymorphism (by changing the strategy in runtime),
|
175
188
|
and multiple-polymorphism (by composing an object of multiple strategies).
|
176
189
|
|
177
|
-
If you want to use classes this will do:
|
190
|
+
If you want to use classes this will do:
|
178
191
|
```ruby
|
179
192
|
class MyARObject < ActiveRecord::Base
|
180
|
-
attribute :
|
193
|
+
attribute :strategy_class, :active_module, possible_modules: StrategySuperclass.subclasses
|
194
|
+
|
195
|
+
def strategy
|
196
|
+
@strategy ||= strategy_class.new(some_args_from_the_instance)
|
197
|
+
end
|
181
198
|
|
182
199
|
def run_strategy!(args)
|
183
|
-
strategy.
|
200
|
+
strategy.call(args)
|
184
201
|
end
|
185
202
|
end
|
186
203
|
```
|
187
204
|
|
188
|
-
But if you are not in the mood to define a class hierarchy for it
|
205
|
+
But if you are not in the mood to define a class hierarchy for it (or if you are performance-savy),
|
206
|
+
you may use modules instead:
|
189
207
|
|
190
208
|
```ruby
|
191
209
|
class MyARObject < ActiveRecord::Base
|
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.1.8
|
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-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '9'
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '7'
|
22
|
+
version: '7.1'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '9'
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '7'
|
32
|
+
version: '7.1'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: bundler
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,7 +44,7 @@ dependencies:
|
|
44
44
|
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: 1.15.0
|
47
|
-
description:
|
47
|
+
description: ActiveModel/ActiveRecord module attribute type implementation
|
48
48
|
email:
|
49
49
|
- pedrorolo@gmail.com
|
50
50
|
executables: []
|
@@ -89,5 +89,5 @@ requirements: []
|
|
89
89
|
rubygems_version: 3.5.7
|
90
90
|
signing_key:
|
91
91
|
specification_version: 4
|
92
|
-
summary:
|
92
|
+
summary: Modules and Classes as first-class active record values
|
93
93
|
test_files: []
|