factory_hero 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d3a31a976b94dbed1796223e518276051261b823
4
+ data.tar.gz: 081920dd4f22ea36e93af887b1b49f0c34479a46
5
+ SHA512:
6
+ metadata.gz: 4c662da0e6b8f0434435e2333c4dbbed9ea999c3ad4d4cf4af112d212dfff31b43f3f3eacf886f6dc4cc6864ce7a7fa2ccce4c09bee515de5d85915950b4890a
7
+ data.tar.gz: 925a2e9a3569753764a9eddba196f0ae6ea089dd34f546778bd09e82da3ae163d54a369afcb7a612cdc65212311229393e514d914a8645f5a68a5918328f6f0a
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.ruby-version
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /Guardfile
6
+ /_yardoc/
7
+ /coverage/
8
+ /doc/
9
+ /pkg/
10
+ /spec/reports/
11
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in factory_hero.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Paweł Gościcki
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.
@@ -0,0 +1,65 @@
1
+ # FactoryHero
2
+
3
+ This gem is a poor man's attempt at replacing the fantastic [FactoryGirl](https://github.com/thoughtbot/factory_girl) gem done by [thoughtbot](https://thoughtbot.com/).
4
+
5
+ It is more of a proof of concept (or rather an exercise in coding) instead of a fully fledged working solution.
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'factory_hero'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```bash
19
+ $ bundle
20
+ ```
21
+
22
+ Or install it yourself as:
23
+
24
+ ```bash
25
+ $ gem install factory_hero
26
+ ```
27
+
28
+
29
+ ## Usage
30
+
31
+ Defining factories:
32
+
33
+ ```ruby
34
+ FactoryHero.define_factory(User) do
35
+ name "foobar"
36
+ end
37
+
38
+ FactoryHero.define_factory(:admin, class: User) do
39
+ name "fobar"
40
+ admin true
41
+ end
42
+ ```
43
+
44
+
45
+ Building factory objects:
46
+
47
+ ```ruby
48
+ FactoryHero.build(:user)
49
+ # => #<User:0x007fb98492834 @name="foobar">
50
+
51
+ FactoryHero.build(:admin)
52
+ # => #<User:0x007fb98492834 @name="foobar" @admin=true>
53
+ ```
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ 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).
60
+
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at
65
+ [https://github.com/pjg/factory_hero](https://github.com/pjg/factory_hero)
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "factory_hero"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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 'factory_hero/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "factory_hero"
8
+ spec.version = FactoryHero::VERSION
9
+ spec.licenses = ['MIT']
10
+ spec.authors = ["Paweł Gościcki"]
11
+ spec.email = "pawel.goscicki@gmail.com"
12
+
13
+ spec.summary = %q{FactoryHero for creating factories}
14
+ spec.description = %q{FactoryGirl replacement (proof of concept)}
15
+ spec.homepage = "https://github.com/pjg/factory_hero"
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 "rspec"
25
+ spec.add_development_dependency "guard-rspec"
26
+ spec.add_development_dependency "pry"
27
+ end
@@ -0,0 +1,32 @@
1
+ require 'factory_hero/configuration'
2
+ require 'factory_hero/exceptions'
3
+ require 'factory_hero/factory'
4
+ require 'factory_hero/symbolize'
5
+ require "factory_hero/version"
6
+
7
+ module FactoryHero
8
+
9
+ using Symbolize
10
+
11
+ def self.configuration
12
+ @@configuration ||= Configuration.new
13
+ end
14
+
15
+ def self.define_factory klass_or_symbol, options = {}, &block
16
+ symbol = klass_or_symbol.symbolize
17
+
18
+ configuration.register_factory Factory.new symbol, options, &block
19
+ end
20
+
21
+ def self.build klass_or_symbol, attrs = {}
22
+ symbol = klass_or_symbol.symbolize
23
+ factory = configuration.load_factory symbol
24
+
25
+ factory.build attrs
26
+ end
27
+
28
+ def self.clear!
29
+ configuration.clear!
30
+ end
31
+
32
+ end
@@ -0,0 +1,41 @@
1
+ class Configuration
2
+
3
+ attr_reader :factories
4
+
5
+ def initialize
6
+ @factories = {}
7
+ end
8
+
9
+ def register_factory factory
10
+ raise_if_exists factory.symbol
11
+
12
+ factories[factory.symbol] = factory
13
+ end
14
+
15
+ def load_factory symbol
16
+ factories.fetch(symbol) do |symbol|
17
+ undefined_factory_exception symbol
18
+ end
19
+ end
20
+
21
+ def clear!
22
+ factories.clear
23
+ end
24
+
25
+ private
26
+
27
+ def raise_if_exists symbol
28
+ return unless factories.has_key? symbol
29
+
30
+ factory_already_defined_exception symbol
31
+ end
32
+
33
+ def undefined_factory_exception symbol
34
+ raise UndefinedFactory.new(symbol), 'No factory definition with this name'
35
+ end
36
+
37
+ def factory_already_defined_exception symbol
38
+ raise FactoryAlreadyDefined.new(symbol), 'There is already a factory defined with this name'
39
+ end
40
+
41
+ end
@@ -0,0 +1,5 @@
1
+ class UndefinedFactory < ArgumentError
2
+ end
3
+
4
+ class FactoryAlreadyDefined < ArgumentError
5
+ end
@@ -0,0 +1,51 @@
1
+ class Factory
2
+
3
+ attr_reader :default_attributes, :symbol
4
+
5
+ def initialize symbol, options = {}, &block
6
+ @symbol = symbol
7
+ @default_attributes = {}
8
+ @options = options
9
+
10
+ instance_eval(&block) if block_given?
11
+ end
12
+
13
+ def build attributes = {}
14
+ build_object.tap do |obj|
15
+ assign_attributes default_attributes
16
+ assign_attributes attributes
17
+ end
18
+ end
19
+
20
+ # used to assign default attributes
21
+ def method_missing(method, *args)
22
+ @default_attributes[method] = args.first
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :options, :object
28
+
29
+ def build_object
30
+ @object = klass.new
31
+ end
32
+
33
+ def klass
34
+ @klass ||= class_from_options || class_from_symbol
35
+ end
36
+
37
+ def class_from_options
38
+ options[:class]
39
+ end
40
+
41
+ def class_from_symbol
42
+ Object::const_get symbol.to_s.capitalize
43
+ end
44
+
45
+ def assign_attributes attributes
46
+ attributes.each do |attribute, value|
47
+ object.public_send "#{ attribute }=", value
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1,9 @@
1
+ module Symbolize
2
+
3
+ refine Object do
4
+ def symbolize
5
+ to_s.downcase.to_sym
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,3 @@
1
+ module FactoryHero
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factory_hero
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Paweł Gościcki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-26 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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
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: pry
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: FactoryGirl replacement (proof of concept)
84
+ email: pawel.goscicki@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - ".gitignore"
90
+ - Gemfile
91
+ - LICENSE
92
+ - README.md
93
+ - Rakefile
94
+ - bin/console
95
+ - bin/setup
96
+ - factory_hero.gemspec
97
+ - lib/factory_hero.rb
98
+ - lib/factory_hero/configuration.rb
99
+ - lib/factory_hero/exceptions.rb
100
+ - lib/factory_hero/factory.rb
101
+ - lib/factory_hero/symbolize.rb
102
+ - lib/factory_hero/version.rb
103
+ homepage: https://github.com/pjg/factory_hero
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.6.3
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: FactoryHero for creating factories
127
+ test_files: []