stateless_module 1.0.1 → 2.0.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 +33 -40
- data/bin/bundler +16 -0
- data/bin/pry +16 -0
- data/bin/rake +16 -0
- data/bin/rspec +16 -0
- data/lib/stateless_module.rb +32 -2
- data/lib/stateless_module/version.rb +1 -1
- data/stateless_module.gemspec +2 -0
- metadata +7 -4
- data/lib/stateless_module/module.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19f88067a73deb2fcf6d9087645c141813ef4277
|
4
|
+
data.tar.gz: 09c762c85169ba9cb7e6a7d6208c97125f1e9de4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9d324e727b4cde1ab7c2b05f11203a72ac7768cd47c1537de8341979a84c00f37366ec4f2df6848db60ba5e079f368d6bd6147a8062efba3aac8589d728f870
|
7
|
+
data.tar.gz: 3f7d3148ebcc62487e77a000a4349a77605f12fa5bbd2f5d4834cd3decc447ac987ee3481a8495930c854942a5ec5a57cc117790ddf73f491a6c37a695d3f02b
|
data/README.md
CHANGED
@@ -1,61 +1,54 @@
|
|
1
1
|
# StatelessModule
|
2
2
|
|
3
|
-
A simple
|
3
|
+
A dead simple little library to help create stateless / immutable modules in Ruby.
|
4
4
|
|
5
|
-
|
5
|
+
Once you declare that your module is stateless, then it will be protected from being included or extended
|
6
|
+
elsewhere and all of its instance and class variables will be protected from mutation.
|
6
7
|
|
7
|
-
|
8
|
+
Ruby is a great object oriented language, but sometimes you want to carve out a small island of sanity where you have no state to keep track of
|
9
|
+
and where you can be guaranteed that your code won't be used out of context (e.g threading).
|
8
10
|
|
9
|
-
|
11
|
+
You can read about some of the benefits to this approach <a href="http://stackoverflow.com/questions/844536/advantages-of-stateless-programming" target="_blank">here</a>.
|
10
12
|
|
11
|
-
|
12
|
-
module YourModule
|
13
|
-
stateless_module
|
13
|
+
## Derp
|
14
14
|
|
15
|
-
|
16
|
-
42
|
17
|
-
end
|
15
|
+
> This is stupid. I could just use `module_function`, or `extend self`, or a frozen singleton, or a frozen module, or _blank_ to do the same thing...
|
18
16
|
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
Maybe. But all of those approaches lack something: `module_function` falls down with private methods; `extend self` offers zero protection against mutation
|
18
|
+
(or being included elsewhere); a frozen singleton would be pretty close, but you'd still be able to mutate instance variables and, semantically,
|
19
|
+
a singleton is an _instance_ of a thing, which is what we're trying to get away from here.
|
22
20
|
|
23
|
-
|
21
|
+
A frozen module would be very close, indeed, to what this library offers, but you'd still have no protection against any instance variables themselves
|
22
|
+
being mutated.
|
24
23
|
|
25
|
-
|
26
|
-
7
|
27
|
-
end
|
28
|
-
end
|
29
|
-
```
|
24
|
+
At the end of the day, you could write all of this code in under an hour.
|
30
25
|
|
31
|
-
|
26
|
+
Or you could just save yourself the hassle and use this gem.
|
32
27
|
|
33
|
-
```ruby
|
34
|
-
YourModule.some_method
|
35
|
-
# => 42
|
36
28
|
|
37
|
-
|
38
|
-
# => 7
|
29
|
+
## Installation
|
39
30
|
|
40
|
-
|
41
|
-
include YourModule
|
42
|
-
end
|
43
|
-
# => Error
|
44
|
-
```
|
31
|
+
`gem install stateless_module'
|
45
32
|
|
46
|
-
|
33
|
+
stateless_module makes use of <a href="http://ruby-doc.org/core-2.0.0/TracePoint.html" target="_blank">TracePoint</a> so it's only good for Ruby 2.0+
|
47
34
|
|
48
|
-
|
49
|
-
It's not really an exact replacement of `extend self`.
|
35
|
+
## Usage
|
50
36
|
|
51
|
-
|
37
|
+
Simply call the `stateless` function from within your module:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
module MyModule
|
41
|
+
stateless_module
|
52
42
|
|
53
|
-
|
54
|
-
|
43
|
+
def some_function
|
44
|
+
# ...
|
45
|
+
end
|
55
46
|
|
56
|
-
|
57
|
-
|
47
|
+
def another_function
|
48
|
+
# ...
|
49
|
+
end
|
58
50
|
|
59
|
-
|
60
|
-
|
51
|
+
# etc...
|
52
|
+
end
|
53
|
+
```
|
61
54
|
|
data/bin/bundler
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'bundler' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('bundler', 'bundler')
|
data/bin/pry
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'pry' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('pry', 'pry')
|
data/bin/rake
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rake' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('rake', 'rake')
|
data/bin/rspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rspec' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('rspec-core', 'rspec')
|
data/lib/stateless_module.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require "stateless_module/module"
|
2
|
-
|
3
1
|
module StatelessModule
|
4
2
|
class << self
|
5
3
|
def included(base)
|
6
4
|
base.send(:extend, base)
|
7
5
|
|
6
|
+
freeze_when_module_completes(base)
|
7
|
+
|
8
8
|
base.class_eval do
|
9
9
|
class << self
|
10
10
|
def included(base)
|
@@ -30,5 +30,35 @@ module StatelessModule
|
|
30
30
|
def extended(base)
|
31
31
|
raise "Use Stateless Module by including it - not by extending it."
|
32
32
|
end
|
33
|
+
|
34
|
+
#######
|
35
|
+
private
|
36
|
+
#######
|
37
|
+
|
38
|
+
def freeze_when_module_completes(base)
|
39
|
+
TracePoint.trace(:end) do |t|
|
40
|
+
if base == t.self
|
41
|
+
base.freeze
|
42
|
+
|
43
|
+
base.instance_variables.each do |v|
|
44
|
+
base.instance_variable_get(v).freeze
|
45
|
+
end
|
46
|
+
|
47
|
+
base.class_variables.each do |v|
|
48
|
+
base.class_variable_get(v).freeze
|
49
|
+
end
|
50
|
+
|
51
|
+
t.disable
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
33
56
|
end
|
34
57
|
end
|
58
|
+
|
59
|
+
class Module
|
60
|
+
def stateless_module
|
61
|
+
include StatelessModule
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
data/stateless_module.gemspec
CHANGED
@@ -9,6 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Jim Whiteman"]
|
10
10
|
spec.email = ["jimtron9000@gmail.com"]
|
11
11
|
|
12
|
+
spec.required_ruby_version = '>= 2.0.0'
|
13
|
+
|
12
14
|
spec.summary = %q{Simple lib for semi-stateless modules.}
|
13
15
|
spec.description = %q{A simple lib for semi-stateless modules in Ruby.}
|
14
16
|
spec.homepage = "https://github.com/jwhiteman/stateless_module"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stateless_module
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Whiteman
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -79,10 +79,13 @@ files:
|
|
79
79
|
- Gemfile
|
80
80
|
- README.md
|
81
81
|
- Rakefile
|
82
|
+
- bin/bundler
|
82
83
|
- bin/console
|
84
|
+
- bin/pry
|
85
|
+
- bin/rake
|
86
|
+
- bin/rspec
|
83
87
|
- bin/setup
|
84
88
|
- lib/stateless_module.rb
|
85
|
-
- lib/stateless_module/module.rb
|
86
89
|
- lib/stateless_module/version.rb
|
87
90
|
- stateless_module.gemspec
|
88
91
|
homepage: https://github.com/jwhiteman/stateless_module
|
@@ -97,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
97
100
|
requirements:
|
98
101
|
- - ">="
|
99
102
|
- !ruby/object:Gem::Version
|
100
|
-
version:
|
103
|
+
version: 2.0.0
|
101
104
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
105
|
requirements:
|
103
106
|
- - ">="
|