pimple 0.0.1.alpha

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 ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .rspec
3
+ coverage
4
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2012 Florian Mhun.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # Pimple for Ruby
2
+
3
+ This is the Ruby implementation of Pimple, originally a leightweight Dependancy Injection library written in PHP and created by Fabien Potencier.
4
+
5
+ Refer to [the original documentation](http://pimple.sensiolabs.org/) for more details about Pimple, you could found better explanations on the topic.
6
+
7
+ ## Installation
8
+
9
+ Install with RubyGems :
10
+
11
+ ```shell
12
+ sudo gem install pimple
13
+ ```
14
+
15
+ Then, create the container :
16
+
17
+ ```ruby
18
+ require 'pimple'
19
+ container = Pimple.new
20
+ ```
21
+
22
+ ## Defining parameter
23
+
24
+ ```ruby
25
+ container[:foo] = 'bar'
26
+ container[:foo] # => bar
27
+ ```
28
+
29
+ ## Defining services
30
+
31
+ Pimple is built to provide services on demand. Just wrap a return instance to pimple in a lambda and get your service later when you need its functionnalities.
32
+
33
+ See that example to inject a redis client with some parameters:
34
+
35
+ ```ruby
36
+ # Defining redis config in container as parameter before ...
37
+ container[:redis_config] = {:host => 'localhost', :port => 6379}
38
+
39
+ # Defining the redis client into Pimple
40
+ container[:redis] = lambda { |c| Redis.new(c[:redis_config]) }
41
+
42
+ # And get an instance
43
+ container[:redis] # => #<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.2.2)>
44
+ ```
45
+
46
+ **Important!** : Each time you get a service by its key, Pimple will call your defined lambda and then return a new instance.
47
+
48
+ ## Defining shared services (Singleton like)
49
+
50
+ Sometimes, you need to work with the same instance each time you access to your service. Use `share` method as shown as below :
51
+
52
+ ```ruby
53
+ container[:session_storage] = container.share { Redis.new }
54
+ ```
55
+
56
+ ## Defining protected parameter (Wrap anonymous function as parameter)
57
+
58
+ Anonymous functions (lambda) can be passed to the container to provide a method that make something. To ensure the lambda will be not evaluated as a service, you need to use `protect` method. Then, when you get the value from the container, you just have to to access the container value to automatically call the function.
59
+ Let me show you an example with a random function :
60
+
61
+ ```ruby
62
+ # First of all, we inject the random function
63
+ container[:random] = container.protect { rand(100) }
64
+
65
+ # ... and sometime in the future, we want a random number.
66
+ # So we call the function from the container by its key
67
+ container[:random] # => 12
68
+ # ... and get another one later !
69
+ container[:random] # => 48
70
+ ```
71
+
72
+ ## Extend services
73
+
74
+ Not implemented
75
+
76
+ # Contribute <3
77
+
78
+ I'm sure this library can be improved by many ways. If you want to improve some stuff, feel free to send a pull-request with your changes. One condition, ensure to test and document your code ;).
79
+
80
+ # License
81
+
82
+ Released under the MIT License. View LICENSE file for details.
83
+ Copyright (c) 2012 Florian Mhun.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ task :default => ['spec']
6
+ task :test => :spec
7
+
8
+ require 'rspec/core/rake_task'
9
+ RSpec::Core::RakeTask.new(:spec)
data/lib/pimple.rb ADDED
@@ -0,0 +1,33 @@
1
+ class Pimple < Hash
2
+
3
+ VERSION = '0.0.1.alpha'
4
+
5
+ def initialize(parameters={})
6
+ self.replace(parameters)
7
+ end
8
+
9
+ def [](key)
10
+ obj = self.fetch key
11
+ obj.kind_of?(Proc) ? obj.call(self) : obj
12
+ rescue
13
+ raise KeyError, "Identifier \"#{key}\" is not defined."
14
+ end
15
+
16
+ def protect
17
+ raise ArgumentError, "Missing block for protected function" unless block_given?
18
+ Proc.new { yield }
19
+ end
20
+
21
+ def share
22
+ raise ArgumentError, "Missing block for shared service" unless block_given?
23
+ lambda do |c|
24
+ @object ||= yield(self)
25
+ @object
26
+ end
27
+ end
28
+
29
+ def extend
30
+ # Not implemented yet
31
+ end
32
+
33
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), 'lib/pimple')
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "pimple"
6
+ s.version = Pimple::VERSION
7
+ s.authors = ["Florian Mhun"]
8
+ s.email = ["florian.mhun@gmail.com"]
9
+ s.homepage = "https://github.com/floommon/ruby-pimple"
10
+ s.description = s.summary = "A lightweight dependency injection container for Ruby"
11
+
12
+ s.platform = Gem::Platform::RUBY
13
+ s.has_rdoc = true
14
+ s.extra_rdoc_files = ['README.md']
15
+ s.license = 'MIT'
16
+
17
+ s.add_development_dependency "rake"
18
+ s.add_development_dependency "rdoc"
19
+ s.add_development_dependency "rspec"
20
+ s.add_development_dependency "simplecov"
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
24
+ s.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pimple do
4
+
5
+ describe '.new' do
6
+
7
+ it 'should be a type of Hash' do
8
+ Pimple.new.kind_of?(Hash).should be_true
9
+ end
10
+
11
+ it 'should initialize with parameters' do
12
+ params = { :redis_classname => 'Redis' }
13
+ Pimple.new(params)[:redis_classname].should == 'Redis'
14
+ end
15
+
16
+ end
17
+
18
+ describe '.[]' do
19
+ let(:container) { Pimple.new }
20
+ before { class Facebook; end; } # Simulate Facebook Api Client class
21
+
22
+ it 'should inject service' do
23
+ container[:facebook] = lambda { |c| Facebook.new }
24
+ container[:facebook].class.should equal(Facebook)
25
+ end
26
+
27
+ it 'should return a new instance each time the [](key) method is invoked' do
28
+ container[:facebook] = lambda { |c| Facebook.new }
29
+ container[:facebook].should_not equal(container[:facebook])
30
+ end
31
+
32
+ it 'should raise KeyError if service not found' do
33
+ lambda { container[:notfound] }.should raise_error(KeyError)
34
+ end
35
+
36
+ end
37
+
38
+ describe '.protect' do
39
+ let(:container) { Pimple.new }
40
+
41
+ it 'should define anonymous function as parameter' do
42
+ container[:lambda_param] = container.protect { rand(1000) }
43
+ container[:lambda_param].should_not equal(container[:lambda_param])
44
+ end
45
+
46
+ it 'should define anonymous function as parameter by Proc.new way' do
47
+ container[:lambda_param] = Proc.new { rand(1000) }
48
+ container[:lambda_param].should_not equal(container[:lambda_param])
49
+ end
50
+
51
+ it 'should raise ArgumentError when block is missing' do
52
+ lambda { container.protect }.should raise_error(ArgumentError)
53
+ end
54
+
55
+ end
56
+
57
+ describe '.share' do
58
+ let(:container) { Pimple.new }
59
+ before { class Facebook; end; } # Simulate Facebook Api Client class
60
+
61
+ it 'should define shared (global) service' do
62
+ container[:facebook] = container.share { |c| Facebook.new }
63
+ container[:facebook].class.should equal(Facebook)
64
+ end
65
+
66
+ it 'should get the same instance each time [](key) method is invoked' do
67
+ container[:facebook] = container.share { |c| Facebook.new }
68
+ container[:facebook].should equal(container[:facebook])
69
+ end
70
+
71
+ end
72
+
73
+ describe '.extend', :pending => true do
74
+
75
+ end
76
+
77
+ end
@@ -0,0 +1,13 @@
1
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
2
+
3
+ require 'simplecov'
4
+
5
+ SimpleCov.start do
6
+ add_group 'Libraries', 'lib'
7
+ end
8
+
9
+ require 'pimple'
10
+
11
+ RSpec.configure do |config|
12
+ config.filter_run_excluding :broken => true
13
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pimple
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.alpha
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Florian Mhun
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: simplecov
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: A lightweight dependency injection container for Ruby
79
+ email:
80
+ - florian.mhun@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files:
84
+ - README.md
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE
89
+ - README.md
90
+ - Rakefile
91
+ - lib/pimple.rb
92
+ - ruby-pimple.gemspec
93
+ - spec/pimple_spec.rb
94
+ - spec/spec_helper.rb
95
+ homepage: https://github.com/floommon/ruby-pimple
96
+ licenses:
97
+ - MIT
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>'
112
+ - !ruby/object:Gem::Version
113
+ version: 1.3.1
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 1.8.21
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: A lightweight dependency injection container for Ruby
120
+ test_files:
121
+ - spec/pimple_spec.rb
122
+ - spec/spec_helper.rb