subroutine-factory 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 26e7af94d4befaec49de002e7460572005754d21
4
+ data.tar.gz: b5b1b80adc2b9c37bb82cccf7787775b7fd449fc
5
+ SHA512:
6
+ metadata.gz: 6fe24be9bab1509b6b69d3f196e5ab4b600715972bf3c8cbcb7bcf174fa937976cb32eb1e6366c2f57d24b3e333489e50a568268b6a7997571f878c417209dd7
7
+ data.tar.gz: a0eee2e1db2c9a1e4d044f0e1b1739cfc5152327fa96dbbc766da5c8a90f95ec22905e7b6d9a8b6f12802aaa25c7a7c0c0fa36fb2a4c5a3c69870174fb4ea4a4
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in subroutine-factory.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Mike Nelson
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,84 @@
1
+ # Subroutine::Factory
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'subroutine-factory'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install subroutine-factory
18
+
19
+ ## Usage
20
+
21
+ The Subroutine gem does a great job of encapsulating your app's business logic. To simplify the usage of Subroutine Ops in your test suite, utilize the subroutine-factory gem.
22
+
23
+ Defining factories:
24
+
25
+ ```ruby
26
+ Subroutine::Factory.define :signup do
27
+ op ::SignupOp
28
+
29
+ inputs :email, sequence{|n| "foo{n}@example.com" }
30
+ inputs :password, "password123"
31
+
32
+ # by default, the op will be returned when the factory is used.
33
+ # this `output` returns the value of the accessor on the resulting op
34
+ output :user
35
+ end
36
+
37
+ Subroutine::Factory.define :business_signup do
38
+
39
+ # all configurations are inherited from the "signup" factory
40
+ parent :signup
41
+
42
+ op ::BusinessSignupOp
43
+
44
+ input :business_name, sequence{|n| "Business #{n}" }
45
+
46
+ # you can output multiple things into a hash by using `outputs`
47
+ outputs :user, :business
48
+ end
49
+ ```
50
+
51
+ Using Factories:
52
+
53
+ ```ruby
54
+ user = Subroutine::Factory.create :signup
55
+ user = Subroutine::Factory.create :signup, email: "foo@bar.com"
56
+
57
+ out = Subroutine::Factory.create :business_signup
58
+ out[:user] #=> User{}
59
+ out[:business] #=> Business{}
60
+ ```
61
+
62
+ Spec / Test Helper:
63
+ ```ruby
64
+ #spec_helper.rb, test_helper.rb
65
+ config.include ::Subroutine::Factory::SpecHelper
66
+
67
+ # in your tests
68
+ user = factory(:signup)
69
+ ```
70
+
71
+ ## Development
72
+
73
+ 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.
74
+
75
+ 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).
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mnelson/subroutine-factory.
80
+
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "subroutine/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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,66 @@
1
+ require 'active_support/inflector/methods'
2
+
3
+ module Subroutine
4
+ module Factory
5
+ class Builder
6
+
7
+ def initialize(config, *args)
8
+ @config = config
9
+ @options = config.options
10
+ @args = args
11
+ end
12
+
13
+ def execute!
14
+ op = op_class.new(*input_args)
15
+ Array(@options[:befores]).each{|block| block.call(op) }
16
+ op.submit!
17
+ output = extract_output(op)
18
+ Array(@options[:afters]).each{|block| block.call(op, output) }
19
+ output
20
+ end
21
+
22
+ protected
23
+
24
+ def op_class
25
+ klass = @options[:op].constantize
26
+ end
27
+
28
+ def input_args
29
+ args = @args.dup
30
+ overrides = args.extract_options!
31
+ inputs = build_inputs(overrides)
32
+
33
+ args.push(inputs)
34
+ args
35
+ end
36
+
37
+ def build_inputs(overrides)
38
+ out = {}
39
+ @options[:inputs].each_pair do |k,v|
40
+ if overrides.has_key?(k)
41
+ out[k] = overrides[k]
42
+ else
43
+ out[k] = v.respond_to?(:call) ? v.call : v
44
+ end
45
+ end
46
+
47
+ out
48
+ end
49
+
50
+ def extract_output(op)
51
+ if @options[:output]
52
+ return op.send(@options[:output])
53
+ elsif @options[:outputs]
54
+ if @options[:outputs].length == 1 && @options[:outputs][0].is_a?(Array)
55
+ @options[:outputs][0].map{|output| op.send(output) }
56
+ else
57
+ @options[:outputs].inject({}){|out, output| out[output] = op.send(output); out }
58
+ end
59
+ else
60
+ op
61
+ end
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,83 @@
1
+ require 'active_support/core_ext/hash/deep_merge'
2
+
3
+ module Subroutine
4
+ module Factory
5
+ class Config
6
+
7
+ delegate :sequence, to: "::Subroutine::Factory"
8
+
9
+ attr_reader :options
10
+
11
+ def initialize(options)
12
+ @options = options || {}
13
+
14
+ parent(@options.delete(:parent)) if @options[:parent]
15
+
16
+ sanitize!
17
+ end
18
+
19
+ def parent(parent)
20
+ inherit_from(parent)
21
+ end
22
+
23
+ def validate!
24
+ raise "Missing op configuration" unless @options[:op]
25
+ end
26
+
27
+ def op(op_class)
28
+ @options[:op] = case op_class
29
+ when String
30
+ op_class
31
+ when Class
32
+ op_class.name
33
+ else
34
+ op_class.to_s
35
+ end
36
+ end
37
+
38
+ def before(&block)
39
+ @options[:befores].push(block)
40
+ end
41
+
42
+ def after(&block)
43
+ @options[:afters].push(block)
44
+ end
45
+
46
+ def input(name, value)
47
+ @options[:inputs][name.to_sym] = value
48
+ end
49
+
50
+ def output(name)
51
+ @options.delete(:outputs)
52
+ @options[:output] = name
53
+ end
54
+
55
+ def outputs(*names)
56
+ @options.delete(:output)
57
+ @options[:outputs] = names
58
+ end
59
+
60
+ protected
61
+
62
+ def inherit_from(parent)
63
+ parent = ::Subroutine::Factory.get_config!(parent) unless parent.is_a?(::Subroutine::Factory::Config)
64
+ @options.deep_merge!(parent.options)
65
+ sanitize!
66
+ end
67
+
68
+ def sanitize!
69
+ op(@options[:op]) if @options[:op]
70
+
71
+ @options[:inputs] ||= {}
72
+ @options[:inputs].each_pair(&method(:input))
73
+
74
+ outputs(*@options[:outputs]) if @options[:outputs]
75
+ output(@options[:output]) if @options[:output]
76
+
77
+ @options[:befores] = @options[:befores].dup if @options[:befores]
78
+ @options[:afters] = @options[:afters].dup if @options[:afters]
79
+ end
80
+
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,13 @@
1
+ module Subroutine
2
+ module Factory
3
+ module SpecHelper
4
+ def factory(*args)
5
+ ::Subroutine::Factory.create(*args)
6
+ end
7
+
8
+ def factory!(*args)
9
+ factory(*args)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Subroutine
2
+ module Factory
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,53 @@
1
+ require 'subroutine'
2
+ require "subroutine/factory/version"
3
+ require "subroutine/factory/config"
4
+ require "subroutine/factory/builder"
5
+ require "subroutine/factory/spec_helper"
6
+
7
+ module Subroutine
8
+ module Factory
9
+
10
+ @@configs = {}
11
+ @@sequence = 1
12
+
13
+ def self.configs
14
+ @@configs
15
+ end
16
+
17
+ def self.define(name, options = {}, &block)
18
+ config = ::Subroutine::Factory::Config.new(options)
19
+ @@configs[name.to_sym] = config
20
+ config.instance_eval(&block) if block_given?
21
+ config.validate!
22
+ config
23
+ end
24
+
25
+ def self.get_config(name)
26
+ @@configs[name.to_sym]
27
+ end
28
+
29
+ def self.get_config!(name)
30
+ config = get_config(name)
31
+ raise "Unknown Subroutine::Factory `#{name}`" unless config
32
+ return config
33
+ end
34
+
35
+ def self.create(name, *args)
36
+ config = get_config!(name)
37
+ builder = ::Subroutine::Factory::Builder.new(config, *args)
38
+ builder.execute!
39
+ end
40
+
41
+ def self.sequence(&lambda)
42
+ if block_given?
43
+ Proc.new{
44
+ @@sequence += 1
45
+ lambda.call(@@sequence)
46
+ }
47
+ else
48
+ @@sequence += 1
49
+ end
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'subroutine/factory/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "subroutine-factory"
8
+ spec.version = Subroutine::Factory::VERSION
9
+ spec.authors = ["Mike Nelson"]
10
+ spec.email = ["mike@mnelson.io"]
11
+
12
+ spec.summary = %q{Test factories for ops using Subroutine Ops}
13
+ spec.description = %q{Test factories for ops using Subroutine Ops}
14
+ spec.homepage = "https://github.com/mnelson/subroutine-factory"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem 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 "subroutine"
31
+ spec.add_dependency "activesupport", ">= 3.0"
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.10"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "minitest"
36
+ spec.add_development_dependency "minitest-reporters"
37
+
38
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: subroutine-factory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Nelson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: subroutine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
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
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-reporters
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Test factories for ops using Subroutine Ops
98
+ email:
99
+ - mike@mnelson.io
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/console
111
+ - bin/setup
112
+ - lib/subroutine/factory.rb
113
+ - lib/subroutine/factory/builder.rb
114
+ - lib/subroutine/factory/config.rb
115
+ - lib/subroutine/factory/spec_helper.rb
116
+ - lib/subroutine/factory/version.rb
117
+ - subroutine-factory.gemspec
118
+ homepage: https://github.com/mnelson/subroutine-factory
119
+ licenses:
120
+ - MIT
121
+ metadata:
122
+ allowed_push_host: https://rubygems.org
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.4.6
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Test factories for ops using Subroutine Ops
143
+ test_files: []