sentient_model 1.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/Gemfile +7 -0
- data/Gemfile.lock +27 -0
- data/Rakefile +10 -0
- data/lib/sentient_model.rb +35 -0
- data/sentient_model.gemspec +39 -0
- data/spec/sentient_model_spec.rb +60 -0
- data/spec/spec_helper.rb +5 -0
- metadata +107 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sentient_model (1.0.0)
|
5
|
+
bundler (>= 1.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.1.2)
|
11
|
+
rake (0.9.0)
|
12
|
+
rspec (2.6.0)
|
13
|
+
rspec-core (~> 2.6.0)
|
14
|
+
rspec-expectations (~> 2.6.0)
|
15
|
+
rspec-mocks (~> 2.6.0)
|
16
|
+
rspec-core (2.6.3)
|
17
|
+
rspec-expectations (2.6.0)
|
18
|
+
diff-lcs (~> 1.1.2)
|
19
|
+
rspec-mocks (2.6.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
rake (>= 0.9)
|
26
|
+
rspec (>= 2.6.0)
|
27
|
+
sentient_model!
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module SentientModel
|
2
|
+
def self.included(base)
|
3
|
+
base.extend(ClassMethods)
|
4
|
+
end
|
5
|
+
|
6
|
+
def make_current
|
7
|
+
self.class.current = self
|
8
|
+
end
|
9
|
+
|
10
|
+
def current?
|
11
|
+
self.class.has_current? && self == self.class.current
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
def current
|
16
|
+
Thread.current[sentient_model_name]
|
17
|
+
end
|
18
|
+
|
19
|
+
def current=(m)
|
20
|
+
unless (m.is_a?(self) || m.nil?)
|
21
|
+
raise(ArgumentError, "Expected an object of class '#{self}', got #{m.inspect}")
|
22
|
+
end
|
23
|
+
Thread.current[sentient_model_name] = m
|
24
|
+
end
|
25
|
+
|
26
|
+
def has_current?
|
27
|
+
!self.current.nil?
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def sentient_model_name
|
32
|
+
self.name.downcase.to_sym
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'sentient_model'
|
3
|
+
s.version = '1.0.1'
|
4
|
+
s.platform = Gem::Platform::RUBY
|
5
|
+
s.summary = 'Define variables available to an entire request with ease.'
|
6
|
+
s.description = 'Inspired by sentient_user Gem.'
|
7
|
+
|
8
|
+
s.required_ruby_version = '>= 1.8.7'
|
9
|
+
s.required_rubygems_version = ">= 1.3.6"
|
10
|
+
|
11
|
+
s.author = 'Christoph Schiessl'
|
12
|
+
s.email = 'chs@proactive.cc'
|
13
|
+
s.homepage = "http://github.com/cs/sentientmodel"
|
14
|
+
|
15
|
+
s.homepage = %q{http://github.com/bokmann/sentient_user}
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubygems_version = %q{1.3.7}
|
18
|
+
s.summary = %q{A trivial bit of common code}
|
19
|
+
|
20
|
+
s.require_path = "lib"
|
21
|
+
|
22
|
+
s.files = [
|
23
|
+
"Rakefile",
|
24
|
+
"Gemfile",
|
25
|
+
"Gemfile.lock",
|
26
|
+
"lib/sentient_model.rb",
|
27
|
+
"sentient_model.gemspec",
|
28
|
+
"spec/spec_helper.rb",
|
29
|
+
"spec/sentient_model_spec.rb"
|
30
|
+
]
|
31
|
+
|
32
|
+
s.test_files = [
|
33
|
+
"spec/spec_helper.rb",
|
34
|
+
"spec/sentient_model_spec.rb"
|
35
|
+
]
|
36
|
+
|
37
|
+
s.add_dependency('bundler', '>= 1.0')
|
38
|
+
s.add_development_dependency('rake', '>= 0.9')
|
39
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TestClass
|
4
|
+
include SentientModel
|
5
|
+
end
|
6
|
+
|
7
|
+
class OtherTestClass
|
8
|
+
include SentientModel
|
9
|
+
end
|
10
|
+
|
11
|
+
describe SentientModel do
|
12
|
+
before do
|
13
|
+
@foo = TestClass.new
|
14
|
+
@bar = TestClass.new
|
15
|
+
end
|
16
|
+
|
17
|
+
context "by default there should be no sentient model" do
|
18
|
+
it { TestClass.current.should be_nil }
|
19
|
+
it { TestClass.has_current?.should be_false }
|
20
|
+
it { @foo.current?.should be_false }
|
21
|
+
it { @bar.current?.should be_false }
|
22
|
+
end
|
23
|
+
|
24
|
+
context "#make_current" do
|
25
|
+
before { @foo.make_current }
|
26
|
+
it { TestClass.current.should eql(@foo) }
|
27
|
+
it { TestClass.has_current?.should be_true }
|
28
|
+
it { @foo.current?.should be_true }
|
29
|
+
it { @bar.current?.should be_false }
|
30
|
+
end
|
31
|
+
|
32
|
+
context ".current=" do
|
33
|
+
before { TestClass.current = @foo }
|
34
|
+
it { TestClass.current.should eql(@foo) }
|
35
|
+
it { TestClass.has_current?.should be_true }
|
36
|
+
it { @foo.current?.should be_true }
|
37
|
+
it { @bar.current?.should be_false }
|
38
|
+
context "with invalid argument" do
|
39
|
+
it { ( lambda { TestClass.current = OtherTestClass.new } ).should raise_error(ArgumentError) }
|
40
|
+
end
|
41
|
+
context "with nil argument" do
|
42
|
+
before { TestClass.current = nil }
|
43
|
+
it { TestClass.current.should be_nil }
|
44
|
+
it { TestClass.has_current?.should be_false }
|
45
|
+
it { @foo.current?.should be_false }
|
46
|
+
it { @bar.current?.should be_false }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "two sentient models shouldn't effect each other" do
|
51
|
+
before do
|
52
|
+
@foo = TestClass.new.make_current
|
53
|
+
@bar = OtherTestClass.new.make_current
|
54
|
+
end
|
55
|
+
it { TestClass.current.should eql(@foo) }
|
56
|
+
it { TestClass.current.should_not eql(@bar) }
|
57
|
+
it { OtherTestClass.current.should eql(@bar) }
|
58
|
+
it { OtherTestClass.current.should_not eql(@foo) }
|
59
|
+
end
|
60
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sentient_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 21
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Christoph Schiessl
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-05-28 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: bundler
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 15
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
version: "1.0"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 25
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
- 9
|
48
|
+
version: "0.9"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
description: Inspired by sentient_user Gem.
|
52
|
+
email: chs@proactive.cc
|
53
|
+
executables: []
|
54
|
+
|
55
|
+
extensions: []
|
56
|
+
|
57
|
+
extra_rdoc_files: []
|
58
|
+
|
59
|
+
files:
|
60
|
+
- Rakefile
|
61
|
+
- Gemfile
|
62
|
+
- Gemfile.lock
|
63
|
+
- lib/sentient_model.rb
|
64
|
+
- sentient_model.gemspec
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
- spec/sentient_model_spec.rb
|
67
|
+
has_rdoc: true
|
68
|
+
homepage: http://github.com/bokmann/sentient_user
|
69
|
+
licenses: []
|
70
|
+
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
hash: 57
|
82
|
+
segments:
|
83
|
+
- 1
|
84
|
+
- 8
|
85
|
+
- 7
|
86
|
+
version: 1.8.7
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
hash: 23
|
93
|
+
segments:
|
94
|
+
- 1
|
95
|
+
- 3
|
96
|
+
- 6
|
97
|
+
version: 1.3.6
|
98
|
+
requirements: []
|
99
|
+
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.6.2
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: A trivial bit of common code
|
105
|
+
test_files:
|
106
|
+
- spec/spec_helper.rb
|
107
|
+
- spec/sentient_model_spec.rb
|