bazza 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create use 1.9.2@bazza
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+ gem 'rspec'
data/README.markdown ADDED
@@ -0,0 +1,80 @@
1
+ bazza - the builder
2
+ =======
3
+
4
+ Build objects easily:
5
+
6
+ * specify defaults
7
+ * tailor defaults within tests at build time with optional hash or block
8
+ * chain builders
9
+ * reuse builder instances to create multiple objects
10
+ * specialise builders
11
+ * Readable usage
12
+ ```ruby
13
+ customer = Customer.with.Order.with(:total => 22.21).thats.active
14
+ ```
15
+
16
+ Why not a use factory?
17
+
18
+ * Common for consumers of builders to:
19
+ * specialise defaults; provide a readable way to accommodate this;
20
+ * build several objects of the same type;
21
+
22
+ An Example
23
+ ----------
24
+
25
+ ```ruby
26
+ customer = Customer.with.Order.with(:total => 22.21).thats.active
27
+ ```
28
+
29
+ class Customer
30
+ end
31
+
32
+ class Order
33
+ end
34
+
35
+ class CustomerBuilder
36
+ end
37
+
38
+ class OrderBuilder
39
+ end
40
+
41
+
42
+ License
43
+ -------
44
+
45
+ <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">
46
+ <img src="http://i.creativecommons.org/l/by/3.0/88x31.png" alt="Creative Commons License" style="border:none;" height="31" width="88">
47
+ </a>
48
+
49
+
50
+ TODO
51
+ ----
52
+
53
+ * Class>>#build interpolate builder string rather than concat
54
+ * Split bazza spec by building/defaulting to reduce nesting
55
+ * Gemfile: make rspec dev dependency
56
+ * Overriding defaults
57
+ * Reusing builders
58
+ * Replace String>>#to_class implementation from stack overflow article with a spec'd/tested version
59
+
60
+ Notes
61
+ -----
62
+
63
+ * Old school way:
64
+
65
+ orderBuilder = OrderBuilder.new.with(:total => 22.21).makeActive
66
+ customer = CustomerBuilder.with(:orderBuilder => orderBuilder).build
67
+
68
+ * Aim:
69
+
70
+ Customer.with.Order.with(:total => 22.21).thats.active
71
+
72
+ - with
73
+ - no args: assumes next token is class name of associated attribute
74
+ - no-op, return self
75
+ - next message expected to be class name, resolve via method missing?
76
+ - What about other method missing handlers? Better to return instance of class rather than self that permits chaining.
77
+ - attributeBuilder
78
+
79
+ - args: attributes of target being built
80
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task :default => :spec
data/bazza.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "bazza/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "bazza"
7
+ s.version = Bazza::VERSION
8
+ s.authors = ["mattdunn"]
9
+ s.email = ["contact@mattonsoftware.com"]
10
+ s.homepage = "https://github.com/mattdunn/bazza"
11
+ s.summary = "bazza - the builder"
12
+ s.description = ""
13
+
14
+ s.rubyforge_project = "bazza"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+ # s.add_runtime_dependency "rest-client"
23
+ end
data/lib/bazza.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "bazza/version"
2
+ require "bazza/string_extensions"
3
+ require "bazza/class_extensions"
@@ -0,0 +1,6 @@
1
+ class Class
2
+ def build
3
+ klass = (self.name + 'Builder').to_class
4
+ return klass.nil? ? nil : klass.new.build
5
+ end
6
+ end
@@ -0,0 +1,11 @@
1
+ # Rather than introducing a dependency on ActiveSupports#constantize,
2
+ # using implementation from http://stackoverflow.com/questions/1448670/ruby-stringto-class
3
+ class String
4
+ def to_class(parent = Kernel)
5
+ chain = self.split "::"
6
+ klass = parent.const_get chain.shift
7
+ return chain.size < 1 ? (klass.is_a?(Class) ? klass : nil) : chain.join("::").to_class(klass)
8
+ rescue
9
+ nil
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Bazza
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'models/order'
3
+
4
+ describe Bazza do
5
+ describe 'building an object' do
6
+ describe 'resolving builder class [ClassName]Builder' do
7
+ context 'when no class exists' do
8
+ it 'should return nil' do
9
+ Order.build.should be_nil
10
+ end
11
+ end
12
+ context 'when class exists' do
13
+ before do
14
+ class OrderBuilder
15
+ def build
16
+ Order.new({})
17
+ end
18
+ end
19
+ end
20
+ it 'should create an instance' do
21
+ Order.build.should be_an_instance_of Order
22
+ end
23
+ context 'with defaults' do
24
+ before do
25
+ class OrderBuilder
26
+ def build
27
+ Order.new(:total => 22.34)
28
+ end
29
+ end
30
+ end
31
+ it 'should create instance with defaults' do
32
+ Order.build.total.should == 22.34
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ class Order
2
+ attr_reader :total
3
+
4
+ def initialize(attrs)
5
+ @total = attrs[:total]
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'bazza'
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :rspec
7
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bazza
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - mattdunn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-10-18 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :development
26
+ version_requirements: *id001
27
+ description: ""
28
+ email:
29
+ - contact@mattonsoftware.com
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - .gitignore
38
+ - .rspec
39
+ - .rvmrc
40
+ - Gemfile
41
+ - README.markdown
42
+ - Rakefile
43
+ - bazza.gemspec
44
+ - lib/bazza.rb
45
+ - lib/bazza/class_extensions.rb
46
+ - lib/bazza/string_extensions.rb
47
+ - lib/bazza/version.rb
48
+ - spec/bazza_spec.rb
49
+ - spec/models/order.rb
50
+ - spec/spec_helper.rb
51
+ has_rdoc: true
52
+ homepage: https://github.com/mattdunn/bazza
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project: bazza
75
+ rubygems_version: 1.6.1
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: bazza - the builder
79
+ test_files:
80
+ - spec/bazza_spec.rb
81
+ - spec/models/order.rb
82
+ - spec/spec_helper.rb