ruby_multiton 0.0.5.pre → 0.0.6.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +179 -2
- data/lib/multiton/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51a3a796c0ca21cd5533ca4c3e5932945261dc3d
|
4
|
+
data.tar.gz: 5866f116e8d626414cc135386cef3eb4598fd92c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a48dbcaefbc2e1cc2d17269b7b21eba9a41f39e45328ce5655cb70dafc0609ce367ffd9aaccdd3bbb4f2626e88ab8802cb28c91f2a83d83a4fb962e7d9d86061
|
7
|
+
data.tar.gz: 63ea49d20f752e16f397bd03b7d01621084b9a2ee48742c6733195ff48a42d6416e6398eb51e078afa931a5299b6e3d9e47b91bdd1e2a7f238d0c81e9ce07ada
|
data/README.md
CHANGED
@@ -6,7 +6,184 @@
|
|
6
6
|
[![Test Coverage](http://img.shields.io/codeclimate/coverage/github/gdeoliveira/ruby_multiton.svg)][codeclimate]
|
7
7
|
[![Inline docs](http://inch-ci.org/github/gdeoliveira/ruby_multiton.svg?branch=master)][inch-ci]
|
8
8
|
|
9
|
-
[
|
10
|
-
|
9
|
+
Multiton is an implementation of the [multiton pattern][multiton] in pure Ruby. Some its features include:
|
10
|
+
|
11
|
+
- Can be used to implement the [singleton pattern][singleton] in a very straightforwad way
|
12
|
+
- Transparent interface that makes designing multiton classes as easy as designing regular ones
|
13
|
+
- Support for [serializing][marshal_dump] and [deserializing][marshal_load] multiton instances
|
14
|
+
- Clonning, duplicating and inheriting from a multiton class will create a *new* multiton class
|
15
|
+
- Thread safety, implemented via [shared/exclusive locks][sync]
|
16
|
+
- [Compatible with MRI, JRuby and other Ruby implementations][travis].
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem "ruby_multiton"
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install ruby_multiton
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
#### Implementing a singleton class
|
37
|
+
|
38
|
+
The easiest use case for Multiton is to implement a singleton class. To achieve this Multiton mimics the approach used
|
39
|
+
by the [Singleton module][singleton_module] from Ruby's standard library:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require "multiton"
|
43
|
+
|
44
|
+
class C
|
45
|
+
extend Multiton
|
46
|
+
end
|
47
|
+
|
48
|
+
C.instance.object_id #=> 47143710911900
|
49
|
+
C.instance.object_id #=> 47143710911900
|
50
|
+
```
|
51
|
+
|
52
|
+
As an example lets create a singleton object that returns the amount of seconds elapsed since it was first initialized:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
class Timestamp
|
56
|
+
extend Multiton
|
57
|
+
|
58
|
+
def initialize
|
59
|
+
self.initialized_at = Time.now
|
60
|
+
end
|
61
|
+
|
62
|
+
def elapsed_seconds
|
63
|
+
Time.now - initialized_at
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
attr_accessor :initialized_at
|
69
|
+
end
|
70
|
+
|
71
|
+
Timestamp.instance #=> #<Timestamp:0x00562e486384c8 @initialized_at=2017-04-17 20:00:15 +0000>
|
72
|
+
|
73
|
+
# Some time later...
|
74
|
+
Timestamp.instance.elapsed_seconds #=> 542.955918039
|
75
|
+
```
|
76
|
+
|
77
|
+
#### Implementing a multiton class
|
78
|
+
|
79
|
+
To implement a multiton class we will need a `key` to be able to access the different instances afterwards. Multiton
|
80
|
+
achieves this by using the parameters passed to the `initialize` method as the `key`:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
require "multiton"
|
84
|
+
|
85
|
+
class C
|
86
|
+
extend Multiton
|
87
|
+
|
88
|
+
def initialize(key)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
C.instance(:one).object_id #=> 47387777147320
|
93
|
+
C.instance(:one).object_id #=> 47387777147320
|
94
|
+
|
95
|
+
C.instance(:two).object_id #=> 47387776632880
|
96
|
+
C.instance(:two).object_id #=> 47387776632880
|
97
|
+
```
|
98
|
+
|
99
|
+
As an example lets create multiton objects representing playing cards that we can check if they were drawn or not:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
class Card
|
103
|
+
extend Multiton
|
104
|
+
|
105
|
+
def initialize(number, suit)
|
106
|
+
self.has_been_drawn = false
|
107
|
+
end
|
108
|
+
|
109
|
+
def draw
|
110
|
+
self.has_been_drawn = true
|
111
|
+
end
|
112
|
+
|
113
|
+
def drawn?
|
114
|
+
has_been_drawn
|
115
|
+
end
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
attr_accessor :has_been_drawn
|
120
|
+
end
|
121
|
+
|
122
|
+
Card.instance(10, :spades).drawn? #=> false
|
123
|
+
|
124
|
+
Card.instance(5, :hearts).draw
|
125
|
+
Card.instance(10, :spades).draw
|
126
|
+
|
127
|
+
Card.instance(5, :hearts).drawn? #=> true
|
128
|
+
Card.instance(10, :spades).drawn? #=> true
|
129
|
+
Card.instance(2, :diamonds).drawn? #=> false
|
130
|
+
```
|
131
|
+
|
132
|
+
#### Serializing and deserializing multiton instances
|
133
|
+
|
134
|
+
Multiton instances can be serialized and deserialized like regular objects. Continuing with the previous `Card` example:
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
Card.instance(2, :diamonds).drawn? #=> false
|
138
|
+
|
139
|
+
serialized_card = Marshal.dump(Card.instance(2, :diamonds))
|
140
|
+
|
141
|
+
Marshal.load(serialized_card).drawn? #=> false
|
142
|
+
|
143
|
+
Card.instance(2, :diamonds).draw
|
144
|
+
|
145
|
+
Marshal.load(serialized_card).drawn? #=> true
|
146
|
+
```
|
147
|
+
|
148
|
+
#### Clonning, duplicating and inheriting from multiton classes
|
149
|
+
|
150
|
+
Multiton supports cloning, duplicating and inheriting from a multiton class:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
require "multiton"
|
154
|
+
|
155
|
+
class C
|
156
|
+
extend Multiton
|
157
|
+
end
|
158
|
+
|
159
|
+
D = C.clone
|
160
|
+
|
161
|
+
E = D.dup
|
162
|
+
|
163
|
+
class F < E
|
164
|
+
end
|
165
|
+
|
166
|
+
F.instance.object_id #=> 47418482076880
|
167
|
+
F.instance.object_id #=> 47418482076880
|
168
|
+
```
|
169
|
+
|
170
|
+
Note that `C`, `D`, `E` and `F` are all considered different classes and will consequently not share any instances.
|
171
|
+
|
172
|
+
## Contributing
|
173
|
+
|
174
|
+
1. Fork it ( https://github.com/gdeoliveira/ruby_multiton/fork )
|
175
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
176
|
+
3. Commit your changes (`git commit -am "Add some feature"`)
|
177
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
178
|
+
5. Create a new Pull Request
|
179
|
+
|
11
180
|
[codeclimate]: https://codeclimate.com/github/gdeoliveira/ruby_multiton
|
181
|
+
[gem]: https://rubygems.org/gems/ruby_multiton
|
12
182
|
[inch-ci]: http://inch-ci.org/github/gdeoliveira/ruby_multiton
|
183
|
+
[marshal_dump]: https://ruby-doc.org/core-2.4.1/Marshal.html#method-c-dump
|
184
|
+
[marshal_load]: https://ruby-doc.org/core-2.4.1/Marshal.html#method-c-load
|
185
|
+
[multiton]: https://en.wikipedia.org/wiki/Multiton_pattern
|
186
|
+
[singleton]: https://en.wikipedia.org/wiki/Singleton_pattern
|
187
|
+
[singleton_module]: https://ruby-doc.org/stdlib-2.4.1/libdoc/singleton/rdoc/Singleton.html
|
188
|
+
[sync]: https://ruby-doc.org/stdlib-2.4.1/libdoc/sync/rdoc/Sync.html
|
189
|
+
[travis]: http://travis-ci.org/gdeoliveira/ruby_multiton/branches
|
data/lib/multiton/version.rb
CHANGED