Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelramsay committed Sep 24, 2016
0 parents commit 6dbcce5
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sudo: false
language: ruby
rvm:
- 2.2.2
before_install: gem install bundler -v 1.13.1
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in use_case_pattern.gemspec
gemspec
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# UseCasePattern

A module that helps you to implement the Use Case design pattern.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'use_case_pattern'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install use_case_pattern

## Usage

TODO: Write usage instructions here

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

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).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/abletech/use_case_pattern.
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "use_case_pattern"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
8 changes: 8 additions & 0 deletions lib/use_case_pattern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "use_case_pattern/version"
require "use_case_pattern/base"

module UseCasePattern
def self.included(receiver)
receiver.send :include, UseCasePattern::Base
end
end
43 changes: 43 additions & 0 deletions lib/use_case_pattern/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "active_support"
require "active_model"

module UseCasePattern
module Base
extend ActiveSupport::Concern
include ActiveModel::Validations

module ClassMethods
# The perform method of a UseCase should always return itself
def perform(*args)
new(*args).tap { |use_case| use_case.perform }
end

# raise an exception if perform generates any errors
def perform!(*args)
new(*args).tap { |use_case| use_case.perform! }
end
end

# implement all the steps required to complete this use case
def perform
raise NotImplementedError
end

def perform!
perform

if failure?
raise "Unexpected error #{errors.full_messages.join(', ')}"
end
end

# inside of perform, add errors if the use case did not succeed
def success?
errors.none?
end

def failure?
errors.any?
end
end
end
3 changes: 3 additions & 0 deletions lib/use_case_pattern/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module UseCasePattern
VERSION = "1.0.0"
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require "use_case_pattern"
43 changes: 43 additions & 0 deletions spec/use_case_pattern/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "spec_helper"

describe UseCasePattern::Base do
describe "#perform" do
context "when the including class has defined a perform method" do
it "should call the perform method" do
expect(SimpleUseCase).to receive(:perform)
SimpleUseCase.perform
end
end

context "when the including class has defined a perform method and initializer" do
subject(:use_case){ UseCaseWithConstructor.perform(10) }

it "should call the initializer" do
expect(UseCaseWithConstructor).to receive(:new).with(10).and_call_original
use_case
end

it "should have no errors" do
expect(use_case.errors.full_messages).to eq([])
end
end
end
end

class SimpleUseCase
include UseCasePattern

def perform
end
end

class UseCaseWithConstructor
include UseCasePattern

def initialize(args)
@args = args
end

def perform
end
end
7 changes: 7 additions & 0 deletions spec/use_case_pattern_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "spec_helper"

describe UseCasePattern do
it "has a version number" do
expect(UseCasePattern::VERSION).not_to be nil
end
end
31 changes: 31 additions & 0 deletions use_case_pattern.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'use_case_pattern/version'

Gem::Specification.new do |spec|
spec.name = "use_case_pattern"
spec.version = UseCasePattern::VERSION
spec.authors = ["Nigel Ramsay"]
spec.email = ["[email protected]"]

spec.summary = "A module that helps you to implement the Use Case design pattern"
spec.description = "A module that helps you to implement the Use Case design pattern"
spec.homepage = "https://github.com/abletech/use_case_pattern"

spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end

spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.required_ruby_version = '~> 2.0'

spec.add_runtime_dependency "activemodel", [">= 4.0.0", "~> 5.0.0"]
spec.add_runtime_dependency "activesupport", [">= 4.0.0", "~> 5.0.0"]

spec.add_development_dependency "bundler", "~> 1.13"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
end

0 comments on commit 6dbcce5

Please sign in to comment.