simple_builder 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 737ab2a3d98af032aaaf155e5866c4995f133470
4
+ data.tar.gz: 9e11f0d7330398dca7ea6131f97cc96e58df1a3b
5
+ SHA512:
6
+ metadata.gz: b903db9e4a8be46c88e1203b938eeb52de47f149487378c360f3e7879ff2a6caaa596855a16297cb6643b63810cce87ec7e90f0f54ae0c7cab219bf094763029
7
+ data.tar.gz: d26e600d8bbd709c357d72d3542edd80686107d8673a0972153f9f57c6934aee81d3bdcb3d5d9fbdc189d9ba1407667e8cb0755c8a46cc4296e24e1f4c2ec3f6
data/.gitignore ADDED
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ simple_builder_gem
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_builder.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,15 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :bundler do
5
+ watch('Gemfile')
6
+ watch(/^.+\.gemspec/)
7
+ end
8
+
9
+ guard :rspec do
10
+ watch(%r{^spec/.+_spec\.rb$})
11
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
12
+ watch('spec/spec_helper.rb') { "spec" }
13
+
14
+ end
15
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Matt Wilson
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.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # SimpleBuilder
2
+
3
+ Dead-simple base class to build create/update service objects from. I got tired of reinventing something like this is every ruby/rails/sinatra app I write, so here's a gem, cruel world :).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'simple_builder'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install simple_builder
18
+
19
+ ## Usage
20
+
21
+ It's quite simple. Your implementing class has to implement two methods:
22
+
23
+ 1. `#new_instance`
24
+ 2. `#set_attributes`
25
+
26
+ Hopefully, it is obvious what those two methods should do. `#build!` and it's alias `#update!` both call `#save` on the instance to persist the changes, so you should implement or alias that as appropriate.
27
+
28
+ The initializer optionally takes a block (to which we `yield self`) so you can do interesting things like support additional parameters or set stuff up for later.
29
+
30
+ Here are some examples, both from the world of Rails, though this does not depend on anything rails-y:
31
+
32
+ ```ruby
33
+ # The simplest possible builder
34
+ class ChapterBuilder < SimpleBuilder
35
+ def new_instance
36
+ Chapter.new
37
+ end
38
+
39
+ def set_attributes
40
+ self.object.attributes = {
41
+ name: params[:name]
42
+ }
43
+ end
44
+ end
45
+ c = ChapterBuilder.build({ name: 'foo' }) # => #<Chapter id: 9, name: "foo">
46
+ ChapterBuilder.update(c, { name: 'bar' }) # => #<Chapter id: 9, name: "bar">
47
+
48
+ # A more complex example, illustrating the point of the block *and* new_instance
49
+ # Assume rails and a normal, nested-resource style controller scheme
50
+ class EventBuilder < SimpleBuilder
51
+ attr_accessor :league
52
+
53
+ def new_instance
54
+ league.events.build
55
+ end
56
+
57
+ def set_attributes
58
+ self.object.attributes = {
59
+ name: params[:name],
60
+ date: params[:date],
61
+ category: params[:category]
62
+ }
63
+ end
64
+ end
65
+
66
+ event = EventBuilder.build(params[:event]) { |eb| eb.league = @league }
67
+ EventBuilder.update(event, params[:event])
68
+ ```
69
+
70
+ ## Why do this?
71
+
72
+ I don't like the hassle of `strong_parameters`, but I like object security. I like explicit constructors and updates in order to ensure I can write solid, comprehensive unit tests for any business logic around updating and creating business objects.
73
+
74
+ This approach lets me have strong tests, confidence that I am only accepting parameters I explicitly told it too, *and* lets me keep my controllers super-tidy.
75
+
76
+ It's a win-win-win.
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it ( http://github.com/<my-github-username>/simple_builder/fork )
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,37 @@
1
+ class SimpleBuilder
2
+ VERSION = "1.0.0"
3
+
4
+ attr_accessor :params, :object
5
+
6
+ def initialize params, object = nil, &block
7
+ self.params = params
8
+ if block_given?
9
+ yield self
10
+ end
11
+ self.object = object || new_instance
12
+ end
13
+
14
+ # Builder Contract:
15
+ def new_instance
16
+ end
17
+
18
+ def set_attributes
19
+ end
20
+ # end of Contract
21
+
22
+ def self.build params, &block
23
+ new(params, &block).build!
24
+ end
25
+
26
+ def self.update object, params
27
+ new(params, object).update!
28
+ end
29
+
30
+ def build!
31
+ set_attributes
32
+ object.save
33
+ object
34
+ end
35
+ alias_method :update!, :build!
36
+
37
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_builder'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_builder"
8
+ spec.version = SimpleBuilder::VERSION
9
+ spec.authors = ["Matt Wilson"]
10
+ spec.email = ["mhw@hypomodern.com"]
11
+ spec.summary = %q{Simple, PORO object builder/updater service}
12
+ spec.description = %q{I got tired of reinventing a basic builder service in every project.}
13
+ spec.homepage = ""
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 'rspec'
24
+ spec.add_development_dependency 'guard-rspec'
25
+ spec.add_development_dependency 'guard-bundler'
26
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleBuilder do
4
+ let(:params) {
5
+ { foo: 'bar' }
6
+ }
7
+ let(:test_builder) {
8
+ Class.new(SimpleBuilder) do
9
+ def new_instance; "HI THERE"; end
10
+ end
11
+ }
12
+
13
+ context "initialization" do
14
+
15
+ it "yields itself to a block if one is given" do
16
+ SimpleBuilder.new(params) do |new_object|
17
+ new_object.should be_an_instance_of(SimpleBuilder)
18
+ end
19
+ end
20
+
21
+ context "without a given object" do
22
+ it "sets self.object to the results of the #new_instance method" do
23
+ builder = test_builder.new params
24
+ builder.object.should == "HI THERE"
25
+ end
26
+ end
27
+
28
+ context "with a given object" do
29
+ it "sets self.object to the given object" do
30
+ builder = test_builder.new params, "A Dummy Object"
31
+ builder.object.should == "A Dummy Object"
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ describe "#build!" do
38
+ let(:builder) { test_builder.new params }
39
+ before do
40
+ builder.object.stub(:save)
41
+ end
42
+
43
+ it "calls #set_attributes" do
44
+ builder.should_receive(:set_attributes)
45
+ builder.build!
46
+ end
47
+
48
+ it "attempts to save self.object" do
49
+ builder.object.should_receive(:save)
50
+ builder.build!
51
+ end
52
+
53
+ it "returns self.object" do
54
+ builder.build!.should == builder.object
55
+ end
56
+ end
57
+
58
+ end
@@ -0,0 +1,14 @@
1
+ require 'simple_builder'
2
+
3
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ config.filter_run :focus
8
+
9
+ # Run specs in random order to surface order dependencies. If you find an
10
+ # order dependency and want to debug it, you can fix the order by providing
11
+ # the seed, which is printed after each run.
12
+ # --seed 1234
13
+ config.order = 'random'
14
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Wilson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-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.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: 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: guard-bundler
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: I got tired of reinventing a basic builder service in every project.
84
+ email:
85
+ - mhw@hypomodern.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - .ruby-gemset
93
+ - .ruby-version
94
+ - Gemfile
95
+ - Guardfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - lib/simple_builder.rb
100
+ - simple_builder.gemspec
101
+ - spec/simple_builder/simple_builder_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: ''
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.2.0
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Simple, PORO object builder/updater service
127
+ test_files:
128
+ - spec/simple_builder/simple_builder_spec.rb
129
+ - spec/spec_helper.rb