fabricas 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +114 -0
- data/fabricas.gemspec +20 -0
- data/lib/fabricas.rb +57 -0
- data/test/test_fabricas.rb +78 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6ea7ee05cb9fd845ff15449a531358f93c168e8e
|
4
|
+
data.tar.gz: 09af594527a108e52ab1ca2ce405122e13b57055
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7a26f3aab3b18b5ca77832d61fe84d67a91cad11162595523d32635a2d8ec37e5c812d54925c97cfa97e57412a4dd2da1b1c549d144ed3a74706ef6d129bf045
|
7
|
+
data.tar.gz: 003ee49dc7f7b7f93ebad20418a8e5fb80aa6d026c1487fafb3e53131c120122ef4d0854b4979514a00645a96b7bcbfe79d0a856377d9c50ac5345d93688d10a
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Julio Lopez
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
Fabricas
|
2
|
+
====
|
3
|
+
|
4
|
+
Minimalist library for build factories.
|
5
|
+
|
6
|
+
## Introduction
|
7
|
+
|
8
|
+
Fabricas is simple, just a few lines of code. Is framework agnostic, there are no monkey-patch to any class or object and is very useful when you need to build objects fast.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Installing Fabricas is as simple as running:
|
13
|
+
|
14
|
+
```
|
15
|
+
$ gem install fabricas
|
16
|
+
```
|
17
|
+
|
18
|
+
Include Fabricas in your Gemfile with gem 'fabricas' or require it with require 'fabricas'.
|
19
|
+
|
20
|
+
Usage
|
21
|
+
-----
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class User
|
25
|
+
attr_accessor :name, :email
|
26
|
+
end
|
27
|
+
|
28
|
+
Fabricas.define do
|
29
|
+
factory :user do
|
30
|
+
name "Julio"
|
31
|
+
email "email@gmail.com"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
user = Fabricas.build :user
|
36
|
+
puts user.inspect
|
37
|
+
#=> <User @name= "Julio", @email= "email@gmail.com">
|
38
|
+
|
39
|
+
other_user = Fabricas.build :user, name: "Pedro"
|
40
|
+
puts other_user.inspect
|
41
|
+
#=> <User @name= "Pedro", @email= "email@gmail.com">
|
42
|
+
```
|
43
|
+
|
44
|
+
### Defining a class name
|
45
|
+
|
46
|
+
You can provide your own class name to your factories using the `class_name` key:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
Fabricas.define do
|
50
|
+
factory :admin, class_name: "User" do
|
51
|
+
name "Big Boss"
|
52
|
+
admin true
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
admin = Fabricas.build :admin
|
57
|
+
puts admin.inspect
|
58
|
+
#=> <User @name= "Big Boss", @admin= true>
|
59
|
+
```
|
60
|
+
|
61
|
+
### Sending blocks as attributes
|
62
|
+
|
63
|
+
Also you can use blocks in your `factory` to send whatever you want.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
Fabricas.define do
|
67
|
+
factory :user do
|
68
|
+
# ...
|
69
|
+
password { User.generate_password }
|
70
|
+
city { Faker::Address.city }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
You can take advantage of them for send other instances.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
class Pet
|
79
|
+
attr_accessor :name, :age
|
80
|
+
end
|
81
|
+
|
82
|
+
Fabricas.define do
|
83
|
+
factory :user do
|
84
|
+
# ...
|
85
|
+
pet { Fabricas.build :pet }
|
86
|
+
end
|
87
|
+
|
88
|
+
factory :pet do
|
89
|
+
name "Firulais"
|
90
|
+
age 12
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
user = Fabricas.build :user
|
95
|
+
puts user.pet.inspect
|
96
|
+
#=> <Pet @name= "Firulais", @age= 12>
|
97
|
+
```
|
98
|
+
|
99
|
+
### Dependent Attributes
|
100
|
+
Attributes can be built using other attributes
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
Fabricas.define do
|
104
|
+
factory :user do
|
105
|
+
name "Julio"
|
106
|
+
url "workingawesome.com"
|
107
|
+
email { "#{name.downcase}@#{url}" }
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
user = Fabricas.build :user
|
112
|
+
puts user.inspect
|
113
|
+
#=> <User @name= "Julio", @url="workingawesome.com" @email= "julio@workingawesome.com">
|
114
|
+
```
|
data/fabricas.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "./lib/fabricas"
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "fabricas"
|
5
|
+
s.version = Fabricas::VERSION
|
6
|
+
s.summary = "Minimalist library for build factories."
|
7
|
+
s.description = "Fabricas is simple, just a few lines of code. Is framework agnostic, there are no monkey-patch to any class or object and is very useful when you need to build objects fast."
|
8
|
+
s.authors = ["Julio Lopez"]
|
9
|
+
s.email = ["ljuliom@gmail.com"]
|
10
|
+
s.homepage = "http://github.com/TheBlasfem/fabricas"
|
11
|
+
s.files = Dir[
|
12
|
+
"LICENSE",
|
13
|
+
"README.md",
|
14
|
+
"lib/**/*.rb",
|
15
|
+
"*.gemspec",
|
16
|
+
"test/**/*.rb"
|
17
|
+
]
|
18
|
+
s.license = "MIT"
|
19
|
+
s.add_development_dependency "cutest", "1.1.3"
|
20
|
+
end
|
data/lib/fabricas.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Copyright (c) 2015 Julio Lopez
|
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 all
|
11
|
+
# 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 THE
|
19
|
+
# SOFTWARE.
|
20
|
+
module Fabricas
|
21
|
+
VERSION = "1.0.0"
|
22
|
+
@factories = {}
|
23
|
+
def self.factories; @factories; end
|
24
|
+
|
25
|
+
def self.define(&block)
|
26
|
+
module_eval(&block)
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.factory(klass, args = {}, &block)
|
30
|
+
if block_given?
|
31
|
+
factories[klass] = CleanRoom.new args.fetch(:class_name, nil)
|
32
|
+
factories[klass].instance_eval(&block)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.build(klass, values = {})
|
37
|
+
_i = const_get((factories[klass].class_name || klass).capitalize).new
|
38
|
+
attributes = factories[klass].attributes.merge(values)
|
39
|
+
attributes.each do |name, value|
|
40
|
+
_i.send("#{name}=", value.is_a?(Proc) ? _i.instance_eval(&value) : value)
|
41
|
+
end
|
42
|
+
_i
|
43
|
+
end
|
44
|
+
|
45
|
+
class CleanRoom < BasicObject
|
46
|
+
attr_reader :attributes, :class_name
|
47
|
+
|
48
|
+
def initialize(class_name)
|
49
|
+
@attributes = {}
|
50
|
+
@class_name = class_name.downcase.to_sym if class_name
|
51
|
+
end
|
52
|
+
|
53
|
+
def method_missing(method_name, *args, &block)
|
54
|
+
attributes[method_name] = args.first || block
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require File.expand_path("../lib/fabricas", File.dirname(__FILE__))
|
2
|
+
|
3
|
+
class User
|
4
|
+
attr_accessor :name, :email, :pet, :admin, :password, :url
|
5
|
+
|
6
|
+
def self.generate_password
|
7
|
+
"5555"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Pet
|
12
|
+
attr_accessor :name, :age, :adopted
|
13
|
+
end
|
14
|
+
|
15
|
+
Fabricas.define do
|
16
|
+
factory :user do
|
17
|
+
name "Julio"
|
18
|
+
url "workingawesome.com"
|
19
|
+
email { "#{name.downcase}@#{url}" }
|
20
|
+
password { User.generate_password }
|
21
|
+
pet { Fabricas.build :pet }
|
22
|
+
end
|
23
|
+
|
24
|
+
factory :admin, class_name: "User" do
|
25
|
+
name "Big Boss"
|
26
|
+
email "boss@gmail.com"
|
27
|
+
admin true
|
28
|
+
end
|
29
|
+
|
30
|
+
factory :pet do
|
31
|
+
name "Calvin"
|
32
|
+
age 1
|
33
|
+
adopted true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
scope do
|
38
|
+
test "build user returns name and email" do
|
39
|
+
user = Fabricas.build :user
|
40
|
+
assert_equal user.name, "Julio"
|
41
|
+
assert_equal user.url, "workingawesome.com"
|
42
|
+
end
|
43
|
+
|
44
|
+
test "build user with name returns the sent name" do
|
45
|
+
user = Fabricas.build :user, name: "Pedro"
|
46
|
+
assert_equal user.name, "Pedro"
|
47
|
+
assert_equal user.url, "workingawesome.com"
|
48
|
+
end
|
49
|
+
|
50
|
+
test "multiple factories" do
|
51
|
+
pet = Fabricas.build :pet
|
52
|
+
assert_equal pet.name, "Calvin"
|
53
|
+
assert_equal pet.age, 1
|
54
|
+
assert_equal pet.adopted, true
|
55
|
+
end
|
56
|
+
|
57
|
+
test "factory object instances by proc" do
|
58
|
+
user = Fabricas.build :user
|
59
|
+
assert_equal user.pet.name, "Calvin"
|
60
|
+
assert_equal user.pet.age, 1
|
61
|
+
end
|
62
|
+
|
63
|
+
test "factory run method in proc" do
|
64
|
+
user = Fabricas.build :user
|
65
|
+
assert_equal user.password, "5555"
|
66
|
+
end
|
67
|
+
|
68
|
+
test "using another class name" do
|
69
|
+
admin = Fabricas.build :admin
|
70
|
+
assert_equal admin.name, "Big Boss"
|
71
|
+
assert_equal admin.admin, true
|
72
|
+
end
|
73
|
+
|
74
|
+
test "dependent attribute" do
|
75
|
+
user = Fabricas.build :user
|
76
|
+
assert_equal user.email, "julio@workingawesome.com"
|
77
|
+
end
|
78
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fabricas
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julio Lopez
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: cutest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.1.3
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.1.3
|
27
|
+
description: Fabricas is simple, just a few lines of code. Is framework agnostic,
|
28
|
+
there are no monkey-patch to any class or object and is very useful when you need
|
29
|
+
to build objects fast.
|
30
|
+
email:
|
31
|
+
- ljuliom@gmail.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- LICENSE
|
37
|
+
- README.md
|
38
|
+
- fabricas.gemspec
|
39
|
+
- lib/fabricas.rb
|
40
|
+
- test/test_fabricas.rb
|
41
|
+
homepage: http://github.com/TheBlasfem/fabricas
|
42
|
+
licenses:
|
43
|
+
- MIT
|
44
|
+
metadata: {}
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 2.2.2
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: Minimalist library for build factories.
|
65
|
+
test_files: []
|