registry-rails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +68 -0
- data/Rakefile +2 -0
- data/lib/registry-rails.rb +33 -0
- data/lib/registry/rails/engine.rb +8 -0
- data/lib/registry/rails/version.rb +5 -0
- data/registry-rails.gemspec +20 -0
- metadata +58 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Juan Felipe Alvarez Saldarriaga
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Registry::Rails
|
2
|
+
|
3
|
+
A container for storing objects and values in the application as long as the request lives. Registry implements
|
4
|
+
ruby's Singleton module, the same object is always available throughout your application.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem "registry-rails"
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install registry-rails
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Set objects and values that you want to retrieve later.
|
23
|
+
|
24
|
+
````ruby
|
25
|
+
register = Registry::Rails::Register.instance
|
26
|
+
register.set(:foo, "bar")
|
27
|
+
register.set(:colors, ["green", "white", "blue"])
|
28
|
+
````
|
29
|
+
|
30
|
+
You can get a `default value` if the key doesn't exist, or if you want that isn't `nil`.
|
31
|
+
|
32
|
+
````ruby
|
33
|
+
register = Registry::Rails::Register.instance
|
34
|
+
register.get(:foo) #=> "bar"
|
35
|
+
register.get(:colors) #=> ["green", "white", "blue"]
|
36
|
+
register.get(:baz, "default_value") #=> "default_value"
|
37
|
+
````
|
38
|
+
|
39
|
+
The original issue was: How to get the `current_user` data inside a model?, so I ended up
|
40
|
+
with this gem, so, now, the example.
|
41
|
+
|
42
|
+
````ruby
|
43
|
+
class ApplicationController < ActionController::Base
|
44
|
+
before_filter :set_current_user
|
45
|
+
|
46
|
+
def set_current_user
|
47
|
+
register = Registry::Rails::Register.instance
|
48
|
+
register.set(:current_user, current_user)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
````
|
52
|
+
|
53
|
+
````ruby
|
54
|
+
class Foo < ActiveRecord::Base
|
55
|
+
def self.do_something(options = {})
|
56
|
+
register = Registry::Rails::Register.instance
|
57
|
+
current_user_data = register.get(:current_user)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
````
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
1. Fork it
|
65
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
66
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
67
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
68
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module Registry
|
2
|
+
module Rails
|
3
|
+
require "registry/rails/engine" if defined?(Rails)
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
require "singleton"
|
8
|
+
require "registry/rails/version"
|
9
|
+
|
10
|
+
module Registry
|
11
|
+
module Rails
|
12
|
+
class Register
|
13
|
+
include Singleton
|
14
|
+
|
15
|
+
attr_reader :registry
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@registry = {}
|
19
|
+
end
|
20
|
+
|
21
|
+
def set(key, value = nil)
|
22
|
+
@registry[key.to_s] = value
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def get(key, default = nil)
|
27
|
+
return @registry[key.to_s] if @registry.key?(key.to_s)
|
28
|
+
|
29
|
+
default
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/registry/rails/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Juan Felipe Alvarez Saldarriaga"]
|
6
|
+
gem.email = ["nebiros@gmail.com"]
|
7
|
+
gem.description = <<-EOF
|
8
|
+
A container for storing objects and values in the application as long as the request lives. Registry implements
|
9
|
+
ruby's Singleton module, the same object is always available throughout your application.
|
10
|
+
EOF
|
11
|
+
gem.summary = %q{A container for storing objects and values in the application as long as the request lives.}
|
12
|
+
gem.homepage = ""
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split($\)
|
15
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.name = "registry-rails"
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.version = Registry::Rails::VERSION
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: registry-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Juan Felipe Alvarez Saldarriaga
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-27 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! " A container for storing objects and values in the application
|
15
|
+
as long as the request lives. Registry implements\n ruby's Singleton module,
|
16
|
+
the same object is always available throughout your application.\n"
|
17
|
+
email:
|
18
|
+
- nebiros@gmail.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- lib/registry-rails.rb
|
29
|
+
- lib/registry/rails/engine.rb
|
30
|
+
- lib/registry/rails/version.rb
|
31
|
+
- registry-rails.gemspec
|
32
|
+
homepage: ''
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.24
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: A container for storing objects and values in the application as long as
|
56
|
+
the request lives.
|
57
|
+
test_files: []
|
58
|
+
has_rdoc:
|