traver 0.2.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 +10 -0
- data/.travis.yml +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +138 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/traver/factory_definer.rb +47 -0
- data/lib/traver/object_creator.rb +85 -0
- data/lib/traver/version.rb +3 -0
- data/lib/traver.rb +19 -0
- data/traver.gemspec +27 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 338d95191adfb1ad3addd5e0d12331e772186643
|
4
|
+
data.tar.gz: f22f8a30c993626c0985c4f3bc5e8685caedb6e4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c8855182f196e730cd4571c4f32a79d43c95e8d87b9ef79b90d9f827db362c48ae788b67a00083d3e3f3050fe73c0aaf8c29c4704550212bbc51b3e07a6b2524
|
7
|
+
data.tar.gz: 724bcaeb91174f485b5c4d6bbb36818e227c790959b357def7d56ae55edc4d40bd3224c8806f0a5a3e09f2c49f9ce51556e88d0f512b9cec11fa994b141a98e6
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Yuri Kaspervich
|
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,138 @@
|
|
1
|
+
# Traver [](https://travis-ci.org/yukas/traver)
|
2
|
+
|
3
|
+
Traver is a test data generation framework.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```shell
|
8
|
+
gem install traver
|
9
|
+
```
|
10
|
+
|
11
|
+
or add the following line to Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'traver'
|
15
|
+
```
|
16
|
+
|
17
|
+
and run `bundle install` from your shell.
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Create an object with attributes:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
blog = Traver.create(blog: { title: "Blog" }) #=> #<Blog @title="Blog">
|
25
|
+
```
|
26
|
+
|
27
|
+
Define and use factories:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
Traver.factory(:post, {
|
31
|
+
title: "Hello"
|
32
|
+
})
|
33
|
+
|
34
|
+
Traver.factory(:published_post, :post, {
|
35
|
+
published: true
|
36
|
+
})
|
37
|
+
|
38
|
+
Traver.factory(:draft_post, :post, {
|
39
|
+
published: false
|
40
|
+
})
|
41
|
+
|
42
|
+
Traver.create(:published_post) #=> #<Post @title="Hello", @published=true>
|
43
|
+
Traver.create(:draft_post) #=> #<Post @title="Hello", @published=false>
|
44
|
+
```
|
45
|
+
|
46
|
+
Create associated object:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
blog = Traver.create(blog: {
|
50
|
+
title: "Hello",
|
51
|
+
user: { name: "Mike" }
|
52
|
+
})
|
53
|
+
|
54
|
+
blog.user #=> #<User @name="Mike">
|
55
|
+
```
|
56
|
+
|
57
|
+
Create object collections:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
blog = Traver.create(blog: {
|
61
|
+
title: "Hello",
|
62
|
+
posts: [
|
63
|
+
{ title: "Post #1" },
|
64
|
+
{ title: "Post #2" }
|
65
|
+
]
|
66
|
+
})
|
67
|
+
|
68
|
+
blog.posts #=> [#<Post @title="Post #1">, #<Post @title="Post #2">]
|
69
|
+
|
70
|
+
# More concise syntax
|
71
|
+
blog = Traver.create(blog: {
|
72
|
+
title: "Hello",
|
73
|
+
posts: [2, title: "Post #${n}"]
|
74
|
+
})
|
75
|
+
|
76
|
+
blog.posts #=> [#<Post @title="Post #1">, #<Post @title="Post #2">]
|
77
|
+
|
78
|
+
# Having defined factory
|
79
|
+
Traver.factory(post: { title: "Post #${n}"})
|
80
|
+
|
81
|
+
# Even more concise
|
82
|
+
blog = Traver.create(blog: { title: "Hello", posts: 2 })
|
83
|
+
|
84
|
+
blog.posts #=> [#<Post @title="Post #1">, #<Post @title="Post #2">]
|
85
|
+
|
86
|
+
```
|
87
|
+
|
88
|
+
Any level of nesting:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
blog = Traver.create(blog: {
|
92
|
+
title: "Blog",
|
93
|
+
posts: [{
|
94
|
+
title: "Hello",
|
95
|
+
tags: [{ name: "Happy" }]
|
96
|
+
}]
|
97
|
+
})
|
98
|
+
|
99
|
+
blog.posts.first.tag.first #=> #<Tag @name="Happy">
|
100
|
+
```
|
101
|
+
|
102
|
+
Create lists:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
users = Traver.create_list(:user, 2, email: "user${n}@mail.me")
|
106
|
+
#=> [#<User @email="user1@mail.me">, #<User @email="user2@mail.me">]
|
107
|
+
```
|
108
|
+
|
109
|
+
Graph is a convenient way to reference created objects:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
graph = Traver.create_graph(blog: { posts: [{ tags: 2 }] })
|
113
|
+
|
114
|
+
graph.blog #=> #<Blog>
|
115
|
+
graph.posts #=> [#<Post>]
|
116
|
+
graph.post1 #=> #<Post>
|
117
|
+
graph.tags #=> [#<Tag>, #<Tag>]
|
118
|
+
graph.tag1 #=> #<Tag>
|
119
|
+
graph.tag2 #=> #<Tag>
|
120
|
+
|
121
|
+
blog, post = Traver.create_graph(blog: { posts: 1 })[:blog, :post]
|
122
|
+
```
|
123
|
+
|
124
|
+
## Development
|
125
|
+
|
126
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
127
|
+
|
128
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
129
|
+
|
130
|
+
## Contributing
|
131
|
+
|
132
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yukas/traver.
|
133
|
+
|
134
|
+
|
135
|
+
## License
|
136
|
+
|
137
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
138
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "traver"
|
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,47 @@
|
|
1
|
+
module Traver
|
2
|
+
class FactoryDefiner
|
3
|
+
attr_reader :defined_factories
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@defined_factories = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.instance
|
10
|
+
@instance ||= FactoryDefiner.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def define_factory(factory_name, *options)
|
14
|
+
parent_name = nil
|
15
|
+
|
16
|
+
if options.size == 1
|
17
|
+
params = options.first
|
18
|
+
elsif options.size == 2
|
19
|
+
parent_name, params = options
|
20
|
+
end
|
21
|
+
|
22
|
+
defined_factories[factory_name] = { params: params, parent: parent_name }
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_object_class(factory_name)
|
26
|
+
if defined_factories[factory_name] && parent_name = defined_factories[factory_name][:parent]
|
27
|
+
get_object_class(parent_name)
|
28
|
+
else
|
29
|
+
Object.const_get(factory_name.to_s.capitalize)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def apply_factory_params(factory_name, params)
|
34
|
+
factory_params = get_factory_params(factory_name)
|
35
|
+
|
36
|
+
factory_params.merge(params)
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def get_factory_params(factory_name)
|
42
|
+
if factory = defined_factories[factory_name]
|
43
|
+
get_factory_params(factory[:parent]).merge(factory[:params])
|
44
|
+
end || {}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module Traver
|
2
|
+
class ObjectCreator
|
3
|
+
attr_reader :factory_name, :params
|
4
|
+
attr_reader :created_object
|
5
|
+
|
6
|
+
def initialize(options)
|
7
|
+
options = { options => {} } if options.is_a?(Symbol)
|
8
|
+
|
9
|
+
@factory_name, @params = options.first
|
10
|
+
@factory_definer = FactoryDefiner.instance
|
11
|
+
end
|
12
|
+
|
13
|
+
def create_object
|
14
|
+
instantiate_object
|
15
|
+
set_object_state
|
16
|
+
persist_object
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
attr_reader :factory_definer
|
21
|
+
|
22
|
+
def instantiate_object
|
23
|
+
@created_object = get_class.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_class
|
27
|
+
factory_definer.get_object_class(factory_name)
|
28
|
+
end
|
29
|
+
|
30
|
+
def set_object_state
|
31
|
+
factory_params.each do |attribute, value|
|
32
|
+
if nested_params?(value)
|
33
|
+
create_nested_object(attribute, value)
|
34
|
+
elsif collection_params?(value)
|
35
|
+
create_collection(attribute, value)
|
36
|
+
else
|
37
|
+
set_attribute(attribute, value)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def factory_params
|
43
|
+
factory_definer.apply_factory_params(factory_name, params)
|
44
|
+
end
|
45
|
+
|
46
|
+
def nested_params?(params)
|
47
|
+
params.is_a?(Hash)
|
48
|
+
end
|
49
|
+
|
50
|
+
def create_nested_object(k, v)
|
51
|
+
set_attribute(k, do_create_object(k => v))
|
52
|
+
end
|
53
|
+
|
54
|
+
def do_create_object(params)
|
55
|
+
object_creator = ObjectCreator.new(params)
|
56
|
+
object_creator.create_object
|
57
|
+
|
58
|
+
object_creator.created_object
|
59
|
+
end
|
60
|
+
|
61
|
+
def set_attribute(attribute, value)
|
62
|
+
created_object.public_send("#{attribute}=", value)
|
63
|
+
end
|
64
|
+
|
65
|
+
def collection_params?(params)
|
66
|
+
params.is_a?(Array)
|
67
|
+
end
|
68
|
+
|
69
|
+
def create_collection(attribute, collection_params)
|
70
|
+
collection = collection_params.map do |params|
|
71
|
+
do_create_object(singularize(attribute) => params)
|
72
|
+
end
|
73
|
+
|
74
|
+
set_attribute(attribute, collection)
|
75
|
+
end
|
76
|
+
|
77
|
+
def singularize(val)
|
78
|
+
val.to_s.chomp("s").to_sym
|
79
|
+
end
|
80
|
+
|
81
|
+
def persist_object
|
82
|
+
created_object.save if defined?(Rails)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/lib/traver.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "traver/version"
|
2
|
+
require "traver/factory_definer"
|
3
|
+
require "traver/object_creator"
|
4
|
+
|
5
|
+
module Traver
|
6
|
+
class Error < Exception; end
|
7
|
+
|
8
|
+
def self.factory(factory_name, *options)
|
9
|
+
factory_definer = FactoryDefiner.instance
|
10
|
+
factory_definer.define_factory(factory_name, *options)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.create(options)
|
14
|
+
object_creator = ObjectCreator.new(options)
|
15
|
+
object_creator.create_object
|
16
|
+
|
17
|
+
object_creator.created_object
|
18
|
+
end
|
19
|
+
end
|
data/traver.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'traver/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "traver"
|
8
|
+
spec.version = Traver::VERSION
|
9
|
+
spec.authors = ["Yuri Kaspervich"]
|
10
|
+
spec.email = ["ykas.gg@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Traver is an object creation framework}
|
13
|
+
spec.description = %q{Traver is an object creation framework}
|
14
|
+
spec.homepage = "http://github.com/yukas/traver"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
25
|
+
spec.add_development_dependency "activerecord"
|
26
|
+
spec.add_development_dependency "sqlite3"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: traver
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuri Kaspervich
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activerecord
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Traver is an object creation framework
|
84
|
+
email:
|
85
|
+
- ykas.gg@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- bin/console
|
97
|
+
- bin/setup
|
98
|
+
- lib/traver.rb
|
99
|
+
- lib/traver/factory_definer.rb
|
100
|
+
- lib/traver/object_creator.rb
|
101
|
+
- lib/traver/version.rb
|
102
|
+
- traver.gemspec
|
103
|
+
homepage: http://github.com/yukas/traver
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.5.1
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: Traver is an object creation framework
|
127
|
+
test_files: []
|