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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: abd6e41c388e3cc924ed948ca61ddc8efe92ceeb
4
- data.tar.gz: 437c82acdf440a4e59a35cdf843f1497417fed15
3
+ metadata.gz: 19f88067a73deb2fcf6d9087645c141813ef4277
4
+ data.tar.gz: 09c762c85169ba9cb7e6a7d6208c97125f1e9de4
5
5
  SHA512:
6
- metadata.gz: 08009f97eb82a37dec5f476bd1d0b693fd25032bf399d311485f2b137963dbe246e460710e6932161c77b4be3c4ed558763bd912076b9221d5bcd6f060cf6aec
7
- data.tar.gz: b9b114bed096d37d26e3a909ab10f4d6930a5309661695e8f277dc3c301b2fb9220d27ebe9e5841e3c9be807491a701907d869a1f039cfb84431df0bb2c63c7c
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 lib that wraps `extend self` in Ruby modules.
3
+ A dead simple little library to help create stateless / immutable modules in Ruby.
4
4
 
5
- ## Installation
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
- `gem install stateless_module'
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
- ## Examples
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
- ```ruby
12
- module YourModule
13
- stateless_module
13
+ ## Derp
14
14
 
15
- def some_method
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
- def complex_method
20
- some_private_method
21
- end
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
- private
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
- def some_private_method
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
- You could then use `YourModule` as a semi-stateless module / namespace:
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
- YourModule.complex_method
38
- # => 7
29
+ ## Installation
39
30
 
40
- class SomeClass
41
- include YourModule
42
- end
43
- # => Error
44
- ```
31
+ `gem install stateless_module'
45
32
 
46
- ## Why not just use `module_function` ?
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
- You could. But `module_function` has some problems with private methods.
49
- It's not really an exact replacement of `extend self`.
35
+ ## Usage
50
36
 
51
- ## OK. Why not just use `extend self` ?
37
+ Simply call the `stateless` function from within your module:
38
+
39
+ ```ruby
40
+ module MyModule
41
+ stateless_module
52
42
 
53
- You could - and maybe you should. But this lib also makes it so that any
54
- module you define as stateles won't be able to be included or extended elsewhere.
43
+ def some_function
44
+ # ...
45
+ end
55
46
 
56
- This adds a bit of safety by enforcing that your module won't get mixed up
57
- in some other class / modules state.
47
+ def another_function
48
+ # ...
49
+ end
58
50
 
59
- Also declaring `stateless_module` is a bit more descriptive than
60
- `extend self`.
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')
@@ -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
+
@@ -1,3 +1,3 @@
1
1
  module StatelessModule
2
- VERSION = "1.0.1"
2
+ VERSION = "2.0.1"
3
3
  end
@@ -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: 1.0.1
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: 2015-04-19 00:00:00.000000000 Z
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: '0'
103
+ version: 2.0.0
101
104
  required_rubygems_version: !ruby/object:Gem::Requirement
102
105
  requirements:
103
106
  - - ">="
@@ -1,5 +0,0 @@
1
- class Module
2
- def stateless_module
3
- include StatelessModule
4
- end
5
- end