current_instence 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in current_instence.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Thread Safe Current Class Instance
2
+
3
+ ## Implementation
4
+
5
+ Include the gem:
6
+
7
+ gem 'current_instence'
8
+
9
+ Use Like So:
10
+
11
+ ~~~ruby
12
+ class User < ActiveRecord::Base
13
+ include CurrentInstence
14
+ end
15
+ ~~~
16
+
17
+ ## Function
18
+
19
+ Set User.current by assigning an object
20
+
21
+ ~~~ruby
22
+ User.current = User.first
23
+ User.current #=> <User id: 1, name: "Flinn"...>
24
+
25
+ User.current = nil
26
+ User.current #=> nil
27
+ ~~~
28
+
29
+ Set Food.current by id
30
+
31
+ ~~~ruby
32
+ Food.current_id #=> nil
33
+ Food.current_id = 1
34
+ Food.current #=> <Food id: 1, name: "Fondue"...>
35
+
36
+ Food.current_id = nil
37
+ Food.current #=> nil
38
+ ~~~
39
+
40
+ ## TODO
41
+
42
+ * Tests
43
+ * Caching
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/current_instence/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Quinn"]
6
+ gem.email = ["chaffeqa@gmail.com"]
7
+ gem.description = %q{Thread Safe Current Class Instance}
8
+ gem.summary = <<-DOC
9
+ Thread Safe Current Class Instance. Use Like So:
10
+
11
+ class User < ActiveRecord::Base
12
+ include CurrentInstence
13
+ end
14
+ DOC
15
+ gem.homepage = "https://github.com/chaffeqa/Current-Instence"
16
+
17
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ gem.files = `git ls-files`.split("\n")
19
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ gem.name = "current_instence"
21
+ gem.require_paths = ["lib"]
22
+ gem.version = CurrentInstence::VERSION
23
+
24
+
25
+ end
@@ -0,0 +1,11 @@
1
+ module CurrentInstance
2
+ def self.included(base)
3
+ base.class_eval %q{
4
+ @@current_name = "current_#{base.name.downcase}".to_sym
5
+ cattr_accessor :current_name
6
+ @@current_name_id = "#\{@@current_name\}_id".to_sym
7
+ cattr_accessor :current_name_id
8
+ }
9
+ base.extend(ClassMethods)
10
+ end
11
+ end
@@ -0,0 +1,38 @@
1
+ module CurrentInstance
2
+ module ClassMethods
3
+
4
+
5
+ # Set Food.current by id:
6
+ # Food.current_id #=> nil
7
+ # Food.current_id = 1
8
+ # Food.current #=> #<Food id: 1, name: "Fondue"...>
9
+ # Food.current_id = nil
10
+ # Food.current #=> nil
11
+ def current_id
12
+ Thread.current[current_name_id]
13
+ end
14
+
15
+
16
+ def current_id=(current_id)
17
+ Thread.current[current_name_id] = current_id
18
+ end
19
+
20
+ # Set User.current by assigning an object:
21
+ # User.current = User.first
22
+ # User.current #=> #<User id: 1, name: "Flinn"...>
23
+ # User.current = nil
24
+ # User.current #=> nil
25
+ def current
26
+ unless current_id.blank?
27
+ Thread.current[current_name] ||= self.respond_to?(:cached_find_by_id) ?
28
+ cached_find_by_id(current_id) :
29
+ find_by_id(current_id)
30
+ end
31
+ end
32
+
33
+ def current=(current)
34
+ Thread.current[current_name] = current
35
+ self.current_id = current.blank? ? nil : (current.id rescue nil)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module CurrentInstence
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ require "current_instence/version"
2
+ require "current_instence/base"
3
+ require "current_instence/class_methods"
4
+
5
+ # Thread Safe Current Class Instance
6
+ #
7
+ # Use Like So:
8
+ # class User < ActiveRecord::Base
9
+ # include CurrentInstence
10
+ # end
11
+ #
12
+ module CurrentInstence
13
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: current_instence
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Quinn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Thread Safe Current Class Instance
15
+ email:
16
+ - chaffeqa@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - README.md
24
+ - Rakefile
25
+ - current_instence.gemspec
26
+ - lib/current_instence.rb
27
+ - lib/current_instence/base.rb
28
+ - lib/current_instence/class_methods.rb
29
+ - lib/current_instence/version.rb
30
+ homepage: https://github.com/chaffeqa/Current-Instence
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.10
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: ! 'Thread Safe Current Class Instance. Use Like So: class User < ActiveRecord::Base
54
+ include CurrentInstence end'
55
+ test_files: []