current_object 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +25 -0
- data/README.textile +29 -0
- data/Rakefile +2 -0
- data/current_object.gemspec +23 -0
- data/init.rb +1 -0
- data/lib/current_object.rb +30 -0
- data/lib/current_object/version.rb +3 -0
- data/spec/current_object_spec.rb +27 -0
- data/spec/spec_helper.rb +2 -0
- metadata +106 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
current_object (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.2)
|
10
|
+
rspec (2.2.0)
|
11
|
+
rspec-core (~> 2.2)
|
12
|
+
rspec-expectations (~> 2.2)
|
13
|
+
rspec-mocks (~> 2.2)
|
14
|
+
rspec-core (2.2.1)
|
15
|
+
rspec-expectations (2.2.0)
|
16
|
+
diff-lcs (~> 1.1.2)
|
17
|
+
rspec-mocks (2.2.0)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
bundler (>= 1.0.0.rc.6)
|
24
|
+
current_object!
|
25
|
+
rspec (>= 2.1)
|
data/README.textile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
h1. current_object
|
2
|
+
|
3
|
+
Do you need to keep track of a current user, account, page, etc? The current_object gem does just that.
|
4
|
+
|
5
|
+
@current_object@ takes the same approach to keeping track of a user as "SentientUser":https://github.com/bokmann/sentient_user
|
6
|
+
by David Bock, but goes completely class agnostic. You can even keep track of multiple "current" objects at once.
|
7
|
+
|
8
|
+
h2. Usage
|
9
|
+
|
10
|
+
Just require the @current_object@ gem into your project and @include CurrentObject@.
|
11
|
+
|
12
|
+
<pre><code>
|
13
|
+
# foo.rb
|
14
|
+
require 'current_object'
|
15
|
+
|
16
|
+
class Foo
|
17
|
+
include CurrentObject
|
18
|
+
end
|
19
|
+
|
20
|
+
# using my foo class
|
21
|
+
|
22
|
+
@foo = Foo.new
|
23
|
+
|
24
|
+
# time to make this my current foo instance
|
25
|
+
@foo.current!
|
26
|
+
|
27
|
+
# what is my current foo instance?
|
28
|
+
Foo.current
|
29
|
+
</code></pre>
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path("../lib/current_object/version", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "current_object"
|
6
|
+
s.version = CurrentObject::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ['Dane Harrigan']
|
9
|
+
s.email = ['dane.harrigan@gmail.com']
|
10
|
+
s.homepage = "http://rubygems.org/gems/current_object"
|
11
|
+
s.summary = "Give any class a notion of what its current instance is."
|
12
|
+
s.description = "Do you need to keep track of a current user, account, page, etc? The current_object gem does just that."
|
13
|
+
|
14
|
+
s.required_rubygems_version = ">= 1.3.6"
|
15
|
+
s.rubyforge_project = "current_object"
|
16
|
+
|
17
|
+
s.add_development_dependency "bundler", ">= 1.0.0.rc.6"
|
18
|
+
s.add_development_dependency 'rspec', '>= 2.1'
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
22
|
+
s.require_path = 'lib'
|
23
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'current_object'
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module CurrentObject
|
2
|
+
def self.included(base)
|
3
|
+
base.send(:include, CurrentObject::InstanceMethods)
|
4
|
+
base.send(:extend, CurrentObject::ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module InstanceMethods
|
8
|
+
# current! - Stores the class instance as the "current" instance for that class.
|
9
|
+
#
|
10
|
+
# @foo = Foo.new
|
11
|
+
# @foo.current!
|
12
|
+
#
|
13
|
+
def current!
|
14
|
+
Thread.current[self.class.name] = self
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
# current - Returns the instance of the class that is stored as the "current" instance.
|
20
|
+
#
|
21
|
+
# @foo = Foo.new
|
22
|
+
# @foo.current!
|
23
|
+
#
|
24
|
+
# Foo.current
|
25
|
+
# # => @foo
|
26
|
+
def current
|
27
|
+
Thread.current[self.name]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Foo
|
4
|
+
include CurrentObject
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Foo do
|
8
|
+
before(:each) { @foo = Foo.new }
|
9
|
+
|
10
|
+
describe '#current!' do
|
11
|
+
it 'stores the class instance in Thread.current[<class-name>]' do
|
12
|
+
@foo.current!
|
13
|
+
Thread.current['Foo'].should == @foo
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'returns itself' do
|
17
|
+
@foo.current!.should == @foo
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '.current' do
|
22
|
+
it 'returns the value stored in Thread.current[<class-name>]' do
|
23
|
+
Thread.current['Foo'] = @foo
|
24
|
+
Foo.current.should == @foo
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: current_object
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
version: "0.1"
|
9
|
+
platform: ruby
|
10
|
+
authors:
|
11
|
+
- Dane Harrigan
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
|
16
|
+
date: 2010-12-08 00:00:00 -05:00
|
17
|
+
default_executable:
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: bundler
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 0
|
30
|
+
- 0
|
31
|
+
- rc
|
32
|
+
- 6
|
33
|
+
version: 1.0.0.rc.6
|
34
|
+
type: :development
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
segments:
|
45
|
+
- 2
|
46
|
+
- 1
|
47
|
+
version: "2.1"
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
description: Do you need to keep track of a current user, account, page, etc? The current_object gem does just that.
|
51
|
+
email:
|
52
|
+
- dane.harrigan@gmail.com
|
53
|
+
executables: []
|
54
|
+
|
55
|
+
extensions: []
|
56
|
+
|
57
|
+
extra_rdoc_files: []
|
58
|
+
|
59
|
+
files:
|
60
|
+
- .gitignore
|
61
|
+
- Gemfile
|
62
|
+
- Gemfile.lock
|
63
|
+
- README.textile
|
64
|
+
- Rakefile
|
65
|
+
- current_object.gemspec
|
66
|
+
- init.rb
|
67
|
+
- lib/current_object.rb
|
68
|
+
- lib/current_object/version.rb
|
69
|
+
- spec/current_object_spec.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
has_rdoc: true
|
72
|
+
homepage: http://rubygems.org/gems/current_object
|
73
|
+
licenses: []
|
74
|
+
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
segments:
|
94
|
+
- 1
|
95
|
+
- 3
|
96
|
+
- 6
|
97
|
+
version: 1.3.6
|
98
|
+
requirements: []
|
99
|
+
|
100
|
+
rubyforge_project: current_object
|
101
|
+
rubygems_version: 1.3.7
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: Give any class a notion of what its current instance is.
|
105
|
+
test_files: []
|
106
|
+
|