deadlift 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cbc98d9b037fca83760002ae1d73b031df6c1137
4
- data.tar.gz: 6b920425e0916598db573e680523576e600970a7
3
+ metadata.gz: d06a1714dd94dd5412117e9b881dbe07be9915f9
4
+ data.tar.gz: 9700b906a37706c21deb84efc3dc7eedee4d71e7
5
5
  SHA512:
6
- metadata.gz: f9882780130db405bde0561292bed8d02ccc4db0e0689b77ff3653ea27ac58cb41b8bd9723b48e89fa0801729e9f923f2e8c5712c64cb057aaf4ec6538a77e19
7
- data.tar.gz: 8a14be060c98e727e361d3fe62b59c77b210a33f9efd12d807a9ea7d20e35a4ea30c98177c23d15eddb8c787eeff51fc5a97fa032a0a83fd9cc23573d587b158
6
+ metadata.gz: 6deef435c2ec0fcf2129a4f452ed0c330632d932312e92c0d93a8c6cc03f1498a4d2d6b6bbbf6764df75b4bc3b2e88e72bc82d5f363245ef41279ee093f1c842
7
+ data.tar.gz: 139640e09e4b6ef2e802e08f55a69d3600785f5ececfaf9845cdca650df91bb105ac95b4e52803e47c759913d872c21f16d093137b4b2f5d7b53abc9dfc72d67
@@ -0,0 +1,26 @@
1
+ .DS_Store
2
+ *.gem
3
+ *.rbc
4
+ /.config
5
+ /coverage/
6
+ /InstalledFiles
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/examples.txt
10
+ /test/tmp/
11
+ /test/version_tmp/
12
+ /tmp/
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+ /.yardoc/
17
+ /_yardoc/
18
+ /doc/
19
+ /rdoc/
20
+ /.bundle/
21
+ /vendor/bundle
22
+ /lib/bundler/man/
23
+ Gemfile.lock
24
+ .ruby-version
25
+ .ruby-gemset
26
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in deadlift.gemspec
4
+ gemspec
@@ -0,0 +1,5 @@
1
+ guard :rspec, cmd: 'bundle exec rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Karl Metum
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.
@@ -0,0 +1,202 @@
1
+ # Deadlift
2
+ [![Gem Version](https://badge.fury.io/rb/deadlift.svg)](https://badge.fury.io/rb/deadlift)
3
+ [![Code Climate](https://codeclimate.com/repos/562cd53ae30ba00e2802697f/badges/59cbdd283684eae64fda/gpa.svg)](https://codeclimate.com/repos/562cd53ae30ba00e2802697f/feed)
4
+ [![Build Status](https://travis-ci.org/karlingen/deadlift.svg?branch=master)](https://travis-ci.org/karlingen/deadlift)
5
+
6
+ Deadlift is a powerful gem that you should be using if you want to remove fat from your controllers and models.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'deadlift'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install deadlift
23
+
24
+ ## Usage
25
+
26
+ TODO: Write usage instructions here
27
+
28
+ # What is this?
29
+
30
+ The concept here is to be able to refactor this example logic:
31
+
32
+ ```ruby
33
+ # ProductsController.rb
34
+ ..
35
+ def create
36
+ @client = Product.new(product_params)
37
+ if @product.save
38
+ NotifyTechDepartment.new(@product.id).deliver
39
+ NotifyCEOAboutProductIncreaseMailer.new(@product.id).deliver
40
+ redirect_to @product
41
+ else
42
+ NotifyCEOAboutProductFailureMailer.new(@product.id).deliver
43
+ render "new"
44
+ end
45
+ end
46
+ ..
47
+ private
48
+ def product_params
49
+ params.require[:product].permit(:name, :price, :brand, :category)
50
+ end
51
+ ```
52
+
53
+ to:
54
+
55
+ ```ruby
56
+ def create
57
+ CreateProductDeadlift.new(params[:product])
58
+ .add_trainer(self) # report to self a.k.a. the controller
59
+ .add_trainer(NotifyCEOAboutProductIncrease.new)
60
+ .perform
61
+ end
62
+
63
+ def create_success(performance)
64
+ redirect_to performance.result
65
+ end
66
+
67
+ def create_failure(performance)
68
+ @client = performance.barbell
69
+ render "new"
70
+ end
71
+ ```
72
+
73
+ Note how we are not asking if the save is successful. We are simply performing a deadlift and setting what will happen if the deadlift is successful and what will happen if it fails.
74
+
75
+ When a deadlift is performed a `Performance` object is passed to either `create_success` or `create_failure` depending on the deadlift's results.
76
+ You can then access the barbell again from the performance or the result itself, which in this case is the created product.
77
+
78
+ Also, we do not need strong parameters since this will be taken care of in the `Barbell` stage
79
+
80
+ **No pain, no gain!**
81
+
82
+
83
+ Deadlifts
84
+ --
85
+ A deadlift is a fancy name for an Interactor which consists of all possible actions that should occur when performing it. Every deadlift needs a `perform` method.
86
+ For instance, in the example above, when performing a deadlift (creating a product) I want to validate the passed in values, persist the product in the database and finally notify some tech-department that we have a new product in stock.
87
+
88
+ I'd define the deadlift like this:
89
+
90
+ ```ruby
91
+ # app/deadlifts/create_product_deadlift.rb
92
+ class CreateProductDeadlift < Deadlift::Base
93
+
94
+ # Reference a barbell!
95
+ def self.barbell(params)
96
+ CreateProductBarbell.new(params)
97
+ end
98
+
99
+ def perform
100
+ perform_and_report_for_valid do
101
+ set_attributes
102
+ persist
103
+ notify_tech
104
+ end
105
+ end
106
+
107
+ private
108
+
109
+ def set_attributes
110
+ product.name = barbell.name
111
+ product.price = barbell.price
112
+ product.brand = barbell.brand
113
+ product.category = barbell.category
114
+ end
115
+
116
+ def persist
117
+ product.save
118
+ end
119
+
120
+ def notify_tech
121
+ NotifyTechDepartment.new(product.id).deliver
122
+ end
123
+
124
+ def product
125
+ @product ||= Product.new
126
+ end
127
+
128
+ # Setting what the result should be when calling performance.result
129
+ def result
130
+ product
131
+ end
132
+ end
133
+ ```
134
+
135
+ Barbells
136
+ ---
137
+ A barbell is a fancy name for a plain [reform](https://github.com/apotonick/reform) contract.
138
+ Obviously, to perform a deadlift you need a barbell.
139
+ The barbell is a reform object initialized with `params`. If the barbell is present and valid then the Deadlift is performed.
140
+
141
+ ```ruby
142
+ # app/barbells/create_product_barbell.rb
143
+ class CreateProductBarbell < Deadlift::Barbell::Base
144
+ model :product
145
+
146
+ property :name
147
+ property :price
148
+ property :brand
149
+ property :category
150
+
151
+ validate :name, presence: true
152
+ validate :price, presence: true
153
+ validate :brand, presence: true
154
+ validate :category, presence: true
155
+ end
156
+ ```
157
+
158
+ Trainers
159
+ ---
160
+ You can add a trainer to any deadlift.
161
+ The deadlift reports to the trainer when it is done, whether it has succeeded or failed.
162
+ A trainer is a PORO that can do anything after a deadlift is performed.
163
+
164
+ ```ruby
165
+ # app/trainers/notify_ceo_about_product_increase.rb
166
+ class NotifyCeoAboutProductIncrease
167
+
168
+ def success(performance)
169
+ @absence = performance.result
170
+ NotifyCEOAboutProductIncreaseMailer.new(@absence.id).deliver
171
+ end
172
+
173
+ def failure(performance)
174
+ attrs = performance.barbell.attributes
175
+ NotifyCEOAboutProductFailureMailer.new(attrs: attrs).deliver
176
+ end
177
+ end
178
+
179
+ ```
180
+
181
+ Performances
182
+ ---
183
+ A performance is an object passed from the deadlift results.
184
+ You can ask the performance object for the `#result`.
185
+ You can also ask for the `barbell` object or the `deadlift` if you somehow need it.
186
+
187
+
188
+ ## Development
189
+
190
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bundle exec bin/console` for an interactive prompt that will allow you to experiment.
191
+
192
+ 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).
193
+
194
+ ## Contributing
195
+
196
+ Bug reports and pull requests are welcome on GitHub at https://github.com/karlingen/deadlift. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org/) code of conduct.
197
+
198
+
199
+ ## License
200
+
201
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
202
+
@@ -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
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application '_guard-core' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('guard', '_guard-core')
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "deadlift"
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,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'guard' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('guard', 'guard')
@@ -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,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'deadlift/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'deadlift'
8
+ spec.version = Deadlift::VERSION
9
+ spec.authors = ['Karl Metum']
10
+ spec.email = ['karl.metum@codehacker.se']
11
+
12
+ spec.summary = %q{A gem that shrinkens your fat controller}
13
+ spec.description = %q{Deadlift is a powerful gem that shrinkens your fat code}
14
+ spec.homepage = 'https://github.com/karlingen/deadlift'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'reform', '~> 2.0.5', '>= 2.0.5'
23
+ spec.add_dependency 'activemodel', '~> 4.2.0', '>= 4.2.0'
24
+ spec.add_dependency 'hashugar', '~> 1.0'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.10'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'pry', '~> 0.10.3'
29
+ spec.add_development_dependency 'rspec', '~> 3.3'
30
+ spec.add_development_dependency 'guard', '~> 2.13'
31
+ spec.add_development_dependency 'guard-rspec', '~> 4.6', '>= 4.6.4'
32
+ end
@@ -1,2 +1,13 @@
1
- class Deadlift
1
+ require 'hashugar'
2
+ require 'reform'
3
+ require 'reform/form/active_model/validations'
4
+
5
+ Reform::Contract.class_eval do
6
+ include Reform::Form::ActiveModel::Validations
2
7
  end
8
+
9
+ require 'deadlift/version'
10
+ require 'deadlift/barbell/base'
11
+ require 'deadlift/barbell/default'
12
+ require 'deadlift/performance'
13
+ require 'deadlift/base'
@@ -0,0 +1,12 @@
1
+ module Deadlift
2
+ module Barbell
3
+ class Base < Reform::Contract
4
+
5
+ alias_method :approved?, :validate
6
+
7
+ def initialize(params = {})
8
+ super params.to_hashugar
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ require 'ostruct'
2
+
3
+ module Deadlift
4
+ module Barbell
5
+ class Default < OpenStruct
6
+
7
+ def initialize(params)
8
+ @barbell = Base.new(params)
9
+ super params
10
+ end
11
+
12
+ def errors
13
+ @barbell.errors
14
+ end
15
+
16
+ def approved?
17
+ errors.empty?
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,88 @@
1
+ module Deadlift
2
+ class Base
3
+ attr_reader :barbell
4
+
5
+ def self.barbell(params = {})
6
+ Deadlift::Barbell::Default.new(params)
7
+ end
8
+
9
+ def self.validate(*messages)
10
+ @validations ||= []
11
+ validations.concat messages
12
+ end
13
+
14
+ def self.validations
15
+ @validations
16
+ end
17
+
18
+ def self.get_default_trainers
19
+ Array(@default_trainers)
20
+ end
21
+
22
+ def self.default_trainers(*trainers)
23
+ @default_trainers = trainers
24
+ end
25
+
26
+ def initialize(params)
27
+ klass = self.class
28
+ @params = params
29
+ @barbell = klass.barbell(@params)
30
+ klass.get_default_trainers.each{|trainer| add_trainer(trainer)}
31
+ end
32
+
33
+ def add_trainer(*trainers)
34
+ self.trainers.concat trainers
35
+ self
36
+ end
37
+
38
+ def perform_and_report_for_valid(&block)
39
+ if barbell_approved?
40
+ yield
41
+ report :success
42
+ else
43
+ report :failure
44
+ end
45
+ end
46
+
47
+ def trainers
48
+ @trainers ||= []
49
+ end
50
+
51
+ def report(type)
52
+ trainers.each do |trainer|
53
+ trainer.public_send("#{type}", performance)
54
+ end
55
+ nil
56
+ end
57
+
58
+ def report_progress(performance)
59
+ trainers.each do |trainer|
60
+ trainer.public_send(:progress, performance)
61
+ end
62
+ nil
63
+ end
64
+
65
+ def barbell_approved?
66
+ return false unless barbell.approved?
67
+ Array(self.class.validations).each do |message|
68
+ self.send message
69
+ end
70
+ Array(barbell.errors).empty?
71
+ end
72
+
73
+ def performance
74
+ @performance ||= Deadlift::Performance.new(barbell, result, interaction)
75
+ end
76
+
77
+ # Override if needed
78
+ def result
79
+ NullResult.new
80
+ end
81
+
82
+ def interaction
83
+ self.class.name.demodulize.underscore.to_sym
84
+ end
85
+
86
+ class NullResult; end
87
+ end
88
+ end
@@ -0,0 +1,11 @@
1
+ module Deadlift
2
+ class Performance
3
+ attr_reader :barbell, :result, :deadlift
4
+
5
+ def initialize(barbell, result, deadlift = :undefined_method)
6
+ @result = result
7
+ @barbell = barbell
8
+ @deadlift = deadlift
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Deadlift
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Deadlift do
4
+ it 'has a version number' do
5
+ expect(Deadlift::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Deadlift::Barbell::Base do
4
+
5
+ describe 'with params' do
6
+ let(:params) { { weight: '100' } }
7
+ let(:barbell) { SomeBarbell.new(params) }
8
+
9
+ specify {
10
+ expect(barbell.approved?).to be_truthy
11
+ expect(barbell.weight).to eq params[:weight]
12
+ }
13
+ end
14
+
15
+ describe 'with nested params' do
16
+ let!(:barbell) { DeadliftWithAFriendBarbell.new(params) }
17
+
18
+ describe 'valid input' do
19
+ let(:friend_params) { { name: 'Arnold' } }
20
+ let(:params) { { weight: '100', friend: friend_params } }
21
+
22
+ specify {
23
+ expect(barbell.approved?).to be_truthy
24
+ expect(barbell.weight).to eq params[:weight]
25
+ expect(barbell.friend.name).to eq friend_params[:name]
26
+ }
27
+ end
28
+
29
+ describe 'invalid input' do
30
+ let(:nested_params) { { name: 'Arnold' } }
31
+ let(:params) { { weight: nil, friend: nested_params } }
32
+
33
+ specify {
34
+ expect(barbell.approved?).to be_falsey
35
+ expect(barbell.errors[:weight].size).to eq(1)
36
+ }
37
+ end
38
+
39
+ describe 'invalid input on nested' do
40
+ let(:nested_params) { { name: nil } }
41
+ let(:params) { { weight: 52, friend: nested_params } }
42
+
43
+ specify {
44
+ expect(barbell.approved?).to be_falsey
45
+ expect(barbell.errors[:'friend.name'].size).to eq(1)
46
+ }
47
+ end
48
+
49
+ describe 'multiple instances' do
50
+ let(:params) { { weight: 52 } }
51
+ let(:number_two) { DeadliftWithAFriendBarbell.new({}) }
52
+
53
+ specify { expect(number_two.weight).to_not eq 52 }
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,20 @@
1
+ describe Deadlift::Barbell::Default do
2
+
3
+ subject{described_class.new(params)}
4
+
5
+ let(:params) { {} }
6
+
7
+ context '#approved?' do
8
+
9
+ specify {
10
+ expect(subject.approved?).to be_truthy
11
+ subject.errors.add(:some_property, 'property is not valid')
12
+ expect(subject.approved?).to be_falsey
13
+ }
14
+ end
15
+
16
+ context 'setting values' do
17
+ let(:params) { { test: :value } }
18
+ specify { expect(subject.test).to eq :value }
19
+ end
20
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe SomeDefaultBarbell do
4
+
5
+ subject { described_class.new(params) }
6
+
7
+ describe '#perform' do
8
+ let(:trainer) { DeadliftHelpers::PerformanceSpy.new }
9
+
10
+ context 'valid params' do
11
+ let(:params) { { some_value: 1 } }
12
+
13
+ specify {
14
+ subject.add_trainer(trainer).perform
15
+ expect(trainer.deadlift).to eq :some_default_barbell
16
+ expect(trainer.state).to eq :success
17
+ expect(trainer.performance.result.some_value).to eq params[:some_value]
18
+ }
19
+ end
20
+
21
+ context 'invalid params' do
22
+ let(:params) { { some_value: 2 } }
23
+
24
+ specify {
25
+ subject.add_trainer(trainer).perform
26
+ expect(trainer.deadlift).to eq :some_default_barbell
27
+ expect(trainer.state).to eq :failure
28
+ expect(trainer.performance.barbell.errors.count).to eq 1
29
+ }
30
+ end
31
+ end
32
+ end
33
+
34
+ # The other when using proper barbell and deadlift validations
35
+ describe SomeDeadlift do
36
+
37
+ subject { described_class.new(params) }
38
+
39
+ describe '#perform' do
40
+ let(:trainer) { DeadliftHelpers::PerformanceSpy.new }
41
+
42
+ context 'valid params' do
43
+ let(:params) { { id: 1 } }
44
+ specify {
45
+ expect { subject.add_trainer(trainer).perform}.to raise_error(SomeDeadlift::SomeDeadliftError)
46
+ }
47
+ end
48
+
49
+ context 'invalid params' do
50
+ let(:params) { { id: nil } }
51
+
52
+ specify {
53
+ subject.add_trainer(trainer).perform
54
+ expect(trainer.deadlift).to eq :some_deadlift
55
+ expect(trainer.state).to eq :failure
56
+ }
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,20 @@
1
+ describe Deadlift::Performance do
2
+
3
+ subject { described_class.new(barbell, result, deadlift) }
4
+
5
+ let(:barbell) { 'barbell' }
6
+ let(:result) { 'result' }
7
+ let(:deadlift) { 'deadlift' }
8
+
9
+ context '#barbell' do
10
+ specify { expect(subject.barbell).to eq barbell }
11
+ end
12
+
13
+ context '#result' do
14
+ specify { expect(subject.result).to eq result }
15
+ end
16
+
17
+ context '#deadlift' do
18
+ specify { expect(subject.deadlift).to eq deadlift }
19
+ end
20
+ end
@@ -0,0 +1,37 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
5
+ require 'deadlift'
6
+
7
+ Dir[File.join(__dir__, "support/**/*.rb")].each { |f| require f }
8
+
9
+ RSpec.configure do |config|
10
+ # rspec-expectations config goes here. You can use an alternate
11
+ # assertion/expectation library such as wrong or the stdlib/minitest
12
+ # assertions if you prefer.
13
+ config.expect_with :rspec do |expectations|
14
+ # This option will default to `true` in RSpec 4. It makes the `description`
15
+ # and `failure_message` of custom matchers include text for helper methods
16
+ # defined using `chain`, e.g.:
17
+ # be_bigger_than(2).and_smaller_than(4).description
18
+ # # => "be bigger than 2 and smaller than 4"
19
+ # ...rather than:
20
+ # # => "be bigger than 2"
21
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
22
+ end
23
+
24
+ # rspec-mocks config goes here. You can use an alternate test double
25
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
26
+ config.mock_with :rspec do |mocks|
27
+ # Prevents you from mocking or stubbing a method that does not exist on
28
+ # a real object. This is generally recommended, and will default to
29
+ # `true` in RSpec 4.
30
+ mocks.verify_partial_doubles = true
31
+ end
32
+
33
+ config.fail_fast = true
34
+
35
+ config.filter_run focus: true
36
+ config.run_all_when_everything_filtered = true
37
+ end
@@ -0,0 +1,88 @@
1
+ module DeadliftHelpers
2
+ class SuccessCaller
3
+
4
+ def initialize(arguments)
5
+ @barbell = OpenStruct.new(arguments)
6
+ @has_performed = false
7
+ end
8
+
9
+ def add_trainer(trainer)
10
+ @trainers ||= []
11
+ @trainers << trainer
12
+ self
13
+ end
14
+
15
+ def has_performed?
16
+ @has_performed
17
+ end
18
+
19
+ def perform
20
+ @has_performed = true
21
+ @trainers.each do |trainer|
22
+ trainer.public_send :success, Deadlifts::Performance.new(@barbell, nil, nil)
23
+ end
24
+ end
25
+ end
26
+
27
+ class FailureCaller
28
+
29
+ def initialize(arguments)
30
+ @barbell = Deadlifts::DefaultBarbell.new(arguments)
31
+ @barbell.errors.add(:base, :invalid)
32
+ @has_performed = false
33
+ end
34
+
35
+ def add_trainer(trainer)
36
+ @trainers ||= []
37
+ @trainers << trainer
38
+ self
39
+ end
40
+
41
+ def has_performed?
42
+ @has_performed
43
+ end
44
+
45
+ def perform
46
+ @has_performed = true
47
+ @trainers.each do |trainer|
48
+ trainer.public_send :failure, Deadlifts::Performance.new(@barbell, nil, nil, user: SystemUser.new)
49
+ end
50
+ end
51
+ end
52
+
53
+
54
+ class PerformanceSpy
55
+ attr_reader :performance, :state, :deadlift, :successes, :failures, :progresses
56
+
57
+ def initialize
58
+ @successes = 0
59
+ @failures = 0
60
+ @progresses = 0
61
+ end
62
+
63
+ def success(performance)
64
+ @deadlift = performance.deadlift
65
+ @performance = performance
66
+ @state = :success
67
+ @successes+=1
68
+ end
69
+
70
+ def failure(performance)
71
+ @deadlift = performance.deadlift
72
+ @performance = performance
73
+ @state = :failure
74
+ @failures+=1
75
+ end
76
+
77
+ def progress(performance)
78
+ @deadlift = performance.deadlift
79
+ @performance = performance
80
+ @state = :progress
81
+ @progresses+=1
82
+ end
83
+
84
+ def result
85
+ @performance.result
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,10 @@
1
+ class DeadliftWithAFriendBarbell < Deadlift::Barbell::Base
2
+
3
+ property :weight
4
+ validates :weight, presence: true
5
+
6
+ property :friend do
7
+ property :name
8
+ validates :name, presence: true
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ class SomeBarbell < Deadlift::Barbell::Base
2
+ property :weight
3
+ validates :weight, presence: true
4
+ end
@@ -0,0 +1,18 @@
1
+ class SomeDeadlift < Deadlift::Base
2
+ validate :deadlift_validation
3
+
4
+ def self.barbell(params)
5
+ ValidateIdPresenceBarbell.new(params)
6
+ end
7
+
8
+ def perform
9
+ perform_and_report_for_valid do
10
+ end
11
+ end
12
+
13
+ def deadlift_validation
14
+ raise SomeDeadliftError
15
+ end
16
+
17
+ class SomeDeadliftError < StandardError; end
18
+ end
@@ -0,0 +1,19 @@
1
+ class SomeDefaultBarbell < Deadlift::Base
2
+ validate :deadlift_validation
3
+
4
+ def perform
5
+ perform_and_report_for_valid do
6
+ result.some_value = barbell.some_value
7
+ end
8
+ end
9
+
10
+ def result
11
+ @result ||= OpenStruct.new
12
+ end
13
+
14
+ def deadlift_validation
15
+ if barbell.some_value == 2
16
+ barbell.errors.add(:base, :invalid)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ class ValidateIdPresenceBarbell < Deadlift::Barbell::Base
2
+ property :id
3
+ validates :id, presence: true
4
+ end
metadata CHANGED
@@ -1,22 +1,202 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deadlift
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
- - Karl metum
7
+ - Karl Metum
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-16 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2015-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: reform
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.5
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.5
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.5
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.5
33
+ - !ruby/object:Gem::Dependency
34
+ name: activemodel
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 4.2.0
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 4.2.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 4.2.0
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 4.2.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: hashugar
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.0'
60
+ type: :runtime
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '1.0'
67
+ - !ruby/object:Gem::Dependency
68
+ name: bundler
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '1.10'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '1.10'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rake
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '10.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '10.0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: pry
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: 0.10.3
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: 0.10.3
109
+ - !ruby/object:Gem::Dependency
110
+ name: rspec
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '3.3'
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - "~>"
121
+ - !ruby/object:Gem::Version
122
+ version: '3.3'
123
+ - !ruby/object:Gem::Dependency
124
+ name: guard
125
+ requirement: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: '2.13'
130
+ type: :development
131
+ prerelease: false
132
+ version_requirements: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - "~>"
135
+ - !ruby/object:Gem::Version
136
+ version: '2.13'
137
+ - !ruby/object:Gem::Dependency
138
+ name: guard-rspec
139
+ requirement: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - "~>"
142
+ - !ruby/object:Gem::Version
143
+ version: '4.6'
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: 4.6.4
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - "~>"
152
+ - !ruby/object:Gem::Version
153
+ version: '4.6'
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: 4.6.4
13
157
  description: Deadlift is a powerful gem that shrinkens your fat code
14
- email: karl.metum@codehacker.se
15
- executables: []
158
+ email:
159
+ - karl.metum@codehacker.se
160
+ executables:
161
+ - _guard-core
162
+ - console
163
+ - guard
164
+ - setup
16
165
  extensions: []
17
166
  extra_rdoc_files: []
18
167
  files:
168
+ - ".gitignore"
169
+ - ".rspec"
170
+ - ".travis.yml"
171
+ - CODE_OF_CONDUCT.md
172
+ - Gemfile
173
+ - Guardfile
174
+ - LICENSE.txt
175
+ - README.md
176
+ - Rakefile
177
+ - bin/_guard-core
178
+ - bin/console
179
+ - bin/guard
180
+ - bin/setup
181
+ - deadlift.gemspec
19
182
  - lib/deadlift.rb
183
+ - lib/deadlift/barbell/base.rb
184
+ - lib/deadlift/barbell/default.rb
185
+ - lib/deadlift/base.rb
186
+ - lib/deadlift/performance.rb
187
+ - lib/deadlift/version.rb
188
+ - spec/deadlift_spec.rb
189
+ - spec/lib/deadlift/barbell/base_spec.rb
190
+ - spec/lib/deadlift/barbell/default_spec.rb
191
+ - spec/lib/deadlift/base_spec.rb
192
+ - spec/lib/deadlift/performance_spec.rb
193
+ - spec/spec_helper.rb
194
+ - spec/support/deadlift_helpers.rb
195
+ - spec/support/dummies/deadlift_with_a_friend_barbell.rb
196
+ - spec/support/dummies/some_barbell.rb
197
+ - spec/support/dummies/some_deadlift.rb
198
+ - spec/support/dummies/some_default_barbell.rb
199
+ - spec/support/dummies/validate_id_presence_barbell.rb
20
200
  homepage: https://github.com/karlingen/deadlift
21
201
  licenses:
22
202
  - MIT
@@ -41,4 +221,16 @@ rubygems_version: 2.4.8
41
221
  signing_key:
42
222
  specification_version: 4
43
223
  summary: A gem that shrinkens your fat controller
44
- test_files: []
224
+ test_files:
225
+ - spec/deadlift_spec.rb
226
+ - spec/lib/deadlift/barbell/base_spec.rb
227
+ - spec/lib/deadlift/barbell/default_spec.rb
228
+ - spec/lib/deadlift/base_spec.rb
229
+ - spec/lib/deadlift/performance_spec.rb
230
+ - spec/spec_helper.rb
231
+ - spec/support/deadlift_helpers.rb
232
+ - spec/support/dummies/deadlift_with_a_friend_barbell.rb
233
+ - spec/support/dummies/some_barbell.rb
234
+ - spec/support/dummies/some_deadlift.rb
235
+ - spec/support/dummies/some_default_barbell.rb
236
+ - spec/support/dummies/validate_id_presence_barbell.rb