rom_factory 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +112 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rom_factory/builder.rb +57 -0
- data/lib/rom_factory/config.rb +6 -0
- data/lib/rom_factory/version.rb +3 -0
- data/lib/rom_factory.rb +13 -0
- data/rom_factory.gemspec +37 -0
- metadata +158 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 89b9204be8676e712c9615526bcd5dd26871ca0f
|
4
|
+
data.tar.gz: 924ac3c5c7317aa847c067b8923ae464a7750df3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8c3d100354782edd3dba4718f8dbf7ec2b7ba9425d10acfc74cc726b50fa72591b9d937a88f92fd8aa323acd4adf3675ffe7aa8dd608d354efd74250c85ddc27
|
7
|
+
data.tar.gz: f7916356eb92878a012951aaad9ce87b35e95ba4c0347abf5bfeea28f03ba7de1391efaf3fd44cd13d5acde8de24dd7fe44c32e20cdbff1d7effa61a835f686b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at janjiss@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Janis Miezitis
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# RomFactory
|
2
|
+
|
3
|
+
FactoryGirl is an awesome and ROM is awesome, hence RomFactory is born.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rom_factory'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rom_factory
|
20
|
+
|
21
|
+
## Configuration
|
22
|
+
First, you have to define ROM container:
|
23
|
+
```ruby
|
24
|
+
container = ROM.container(:sql, 'sqlite::memory') do |conf|
|
25
|
+
conf.default.create_table(:users) do
|
26
|
+
primary_key :id
|
27
|
+
column :last_name, String, null: false
|
28
|
+
column :first_name, String, null: false
|
29
|
+
column :email, String, null: false
|
30
|
+
column :created_at, Time, null: false
|
31
|
+
column :updated_at, Time, null: false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
```
|
35
|
+
Once that is done, you will have to specify which container RomFactory will use:
|
36
|
+
```ruby
|
37
|
+
RomFactory::Config.configure do |config|
|
38
|
+
config.container = container
|
39
|
+
end
|
40
|
+
```
|
41
|
+
This library works in conjunction with "rom-repository" so you will need to define repository with create command:
|
42
|
+
```ruby
|
43
|
+
UsersRepo < ROM::Repository[:users] do
|
44
|
+
commands :create
|
45
|
+
end
|
46
|
+
```
|
47
|
+
## Simple use case
|
48
|
+
|
49
|
+
After configuration is done, you can define the factory as follows:
|
50
|
+
```ruby
|
51
|
+
RomFactory::Builder.define do |b|
|
52
|
+
b.factory(repo: UsersRepo, name: :user) do |f|
|
53
|
+
f.first_name "Janis"
|
54
|
+
f.last_name "Miezitis"
|
55
|
+
f.email "janjiss@gmail.com"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
When everything is configured, you can use it in your tests as follows:
|
60
|
+
```ruby
|
61
|
+
user = RomFactory::Builder.create(:user)
|
62
|
+
user.email #=> "janjiss@gmail.com"
|
63
|
+
```
|
64
|
+
|
65
|
+
### Mapping of records
|
66
|
+
To follow ROM conventions, you can pass any object that responds to `.call` method with `as:` key in following way:
|
67
|
+
```ruby
|
68
|
+
class User
|
69
|
+
def self.call(attrs)
|
70
|
+
self.new(attrs)
|
71
|
+
end
|
72
|
+
|
73
|
+
attr_reader :first_name, :last_name, :email
|
74
|
+
|
75
|
+
def initialize(first_name:, last_name:, email:)
|
76
|
+
@first_name, @last_name, @email = first_name, last_name, email
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
RomFactory::Builder.define do |b|
|
81
|
+
b.factory(repo: UsersRepo, name: :user, as: User) do |f|
|
82
|
+
f.first_name "Janis"
|
83
|
+
f.last_name "Miezitis"
|
84
|
+
f.email "janjiss@gmail.com"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
user = RomFactory::Builder.create(:user)
|
89
|
+
user.class #=> User
|
90
|
+
```
|
91
|
+
I would recommend using `dry-types` library or similar for creating your domain objects.
|
92
|
+
|
93
|
+
### Callable properties
|
94
|
+
You can easily define dynamic (callbale) properties if value needs to change every time it needs to be called. Anything that responds to `.call` can be dynamic property.
|
95
|
+
```ruby
|
96
|
+
RomFactory::Builder.define do |b|
|
97
|
+
b.factory(repo: UsersRepo, name: :user) do |f|
|
98
|
+
f.first_name "Janis"
|
99
|
+
f.last_name "Miezitis"
|
100
|
+
f.email "janjiss@gmail.com"
|
101
|
+
f.created_at {Time.now}
|
102
|
+
end
|
103
|
+
end
|
104
|
+
user = RomFactory::Builder.create(:user)
|
105
|
+
user.created_at #=> 2016-08-27 18:17:08 -0500
|
106
|
+
```
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
## License
|
111
|
+
|
112
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rom_factory"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
module RomFactory
|
2
|
+
class Builder
|
3
|
+
@container = Dry::Container.new
|
4
|
+
|
5
|
+
def self.container
|
6
|
+
@container
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.define(&block)
|
10
|
+
factory = new(&block)
|
11
|
+
raise ArgumentError, "Factory #{factory._name} already present" if container.key?(factory._name)
|
12
|
+
container.register(factory._name, factory)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.create(name, attrs = {})
|
16
|
+
factory = container.resolve(name)
|
17
|
+
schema = factory._schema.merge(attrs)
|
18
|
+
schema = schema.map do |k,v|
|
19
|
+
if v.respond_to?(:call)
|
20
|
+
[k, v.call]
|
21
|
+
else
|
22
|
+
[k, v]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
record = factory._repo.create(schema.to_h)
|
26
|
+
factory._as ? factory._as.call(record.to_h) : record
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize
|
30
|
+
yield(self)
|
31
|
+
end
|
32
|
+
|
33
|
+
def factory(name:, repo:, as: nil, &block)
|
34
|
+
@_repo = repo.new(RomFactory::Config.config.container)
|
35
|
+
@_name = name
|
36
|
+
@_as = as
|
37
|
+
@_schema = {}
|
38
|
+
yield(self)
|
39
|
+
end
|
40
|
+
|
41
|
+
attr_reader :_repo, :_name, :_as, :_schema
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def method_missing(name, *args, &block)
|
46
|
+
if @_repo.root.relation.attributes.include?(name)
|
47
|
+
if block_given?
|
48
|
+
@_schema[name] = block
|
49
|
+
else
|
50
|
+
@_schema[name] = args.first
|
51
|
+
end
|
52
|
+
else
|
53
|
+
raise NoMethodError, "undefined method `#{name}' for #{self}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/rom_factory.rb
ADDED
data/rom_factory.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rom_factory/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rom_factory"
|
8
|
+
spec.version = RomFactory::VERSION
|
9
|
+
spec.authors = ["Janis Miezitis"]
|
10
|
+
spec.email = ["janjiss@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{ROM based Factory girl inspired builder library to make your specs awesome}
|
13
|
+
spec.description = %q{}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "dry-configurable", "~> 0.1.7"
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
34
|
+
spec.add_development_dependency "rom", "~> 2.0"
|
35
|
+
spec.add_development_dependency "rom-repository", "~> 0.3.1"
|
36
|
+
spec.add_development_dependency "pry"
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rom_factory
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Janis Miezitis
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-configurable
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.7
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.1.7
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rom
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rom-repository
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.3.1
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.3.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: ''
|
112
|
+
email:
|
113
|
+
- janjiss@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".travis.yml"
|
121
|
+
- CODE_OF_CONDUCT.md
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- bin/console
|
127
|
+
- bin/setup
|
128
|
+
- lib/rom_factory.rb
|
129
|
+
- lib/rom_factory/builder.rb
|
130
|
+
- lib/rom_factory/config.rb
|
131
|
+
- lib/rom_factory/version.rb
|
132
|
+
- rom_factory.gemspec
|
133
|
+
homepage: ''
|
134
|
+
licenses:
|
135
|
+
- MIT
|
136
|
+
metadata:
|
137
|
+
allowed_push_host: https://rubygems.org
|
138
|
+
post_install_message:
|
139
|
+
rdoc_options: []
|
140
|
+
require_paths:
|
141
|
+
- lib
|
142
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
requirements: []
|
153
|
+
rubyforge_project:
|
154
|
+
rubygems_version: 2.5.1
|
155
|
+
signing_key:
|
156
|
+
specification_version: 4
|
157
|
+
summary: ROM based Factory girl inspired builder library to make your specs awesome
|
158
|
+
test_files: []
|