active_module 0.1.4 → 0.1.6
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 -3
- data/lib/active_module/comparison.rb +26 -0
- data/lib/active_module/version.rb +1 -1
- data/lib/active_module.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1473d88ff3d53e8948747dc7827ad2836478a0c6db139c54e11f2a96df58e758
|
4
|
+
data.tar.gz: 045e7b7689734cf7c98654ec1283a747063d1defba78c47e3efbd115c5046675
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67321659d14a0aff48e5917e2242d42773dbc6f86a8fa8cef935956bbda4717858128a405264ff39cde9bd06e223ee266b08a4f4b07a62f600ee3b6de8c54714
|
7
|
+
data.tar.gz: bf7305eabb2c4d81e0514542c7172e440aaf5b17ea5d4bf732705100f8a92cc01f772aae9b3ba0f864c575f1040626d7730f5831d7d7889aec0ed88980f5271a
|
data/README.md
CHANGED
@@ -17,6 +17,35 @@ This is a very generic mechanism that enables many possible utilizations, for in
|
|
17
17
|
|
18
18
|
You can find examples of these in [Usage -> Examples :](#Examples)
|
19
19
|
|
20
|
+
## TL;DR
|
21
|
+
|
22
|
+
Declare module attributes like this:
|
23
|
+
```ruby
|
24
|
+
class MyARObject < ActiveRecord::Base
|
25
|
+
attribute :module_field,
|
26
|
+
:active_module,
|
27
|
+
possible_modules: [MyModule1, MyClass, Nested::Module]
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
Assign them like this:
|
32
|
+
```ruby
|
33
|
+
object.module_field = Nested::Module
|
34
|
+
object.module_field = :Module
|
35
|
+
object.module_field = "Module"
|
36
|
+
object.module_field #=> Nested::Module:Module
|
37
|
+
```
|
38
|
+
|
39
|
+
And compare them like this (optional):
|
40
|
+
```ruby
|
41
|
+
module MyNameSpace
|
42
|
+
using ActiveModule::Comparison
|
43
|
+
|
44
|
+
object.module_field =~ :Module1
|
45
|
+
object.module_field =~ "Module1"
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
20
49
|
## Installation
|
21
50
|
|
22
51
|
Add to your gemfile - and if you are using rails - that's all you need:
|
@@ -54,7 +83,7 @@ class MyARObject < ActiveRecord::Base
|
|
54
83
|
module MyModule1; end
|
55
84
|
module MyModule2; end
|
56
85
|
class MyClass;
|
57
|
-
module
|
86
|
+
module MyModule1; end
|
58
87
|
end
|
59
88
|
end
|
60
89
|
```
|
@@ -63,13 +92,13 @@ You can make the field refer to one of these modules/classes like this:
|
|
63
92
|
class MyARObject < ActiveRecord::Base
|
64
93
|
attribute :module_field,
|
65
94
|
:active_module,
|
66
|
-
possible_modules: [MyModule1, MyModule2, MyClass, MyClass::
|
95
|
+
possible_modules: [MyModule1, MyModule2, MyClass, MyClass::MyModule1]
|
67
96
|
end
|
68
97
|
```
|
69
98
|
And this is it! Easy!<br>
|
70
99
|
|
71
100
|
### Assigning and querying module attributes
|
72
|
-
Now you can use this attribute in many handy ways
|
101
|
+
Now you can use this attribute in many handy ways!
|
73
102
|
<br>
|
74
103
|
<br>
|
75
104
|
For instance, you may refer to it using module literals:
|
@@ -109,6 +138,22 @@ my_ar_object.module_field = "MyClass::MyModule1"
|
|
109
138
|
my_ar_object.module_field #=> MyARObject::MyClass::MyModule::Module
|
110
139
|
```
|
111
140
|
|
141
|
+
### Comparing modules with strings and symbols
|
142
|
+
|
143
|
+
In order to compare modules with Strings or Symbols you'll have to use the `ActiveModule::Comparison`
|
144
|
+
refinement. This refinement adds the method `Module#=~` to the `Module` class, but this change is
|
145
|
+
only available within the namespace that includes the refinement.
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
module YourClassOrModuleThatWantsToCompare
|
149
|
+
using ActiveModule::Comparison
|
150
|
+
|
151
|
+
def method_that_compares
|
152
|
+
my_ar_object.module_field =~ :MyModule1
|
153
|
+
end
|
154
|
+
end
|
155
|
+
```
|
156
|
+
|
112
157
|
## Examples
|
113
158
|
|
114
159
|
### Strategy Pattern (composition-based polymorphism)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModule
|
4
|
+
module Comparison
|
5
|
+
refine ::Module do
|
6
|
+
using ActiveModule::ModuleRefinement
|
7
|
+
|
8
|
+
def =~(other)
|
9
|
+
case other
|
10
|
+
when ::String
|
11
|
+
(@possible_names ||= possible_names).include?(other)
|
12
|
+
when ::Symbol
|
13
|
+
(@possible_names ||= possible_names).include?(other.to_s)
|
14
|
+
else
|
15
|
+
self == other
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
using self
|
21
|
+
|
22
|
+
def self.compare(module1, module2)
|
23
|
+
module1 =~ module2
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/active_module.rb
CHANGED
@@ -4,6 +4,7 @@ require_relative "active_module/version"
|
|
4
4
|
require_relative "active_module/base"
|
5
5
|
require_relative "active_module/invalid_module_value"
|
6
6
|
require_relative "active_module/register"
|
7
|
+
require_relative "active_module/comparison"
|
7
8
|
require "active_module/railtie" if defined?(Rails::Railtie)
|
8
9
|
|
9
10
|
module ActiveModule
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pedro Rolo
|
@@ -58,6 +58,7 @@ files:
|
|
58
58
|
- Rakefile
|
59
59
|
- lib/active_module.rb
|
60
60
|
- lib/active_module/base.rb
|
61
|
+
- lib/active_module/comparison.rb
|
61
62
|
- lib/active_module/invalid_module_value.rb
|
62
63
|
- lib/active_module/module_refinement.rb
|
63
64
|
- lib/active_module/modules_index.rb
|