stub_factory 0.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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .bundle
2
+ coverage
3
+ rdoc
4
+ pkg
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ruby-1.9.2-p0@stub_factory
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source :gemcutter
2
+
3
+ group :development, :test do
4
+ gem 'rake'
5
+ gem 'jeweler', '1.4.0'
6
+ gem 'reek'
7
+ gem 'ruby-debug19', '0.11.6'
8
+ gem 'rspec', '2.0.0.beta.20'
9
+ gem 'autotest', '4.3.2'
10
+ gem 'autotest-fsevent', '0.2.2'
11
+ end
12
+
data/Gemfile.lock ADDED
@@ -0,0 +1,63 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ archive-tar-minitar (0.5.2)
5
+ autotest (4.3.2)
6
+ autotest-fsevent (0.2.2)
7
+ autotest (>= 4.2.4)
8
+ sys-uname
9
+ columnize (0.3.1)
10
+ diff-lcs (1.1.2)
11
+ gemcutter (0.6.1)
12
+ git (1.2.5)
13
+ jeweler (1.4.0)
14
+ gemcutter (>= 0.1.0)
15
+ git (>= 1.2.5)
16
+ rubyforge (>= 2.0.0)
17
+ json_pure (1.4.6)
18
+ linecache19 (0.5.11)
19
+ ruby_core_source (>= 0.1.4)
20
+ rake (0.8.7)
21
+ reek (1.2.8)
22
+ ruby2ruby (~> 1.2)
23
+ ruby_parser (~> 2.0)
24
+ sexp_processor (~> 3.0)
25
+ rspec (2.0.0.beta.20)
26
+ rspec-core (= 2.0.0.beta.20)
27
+ rspec-expectations (= 2.0.0.beta.20)
28
+ rspec-mocks (= 2.0.0.beta.20)
29
+ rspec-core (2.0.0.beta.20)
30
+ rspec-expectations (2.0.0.beta.20)
31
+ diff-lcs (>= 1.1.2)
32
+ rspec-mocks (2.0.0.beta.20)
33
+ ruby-debug-base19 (0.11.24)
34
+ columnize (>= 0.3.1)
35
+ linecache19 (>= 0.5.11)
36
+ ruby_core_source (>= 0.1.4)
37
+ ruby-debug19 (0.11.6)
38
+ columnize (>= 0.3.1)
39
+ linecache19 (>= 0.5.11)
40
+ ruby-debug-base19 (>= 0.11.19)
41
+ ruby2ruby (1.2.4)
42
+ ruby_parser (~> 2.0)
43
+ sexp_processor (~> 3.0)
44
+ ruby_core_source (0.1.4)
45
+ archive-tar-minitar (>= 0.5.2)
46
+ ruby_parser (2.0.4)
47
+ sexp_processor (~> 3.0)
48
+ rubyforge (2.0.4)
49
+ json_pure (>= 1.1.7)
50
+ sexp_processor (3.0.4)
51
+ sys-uname (0.8.4)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ autotest (= 4.3.2)
58
+ autotest-fsevent (= 0.2.2)
59
+ jeweler (= 1.4.0)
60
+ rake
61
+ reek
62
+ rspec (= 2.0.0.beta.20)
63
+ ruby-debug19 (= 0.11.6)
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 James Conroy-Finn
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # stub_factory
2
+
3
+ This is just something I was working on for fun a while back and decided I'd
4
+ push it to Github.
5
+
6
+ Rspec provides `subject` which will do most of what this code does. You
7
+ probably want to use that instead.
8
+
9
+ In your spec_helper
10
+
11
+ require 'stub_factory'
12
+ # Optionally include StubFactory
13
+
14
+ In your specs
15
+
16
+ factory(NextBigThing::Ninja::Foo)
17
+
18
+ describe 'awesomeness' do
19
+ it 'should be easy' do
20
+ foo(:stub => 'this').should be_a?(NextBigThing::Ninja::Foo)
21
+ end
22
+ end
23
+
24
+ You can also specify the method name you want to use to access your instance
25
+
26
+ factory(NextBigThing::Ninja::Foo, :method_name => :ninja_foo)
27
+
28
+ describe 'awesomeness' do
29
+ it 'should be easy' do
30
+ # Then you can access your instance using your preferred method name
31
+ ninja_foo(:stub => 'this').should be_a?(NextBigThing::Ninja::Foo)
32
+ end
33
+ end
34
+
35
+ Check out
36
+ [stub_factory_spec.rb](http://github.com/jcf/stub_factory/blob/master/spec/stub_factory_spec.rb)
37
+ to see `StubFactory` in use.
38
+
39
+ ## Note on Patches/Pull Requests
40
+
41
+ * Fork the project.
42
+ * Make your feature addition or bug fix.
43
+ * Add tests for it. This is important so I don't break it in a
44
+ future version unintentionally.
45
+ * Commit, do not mess with rakefile, version, or history.
46
+ (if you want to have your own version, that is fine but bump version in a
47
+ commit by itself I can ignore when I pull)
48
+ * Send me a pull request. Bonus points for topic branches.
49
+
50
+ ## Copyright
51
+
52
+ Copyright (c) 2010 James Conroy-Finn. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,41 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = 'stub_factory'
8
+ gem.summary = %Q{Simple stubbed factories for Rspec}
9
+ gem.description = %Q{Simple stubbed factories for Rspec}
10
+ gem.email = 'james@logi.cl'
11
+ gem.homepage = 'http://github.com/jcf/stub_factory'
12
+ gem.authors = ['James Conroy-Finn']
13
+ gem.add_development_dependency 'rspec', '>= 1.2.9'
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts 'Jeweler (or a dependency) not available. Install it with: gem install jeweler'
18
+ end
19
+
20
+ begin
21
+ require 'reek/rake/task'
22
+ Reek::Rake::Task.new do |t|
23
+ t.fail_on_error = false
24
+ t.verbose = true
25
+ t.source_files = 'lib/**/*.rb'
26
+ end
27
+ rescue LoadError
28
+ task :reek do
29
+ abort 'Reek is not available. In order to run reek, you must: sudo gem install reek'
30
+ end
31
+ end
32
+
33
+ require 'rake/rdoctask'
34
+ Rake::RDocTask.new do |rdoc|
35
+ version = File.exist?('VERSION') ? File.read('VERSION') : ''
36
+
37
+ rdoc.rdoc_dir = 'rdoc'
38
+ rdoc.title = "stub_factory #{version}"
39
+ rdoc.rdoc_files.include('README*')
40
+ rdoc.rdoc_files.include('lib/**/*.rb')
41
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,3 @@
1
+ major: 0
2
+ minor: 0
3
+ patch: 1
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { 'rspec2' }
@@ -0,0 +1,82 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module StubFactory
4
+ module StringExtensions
5
+ def underscore
6
+ gsub(/::/, '/').
7
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
8
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
9
+ tr('-', '_').
10
+ downcase
11
+ end
12
+ end
13
+
14
+ module ArrayExtensions
15
+ def extract_options!
16
+ last.is_a?(Hash) ? pop : {}
17
+ end
18
+ end
19
+
20
+ # InstanceFactory does all the work. You can access it directly but you
21
+ # should probably just include StubFactory and use the factory helper method.
22
+ #
23
+ # BTW, It's called InstanceFactory and not just Factory so it can be included
24
+ # when you're using FactoryGirl
25
+ class InstanceFactory
26
+ # We don't want to extend all strings in case you've got your own
27
+ # underscore method.
28
+ def initialize(receiver, klass, options = {})
29
+ @receiver = receiver
30
+ @klass = klass
31
+ @method_name = options[:method_name]
32
+ end
33
+
34
+ def with(args)
35
+ @args = args
36
+ self
37
+ end
38
+
39
+ def stub!(stubs = {})
40
+ @stubs = stubs
41
+ self
42
+ end
43
+
44
+ def result
45
+ retrieve_instance || publish_instance
46
+ end
47
+
48
+ def method_name
49
+ @method_name || klass_name
50
+ end
51
+
52
+ private
53
+
54
+ def klass_name
55
+ @klass.to_s.extend(StringExtensions).underscore.split('/').last
56
+ end
57
+
58
+ def instance
59
+ @klass.new(*@args).tap { |_instance| _instance.stub!(@stubs) }
60
+ end
61
+
62
+ def instance_name
63
+ "@__stub_factory_#{method_name}"
64
+ end
65
+
66
+ def retrieve_instance
67
+ @receiver.instance_variable_get(instance_name)
68
+ end
69
+
70
+ def publish_instance
71
+ @receiver.instance_variable_set(instance_name, instance)
72
+ end
73
+ end
74
+
75
+ def factory(klass, options = {})
76
+ _factory = InstanceFactory.new(self, klass, options)
77
+ define_method(_factory.method_name) do |*args|
78
+ stubs = args.extend(ArrayExtensions).extract_options!
79
+ _factory.with(args).stub!(stubs).result
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'stub_factory'
5
+ require 'rspec'
6
+
7
+ include StubFactory
8
+
9
+ Rspec.configure do |config|
10
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe StubFactory do
6
+ describe 'String' do
7
+ factory(String)
8
+
9
+ it 'should define a String instance' do
10
+ string('this is a string')[0..3].should == 'this'
11
+ end
12
+ end
13
+
14
+ describe 'Array overriding method name' do
15
+ factory(Array, :method_name => 'collection')
16
+
17
+ it 'should define an Array instance' do
18
+ collection([1, 2, 3]).length.should == 3
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{stub_factory}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["James Conroy-Finn"]
12
+ s.date = %q{2010-08-31}
13
+ s.description = %q{Simple stubbed factories for Rspec}
14
+ s.email = %q{james@logi.cl}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ ".rspec",
23
+ ".rvmrc",
24
+ "Gemfile",
25
+ "Gemfile.lock",
26
+ "LICENSE",
27
+ "README.md",
28
+ "Rakefile",
29
+ "VERSION.yml",
30
+ "autotest/discover.rb",
31
+ "lib/stub_factory.rb",
32
+ "spec/spec_helper.rb",
33
+ "spec/stub_factory_spec.rb",
34
+ "stub_factory.gemspec"
35
+ ]
36
+ s.homepage = %q{http://github.com/jcf/stub_factory}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.7}
40
+ s.summary = %q{Simple stubbed factories for Rspec}
41
+ s.test_files = [
42
+ "spec/spec_helper.rb",
43
+ "spec/stub_factory_spec.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
52
+ else
53
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
57
+ end
58
+ end
59
+
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stub_factory
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - James Conroy-Finn
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-31 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 2
31
+ - 9
32
+ version: 1.2.9
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Simple stubbed factories for Rspec
36
+ email: james@logi.cl
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.md
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - .rspec
48
+ - .rvmrc
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE
52
+ - README.md
53
+ - Rakefile
54
+ - VERSION.yml
55
+ - autotest/discover.rb
56
+ - lib/stub_factory.rb
57
+ - spec/spec_helper.rb
58
+ - spec/stub_factory_spec.rb
59
+ - stub_factory.gemspec
60
+ has_rdoc: true
61
+ homepage: http://github.com/jcf/stub_factory
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --charset=UTF-8
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.3.7
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Simple stubbed factories for Rspec
92
+ test_files:
93
+ - spec/spec_helper.rb
94
+ - spec/stub_factory_spec.rb