service_factory 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +24 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +65 -0
- data/Rakefile +1 -0
- data/lib/service_factory/version.rb +3 -0
- data/lib/service_factory.rb +41 -0
- data/service_factory.gemspec +26 -0
- data/spec/service_factory_spec.rb +54 -0
- data/spec/spec_helper.rb +23 -0
- metadata +124 -0
data/.gitignore
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
coverage
|
6
|
+
InstalledFiles
|
7
|
+
.yardoc
|
8
|
+
Gemfile.lock
|
9
|
+
InstalledFiles
|
10
|
+
_yardoc
|
11
|
+
coverage
|
12
|
+
doc/
|
13
|
+
lib/bundler/man
|
14
|
+
pkg
|
15
|
+
rdoc
|
16
|
+
spec/reports
|
17
|
+
test/tmp
|
18
|
+
test/version_tmp
|
19
|
+
tmp
|
20
|
+
|
21
|
+
# YARD artifacts
|
22
|
+
.yardoc
|
23
|
+
_yardoc
|
24
|
+
doc/
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Denis Kirichenko
|
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,65 @@
|
|
1
|
+
# ServiceFactory
|
2
|
+
|
3
|
+
IOC container
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'service_factory'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install service_factory
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
1. Create service_factory.rb in config/initializers directory
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
ServiceFactory.register do
|
25
|
+
user_info UserInfo
|
26
|
+
remote_data_service do |url|
|
27
|
+
RemoteData.new(url)
|
28
|
+
end
|
29
|
+
|
30
|
+
env :test, :development do
|
31
|
+
user_info Fake::UserInfo
|
32
|
+
end
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
2. Then you can use it
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
ServiceFactory.user_info("John Dou") #Same as UserInfo.new("John Dou") or Fake::UserInfo.new("John Dou")
|
40
|
+
ServiceFactory.remote_data_service("http://service.com") #Same as RemoteData.new("http://service.com")
|
41
|
+
```
|
42
|
+
|
43
|
+
See spec/service_factory_spec.rb for the full list of features
|
44
|
+
|
45
|
+
3. You can also use rspec-mock with this factory
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
ServiceFactory.should_receive(:user_info).with("John Dou").and_return(double(:user_info, address: "baker street"))
|
49
|
+
```
|
50
|
+
|
51
|
+
4. Sometimes runing rails in development (for example, using `rails s`) lead to empty factory after reloading. To prevent this add code to config/environment/development.rb
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
ActionDispatch::Reloader.to_prepare do
|
55
|
+
load Rails.root.join('config/initializers/implementation.rb')
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
1. Fork it
|
62
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
63
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
64
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
65
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "service_factory/version"
|
2
|
+
|
3
|
+
module ServiceFactory
|
4
|
+
@blocks = {}
|
5
|
+
|
6
|
+
def register(&block)
|
7
|
+
Builder.new(@blocks).instance_eval(&block)
|
8
|
+
end
|
9
|
+
module_function :register
|
10
|
+
|
11
|
+
def self.method_missing(m, *args)
|
12
|
+
if @blocks.include?(m)
|
13
|
+
@blocks[m].call(*args)
|
14
|
+
else
|
15
|
+
super
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Builder
|
20
|
+
def initialize(blocks)
|
21
|
+
@blocks = blocks
|
22
|
+
end
|
23
|
+
|
24
|
+
def method_missing(m, *args, &block)
|
25
|
+
if block_given?
|
26
|
+
@blocks[m] = block
|
27
|
+
elsif args.first.is_a?(Class)
|
28
|
+
cl = args.first
|
29
|
+
@blocks[m] = Proc.new { |*class_args| cl.new(*class_args) }
|
30
|
+
else
|
31
|
+
super
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def env(*environments, &block)
|
36
|
+
if environments.map{|e| e.to_s}.include?(Rails.env)
|
37
|
+
instance_eval(&block)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'service_factory/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "service_factory"
|
8
|
+
spec.version = ServiceFactory::VERSION
|
9
|
+
spec.authors = ["Denis Kirichenko"]
|
10
|
+
spec.email = ["d.kirichenko@fun-box.ru"]
|
11
|
+
spec.description = %q{Service Factory}
|
12
|
+
spec.summary = %q{Service Factory}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "rails"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "service_factory"
|
3
|
+
|
4
|
+
describe ServiceFactory do
|
5
|
+
before do
|
6
|
+
ServiceFactory.register do |i|
|
7
|
+
sample_var String
|
8
|
+
sample_block { Object.new }
|
9
|
+
sample_string { |s| String.new(s) }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "when class given" do
|
14
|
+
it "instantiates it every time" do
|
15
|
+
ServiceFactory.sample_var.object_id.should_not == ServiceFactory.sample_var.object_id
|
16
|
+
end
|
17
|
+
it "instantiates it without args" do
|
18
|
+
ServiceFactory.sample_var.should be_instance_of(String)
|
19
|
+
end
|
20
|
+
it "instantiates it with args" do
|
21
|
+
ServiceFactory.sample_var("sample").should == "sample"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "when block given" do
|
26
|
+
it "executes block without args" do
|
27
|
+
ServiceFactory.sample_block.should be_instance_of(Object)
|
28
|
+
end
|
29
|
+
it "executes block with args" do
|
30
|
+
ServiceFactory.sample_string("sample").should == "sample"
|
31
|
+
end
|
32
|
+
it "executes block every time" do
|
33
|
+
ServiceFactory.sample_block.should_not == ServiceFactory.sample_block
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
it "uses current environment data" do
|
38
|
+
ServiceFactory.register do |i|
|
39
|
+
env :development, :test do
|
40
|
+
sample_var { "test2" }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
ServiceFactory.sample_var.should == "test2"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "ignores data from another environment" do
|
47
|
+
ServiceFactory.register do |i|
|
48
|
+
env :production, :development do
|
49
|
+
sampleVar { "test2" }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
ServiceFactory.sample_var.should_not == "test2"
|
53
|
+
end
|
54
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
|
18
|
+
class Rails
|
19
|
+
def self.env
|
20
|
+
"test"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: service_factory
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Denis Kirichenko
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-02-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
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: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
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: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
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: rspec
|
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: Service Factory
|
79
|
+
email:
|
80
|
+
- d.kirichenko@fun-box.ru
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- lib/service_factory.rb
|
92
|
+
- lib/service_factory/version.rb
|
93
|
+
- service_factory.gemspec
|
94
|
+
- spec/service_factory_spec.rb
|
95
|
+
- spec/spec_helper.rb
|
96
|
+
homepage: ''
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.8.23
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: Service Factory
|
121
|
+
test_files:
|
122
|
+
- spec/service_factory_spec.rb
|
123
|
+
- spec/spec_helper.rb
|
124
|
+
has_rdoc:
|