forge-factories 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.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +5 -0
- data/forge.gemspec +21 -0
- data/lib/forge/version.rb +3 -0
- data/lib/forge.rb +71 -0
- data/spec/forge_spec.rb +48 -0
- metadata +93 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Matte Noble
|
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,60 @@
|
|
1
|
+
# Forge
|
2
|
+
|
3
|
+
A Factory library that's about as barebones as you can get.
|
4
|
+
|
5
|
+
## Define Factories
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
Forge.define(:user, User) do |u|
|
9
|
+
u.name = "Spike Spiegel"
|
10
|
+
u.location = "Mars"
|
11
|
+
end
|
12
|
+
```
|
13
|
+
|
14
|
+
## Defining Factories That Use Other Factories
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
Forge.define(:ship, Ship) do |s|
|
18
|
+
s.name = "Bebop"
|
19
|
+
end
|
20
|
+
|
21
|
+
Forge.define(:user, User) do |u|
|
22
|
+
u.name = "Spike Spiegel"
|
23
|
+
u.ship = Forge.build(:ship)
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
## Building Objects
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
Forge.build(:user)
|
31
|
+
```
|
32
|
+
|
33
|
+
## Building Objects and Overriding Attributes
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
Forge.build(:user, name: "Jet")
|
37
|
+
```
|
38
|
+
|
39
|
+
## DSL
|
40
|
+
|
41
|
+
You can drop the `Forge` part of the methods if you include
|
42
|
+
`Forge::DSL`.
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
RSpec.configure do |config|
|
46
|
+
config.include Forge::DSL
|
47
|
+
end
|
48
|
+
|
49
|
+
it "..." do
|
50
|
+
build(:user).should be_awesome
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
## Errors
|
55
|
+
|
56
|
+
`Forge::DuplicateFactoryError`
|
57
|
+
Raised when you try to define two factories using the same name.
|
58
|
+
|
59
|
+
`Forge::MissingFactoryError`
|
60
|
+
Raised when you try to build a factory that has not been defined.
|
data/Rakefile
ADDED
data/forge.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'forge/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "forge-factories"
|
8
|
+
gem.version = Forge::VERSION
|
9
|
+
gem.authors = ["Matte Noble"]
|
10
|
+
gem.email = ["me@mattenoble.com"]
|
11
|
+
gem.description = %q{Barebones factories}
|
12
|
+
gem.summary = %q{Barebones factories}
|
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.require_paths = ["lib"]
|
18
|
+
|
19
|
+
gem.add_development_dependency "rake"
|
20
|
+
gem.add_development_dependency "rspec"
|
21
|
+
end
|
data/lib/forge.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require "forge/version"
|
2
|
+
|
3
|
+
module Forge
|
4
|
+
class ForgeError < StandardError
|
5
|
+
end
|
6
|
+
|
7
|
+
class DuplicateFactoryError < ForgeError
|
8
|
+
end
|
9
|
+
|
10
|
+
class MissingFactoryError < ForgeError
|
11
|
+
end
|
12
|
+
|
13
|
+
class << self
|
14
|
+
attr_accessor :registry
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.reset!
|
18
|
+
@registry = {}
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.define(name, klass, &block)
|
22
|
+
@registry ||= {}
|
23
|
+
raise DuplicateFactoryError if @registry[name]
|
24
|
+
@registry[name] = Definition.new(klass, &block)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.build(name, attrs={})
|
28
|
+
raise MissingFactoryError unless @registry[name]
|
29
|
+
@registry[name].build(attrs)
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.create(name, attrs={})
|
33
|
+
raise MissingFactoryError unless @registry[name]
|
34
|
+
@registry[name].create(attrs)
|
35
|
+
end
|
36
|
+
|
37
|
+
class Definition
|
38
|
+
attr_accessor :klass, :block
|
39
|
+
|
40
|
+
def initialize(klass, &block)
|
41
|
+
@klass = klass
|
42
|
+
@block = block
|
43
|
+
end
|
44
|
+
|
45
|
+
def build(attrs={})
|
46
|
+
instance = @klass.new.tap { |o| @block.call(o) }
|
47
|
+
attrs.each { |k,v| instance.public_send(:"#{k}=", v) }
|
48
|
+
instance
|
49
|
+
end
|
50
|
+
|
51
|
+
def create(attrs={})
|
52
|
+
build(attrs).tap { |o| o.save }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
module DSL
|
57
|
+
def define(*args, &block)
|
58
|
+
Forge.define(*args, &block)
|
59
|
+
end
|
60
|
+
|
61
|
+
def build(*args)
|
62
|
+
Forge.build(*args)
|
63
|
+
end
|
64
|
+
|
65
|
+
alias_method :forge, :build
|
66
|
+
|
67
|
+
def create(*args)
|
68
|
+
Forge.create(*args)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/spec/forge_spec.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
require_relative "../lib/forge"
|
3
|
+
|
4
|
+
describe Forge do
|
5
|
+
class User < OpenStruct; end
|
6
|
+
|
7
|
+
before do
|
8
|
+
Forge.reset!
|
9
|
+
Forge.define(:user, User) do |u|
|
10
|
+
u.name = "Spike"
|
11
|
+
u.location = "Mars"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it "saves factory definitions to a registry" do
|
16
|
+
Forge.registry[:user].should_not be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "raises an error when defining the same factory twice" do
|
20
|
+
expect { Forge.define(:user, User) }.to raise_error(Forge::DuplicateFactoryError)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "builds a forged object of the type specified when defining it" do
|
24
|
+
Forge.build(:user).should be_a User
|
25
|
+
end
|
26
|
+
|
27
|
+
it "sets attributes on the forged objects based on the block passed during definition" do
|
28
|
+
Forge.build(:user).name.should == "Spike"
|
29
|
+
end
|
30
|
+
|
31
|
+
it "allows attributes to be overwritten via build" do
|
32
|
+
Forge.build(:user, name: "Jet").name.should == "Jet"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "raises an error when trying to build a non-existent factory" do
|
36
|
+
expect { Forge.build(:not_a_thing) }.to raise_error(Forge::MissingFactoryError)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "creates factories with an attribute that is another factory to be built" do
|
40
|
+
Forge.define(:bounty_hunter, User) do |f|
|
41
|
+
f.name = "Spike"
|
42
|
+
f.location = "Mars"
|
43
|
+
f.partner = Forge.build(:user, name: "Jet")
|
44
|
+
end
|
45
|
+
|
46
|
+
Forge.build(:bounty_hunter).partner.name.should == "Jet"
|
47
|
+
end
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: forge-factories
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matte Noble
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-11 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: rspec
|
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
|
+
description: Barebones factories
|
47
|
+
email:
|
48
|
+
- me@mattenoble.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- forge.gemspec
|
59
|
+
- lib/forge.rb
|
60
|
+
- lib/forge/version.rb
|
61
|
+
- spec/forge_spec.rb
|
62
|
+
homepage:
|
63
|
+
licenses: []
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
hash: -2953300044199411992
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
hash: -2953300044199411992
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.24
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: Barebones factories
|
92
|
+
test_files:
|
93
|
+
- spec/forge_spec.rb
|