barn 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 46fea679db70558d95e5db74e670a33a57e400d8
4
+ data.tar.gz: 93fd295b38be5e6fdf479b385f1db050f542d289
5
+ SHA512:
6
+ metadata.gz: c8bbd9f3c336207bb12fd2427361e5e2f128aea2e62519a2f2155be6cf2045d1bbd6ebb7c1b4f56cb3030b297ae9f7107e97bdd87a05d805024375562fe3b1d3
7
+ data.tar.gz: d1b34e51048f4e60e58fb915921a7bc9839dd345095d5dddb45cfe62096dbabdd320e5fcd8d25f44c29591cfacfc5c2ed058e5d5638399781223098afe41fead
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in barn.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jerry Cheung
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,111 @@
1
+ # Barn
2
+
3
+ Store lazily evaluated blocks for building test fixtures.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ # Define factories with lazily evaluated blocks
9
+ Barn.define :post do
10
+ :title => "What's Your Dream Barn Find?",
11
+ :published_at => Time.now
12
+ end
13
+
14
+ # Build instances and override default attributes
15
+ Barn.build(:post, :title => "Original Owner Jaguar E-Type For Sale")
16
+
17
+ # Compose complex objects
18
+ Barn.define :comment do
19
+ :post => build(:post),
20
+ :body => "That's a lot of hay for a little pay."
21
+ end
22
+ ```
23
+
24
+ ## Namespacing
25
+
26
+ When you define factories, they are stored in the global `Barn` namespace by
27
+ default. But we do allow you to create your own namespaces.
28
+
29
+ ```ruby
30
+ module Factory
31
+ module Post
32
+ extend Barn::Namespace
33
+
34
+ define :draft {...}
35
+ define :published {...}
36
+ end
37
+ end
38
+
39
+ Factory::Post.build(:draft)
40
+ ```
41
+
42
+ ## Build Chains
43
+
44
+ A build chain is a list of builders that are called to instantiate an instance.
45
+ It's the same concept as Rack middleware, but we build object instances instead
46
+ of HTTP responses. A builder is anything that responds to `build` and returns a
47
+ contructed object. An environment hash of available options to passed to
48
+ `build`. The default build chain has one builder, [Barn::Builders::HashMerge],
49
+ that merges attributes into a hash. See [/lib/barn/builders] for more examples.
50
+
51
+ ```ruby
52
+ # Inspect a build chain
53
+ Barn.build_chain
54
+ => [Barn::Builders::HashMerge] # Default is to merge hash attributes
55
+
56
+ # After building hash attributes, instantiates an ActiveRecord object
57
+ Barn.build_chain.unshift Barn::Builders::ActiveRecord
58
+
59
+ # Instrument the time it takes to run a build chain
60
+ Barn.build_chain.unshift Barn::Builders::Instrumentation
61
+
62
+ Barn.build_chain
63
+ => [Barn::Builders::Instrumentation, Barn::Builders::ActiveRecord, Barn::Builders::HashMerge]
64
+ ```
65
+
66
+ ## Helpers
67
+
68
+ Barn provides helpers to let you type less in your tests. Include
69
+ `Barn.helpers` to skip typing `Barn.build` and `Barn.define`.
70
+
71
+ ```ruby
72
+ # RSpec
73
+ RSpec.configure { |c| c.include Barn::Helpers }
74
+
75
+ describe "Post" do
76
+ it "requires a title" do
77
+ post = build(:post)
78
+ end
79
+ end
80
+
81
+ # ActiveSupport::TestCase
82
+ class PostTest < ActiveSupport::TestCase
83
+ include Barn::Helpers
84
+
85
+ test "requires a title" do
86
+ post = build(:post)
87
+ end
88
+ end
89
+ ```
90
+
91
+ ## Installation
92
+
93
+ Add this line to your application's Gemfile:
94
+
95
+ gem 'barn'
96
+
97
+ And then execute:
98
+
99
+ $ bundle
100
+
101
+ Or install it yourself as:
102
+
103
+ $ gem install barn
104
+
105
+ ## Contributing
106
+
107
+ 1. Fork it
108
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
109
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
110
+ 4. Push to the branch (`git push origin my-new-feature`)
111
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'barn/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "barn"
8
+ spec.version = Barn::VERSION
9
+ spec.authors = ["Jerry Cheung"]
10
+ spec.email = ["jch@whatcodecraves.com"]
11
+ spec.description = %q{Store lazily evaluated blocks for building test fixtures}
12
+ spec.summary = %q{Small no-magic library for building test factories}
13
+ spec.homepage = "https://github.com/jch/barn"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ spec.add_development_dependency "debugger"
25
+ end
@@ -0,0 +1,10 @@
1
+ require "barn/version"
2
+ require "barn/factory"
3
+ require "barn/namespace"
4
+ require "barn/helpers"
5
+
6
+ require "barn/builders/hash"
7
+
8
+ module Barn
9
+ extend Namespace
10
+ end
@@ -0,0 +1,18 @@
1
+ module Barn
2
+ module Builders
3
+ class Hash
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ # `env` is a hash with:
9
+ # :name - symbol name of the factory
10
+ # :options - optional params passed to `Barn.define`
11
+ # :args - parameters passed to the block of`Barn.build`
12
+ def call(env)
13
+ object = @app.call(env)
14
+ object.merge(env[:args])
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ module Barn
2
+ # maybe Proc already defines __FILENAME__ and __LINENUMBER__
3
+ class Factory
4
+ # help with debugging where something was defined
5
+ attr_accessor :filename, :line
6
+
7
+ attr_reader :name, :options
8
+
9
+ def initialize(name, options = {}, &blk)
10
+ @name = name
11
+ @options = options
12
+ @blk = blk
13
+ end
14
+
15
+ # Returns built object
16
+ def call(env)
17
+ @blk.call(env)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ require 'forwardable'
2
+
3
+ module Barn
4
+ # Forwards all `define` and `build` calls to a barn configured on the class.
5
+ #
6
+ # class SomeTest
7
+ # include Barn::Helpers
8
+ # self.barn = MyCustomBarn
9
+ # end
10
+ module Helpers
11
+ def self.included(base)
12
+ base.class_eval do
13
+ extend Forwardable
14
+ def_delegators :"self.class.barn", :define, :build
15
+
16
+ class <<self
17
+ extend Forwardable
18
+ def_delegators :"self.barn", :define, :build
19
+
20
+ attr_writer :barn
21
+ def barn
22
+ @barn || ::Barn
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,33 @@
1
+ module Barn
2
+ module Namespace
3
+ def define(name, factory_options = {}, &blk)
4
+ blk ||= Proc.new {}
5
+ factories[name] = Factory.new(name, factory_options, &blk)
6
+ end
7
+
8
+ def build(name, build_options = {})
9
+ factory = factories[name]
10
+
11
+ chained_builder = build_chain.reverse.reduce(factory) do |final, builder|
12
+ builder.new(final)
13
+ end # not caching this for simplicity
14
+
15
+ chained_builder.call \
16
+ :factory => factory,
17
+ :args => build_options
18
+ end
19
+
20
+ def build_chain
21
+ @build_chain ||= [Barn::Builders::Hash]
22
+ end
23
+
24
+ def factories
25
+ @factories ||= {}
26
+ end
27
+
28
+ def reset
29
+ @computed_chain = nil
30
+ @factories = {}
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Barn
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,50 @@
1
+ require 'barn'
2
+ require 'minitest/autorun'
3
+
4
+ class BarnTest < MiniTest::Test
5
+ def setup
6
+ Barn.reset
7
+ Barn.define :user do
8
+ {:email => "jollyjerry@gmail.com"}
9
+ end
10
+ end
11
+
12
+ def test_define
13
+ assert Barn.factories[:user]
14
+ end
15
+
16
+ def test_double_define_error
17
+ end
18
+
19
+ def test_build
20
+ user = Barn.build(:user)
21
+ assert_equal 'jollyjerry@gmail.com', user[:email]
22
+ end
23
+
24
+ def test_build_override
25
+ user = Barn.build(:user, :email => 'jch@whatcodecraves.com')
26
+ assert_equal 'jch@whatcodecraves.com', user[:email]
27
+ end
28
+
29
+ def test_build_unknown
30
+
31
+ end
32
+
33
+ def test_helpers
34
+ custom_barn = Module.new do
35
+ extend Barn::Namespace
36
+
37
+ define :foo
38
+ end
39
+
40
+ klass = Class.new do
41
+ include Barn::Helpers
42
+ self.barn = custom_barn
43
+
44
+ define :bar
45
+ end
46
+
47
+ assert custom_barn.factories[:foo]
48
+ assert custom_barn.factories[:bar]
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: barn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jerry Cheung
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-27 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.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.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '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: debugger
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
+ description: Store lazily evaluated blocks for building test fixtures
70
+ email:
71
+ - jch@whatcodecraves.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - barn.gemspec
82
+ - lib/barn.rb
83
+ - lib/barn/builders/hash.rb
84
+ - lib/barn/factory.rb
85
+ - lib/barn/helpers.rb
86
+ - lib/barn/namespace.rb
87
+ - lib/barn/version.rb
88
+ - test/barn_test.rb
89
+ homepage: https://github.com/jch/barn
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.0.3
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Small no-magic library for building test factories
113
+ test_files:
114
+ - test/barn_test.rb