use_case_flow 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1c5a199dc5fdf12df3044f1720005325a61dcc9f
4
+ data.tar.gz: d48040e007384cd9e7fc0c308cf42d5ea6e2049f
5
+ SHA512:
6
+ metadata.gz: 1ac58aa0b07cf4f89393ef11a27e42024a8797012c897018aa5e68921ac4f25901c34016b79b518ff7cbdbb83700cc4b5640b26ad7952bac1f948f286e3603e0
7
+ data.tar.gz: f2754caf7f6da7774b5f39e70efe53879bf4a3472902f2383492a0bd3269e8d76d670a19f1e2dbff9ab00503d45ca40e5181a8c9aeb85b85fc3ace7057bfc814
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+ Exclude:
4
+ - 'bin/**'
5
+ - 'vendor/**/*'
6
+ - 'example/*'
7
+ - 'use_case_flow.gemspec'
8
+ Documentation:
9
+ Enabled: false
10
+ Metrics/LineLength:
11
+ Max: 120
12
+ Metrics/BlockLength:
13
+ Exclude:
14
+ - 'spec/**/*'
15
+ Metrics/MethodLength:
16
+ Max: 15
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.6
6
+ script:
7
+ - bundle exec rubocop
8
+ - bundle exec rspec
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at galaspiotrek@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in use_case_flow.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Piotr
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.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ [![Build Status](https://travis-ci.org/piotr-galas/use_case_flow.svg?branch=master)](https://travis-ci.org/piotr-galas/use_case_flow)
2
+ # UseCaseFlow
3
+
4
+ This gem provide easy and readable way to invoke services/use_cases in your controller
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'use_case_flow'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install use_case_flow, require: false
21
+
22
+ ## Intention
23
+
24
+ Controllers should take care only on things related to http, like `redirect`, `render` , `request`. `response` and so on. Writing code became much easier when we invoke our models inside service/use_cases
25
+ instead in controllers. Imagine code where you invoke some service and then based on result you render special template, or redirect to url and nothing more. From practical point of view we always have one success path
26
+ and few failures. This gem allow very readable syntax for that case.
27
+
28
+
29
+
30
+ ## Usage
31
+ To create service you must:
32
+ - `require use_case_flow`
33
+ - Method you invoke in controller MUST return instance of Success or Failure
34
+ - As example i use `return` `if` syntax because it is easiest way to demonstrate how doest it work. You can use other technics but method must return `Success` or `Failure` instance
35
+
36
+ ```ruby
37
+
38
+ `require use_case_flow`
39
+
40
+ class SomeService
41
+ def call # you can use different name of method
42
+ return Failure.new(:name_what_was_wrong, 'data put to controller') if semething_go_wrong
43
+ return Failure.new(:email_is_empty, {}) if empty_email?
44
+ return Failure.new(:user_not_found, User.new ) if user?
45
+ Success.new('data put to controller')
46
+ end
47
+ end
48
+
49
+ class UserController
50
+
51
+ def create
52
+ result = SomeService.new.call
53
+ # order of line below have no matter
54
+ result.name_what_was_wrong { |data_from_service| render :template, json: data_from_service }
55
+ result.email_is_empty { |data_from_service| redirect_to 'www.example.com' }
56
+ result.user_not_found {|new_user| render json: new_user, status: 422}
57
+ result.success{ |text| render plain: text}
58
+ end
59
+ end
60
+ ```
61
+ example usage from real project, it shows how your controller could looks
62
+
63
+ - [controller](https://github.com/piotr-galas/use_case_flow/blob/master/example/auction_controller.rb)
64
+ - [service](https://github.com/piotr-galas/use_case_flow/blob/master/example/archive.rb)
65
+
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. Then, run `rubocop` to run the rubocop ,You can also run `bin/console` for an interactive prompt that will allow you to experiment.
69
+
70
+ 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).
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/piotr-galas/use_case_flow. 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.
75
+
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'use_case_flow'
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require use_case_flow
4
+
5
+ module Auctions
6
+ module Standard
7
+ class Archive
8
+ def initialize(params, user)
9
+ @params = params
10
+ @user = user
11
+ end
12
+
13
+ def call
14
+ return Failure.new(:not_found, errors: :not_found) if auction.nil?
15
+ return Failure.new(:unauthorized, errors: :not_authorized) unless authorized?
16
+ return Failure.new(:not_allowed, errors: :it_is_not_published_auction) unless auction.published?
17
+ archive_auction
18
+ Success.new(auction)
19
+ end
20
+
21
+ private
22
+
23
+ def authorized?
24
+ auction.user == @user && @user.verified_success?
25
+ end
26
+
27
+ def auction
28
+ @auction ||= Auction.find_by(id: @params[:id])
29
+ end
30
+
31
+ def archive_auction
32
+ if auction.update(status: :archived)
33
+ auction.proposals.pending.each do |proposal|
34
+ Account::Proposals::Update.new({ id: proposal.id, update_action: 'end_auction' }, auction.user).execute
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,65 @@
1
+ class FrontApi::V1::Auction::StandardsController < FrontApi::BaseController
2
+ before_action do
3
+ self.class.serialization_scope :current_front_api_v1_user
4
+ end
5
+
6
+ def create
7
+ result = Auctions::Standard::Create.new(auction_params, current_front_api_v1_user).execute
8
+ result.success { |auction| render json: auction, status: :created }
9
+ result.locked { |errors| render json: errors, status: :locked }
10
+ result.invalid { |errors| render json: errors, status: :unprocessable_entity }
11
+ result.invalid_api { |errors| render json: errors, status: :service_unavailable }
12
+ end
13
+
14
+ def index
15
+ result = Auctions::Standard::Index.new(search_params, current_front_api_v1_user).auction_list_objects
16
+ result.success { |auction_list_objects| render json: auction_list_objects, status: :ok }
17
+ end
18
+
19
+ def show
20
+ result = Auctions::Standard::Show.new(params, current_front_api_v1_user).call
21
+ result.success { |auction| render json: auction, status: :ok }
22
+ result.not_found { |errors| render json: errors, status: :not_found }
23
+ result.not_allowed { |errors| render json: errors, status: :forbidden }
24
+ end
25
+
26
+ def update
27
+ result = Auctions::Standard::Update.new(auction_params, current_front_api_v1_user).call
28
+ result.success { |auction| render json: auction, status: :ok }
29
+ result.locked { |errors| render json: errors, status: :locked }
30
+ result.unauthorized { |errors| render json: errors, status: :unauthorized }
31
+ result.not_found { |errors| render json: errors, status: :not_found }
32
+ result.not_allowed { |errors| render json: errors, status: :forbidden }
33
+ result.invalid { |errors| render json: errors, status: :unprocessable_entity }
34
+ end
35
+
36
+ def renew
37
+ result = Auctions::Standard::Renew.new(auction_params, current_front_api_v1_user).call
38
+ result.success { render json: {}, status: :ok }
39
+ result.not_found { |errors| render json: errors, status: :not_found }
40
+ result.invalid_duration { |errors| render json: errors, status: :unprocessable_entity }
41
+ result.not_published { |errors| render json: errors, status: :forbidden }
42
+ result.unauthorized { |errors| render json: errors, status: :unauthorized }
43
+ result.locked { |errors| render json: errors, status: :locked }
44
+ result.invalid_api { |errors| render json: errors, status: :service_unavailable }
45
+ end
46
+
47
+ def archive
48
+ result = Auctions::Standard::Archive.new(auction_params, current_front_api_v1_user).call
49
+ result.success { |auction| render json: auction, status: :ok }
50
+ result.unauthorized { |errors| render json: errors, status: :unauthorized }
51
+ result.not_found { |errors| render json: errors, status: :not_found }
52
+ result.not_allowed { |errors| render json: errors, status: :forbidden }
53
+ result.invalid { |errors| render json: errors, status: :unprocessable_entity }
54
+ end
55
+
56
+ private
57
+
58
+ def search_params
59
+ params.permit(:query, :category)
60
+ end
61
+
62
+ def auction_params
63
+ params.permit(:id, :status)
64
+ end
65
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'use_case_flow/version'
4
+ require 'use_case_flow/result'
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UseCaseFlow
4
+ class Result
5
+ attr_reader :name
6
+
7
+ def initialize(name, *args)
8
+ @name = name
9
+ @args = args
10
+ @was_called = false
11
+
12
+ define_singleton_method(name) do |&block|
13
+ block.call(*@args)
14
+ @was_called = true
15
+ end
16
+
17
+ define_singleton_method("#{name}?") do
18
+ true
19
+ end
20
+ end
21
+
22
+ def else
23
+ yield unless @was_called
24
+ end
25
+
26
+ def else_fail!
27
+ self.else { raise "unhandled Result '#{@name}' with args: #{@args.inspect}" }
28
+ end
29
+
30
+ # rubocop:disable Style/MethodMissing
31
+ def method_missing(_method_name, *_args, &_block)
32
+ false
33
+ end
34
+
35
+ def values
36
+ @args
37
+ end
38
+
39
+ def value
40
+ @args.first
41
+ end
42
+ end
43
+ end
44
+
45
+ class Success < UseCaseFlow::Result
46
+ def initialize(*args)
47
+ super(:success, *args)
48
+ end
49
+ end
50
+
51
+ class Failure < UseCaseFlow::Result
52
+ def failure?
53
+ true
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UseCaseFlow
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'use_case_flow/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'use_case_flow'
10
+ spec.version = UseCaseFlow::VERSION
11
+ spec.authors = ['Piotr']
12
+ spec.email = ['galaspiotrek@gmail.com']
13
+
14
+ spec.summary = 'It provide readable control flow for use cases / services'
15
+ spec.description = 'You can use it in controller as a layer betwen controller and model'
16
+ spec.homepage = 'https://github.com/piotr-galas/use_case_flow'
17
+ spec.license = 'MIT'
18
+
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
21
+ f.match(%r{^(test|spec|features)/})
22
+ end
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.14'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'rubocop', '~> 0.49.1'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: use_case_flow
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Piotr
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-23 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.49.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.49.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: You can use it in controller as a layer betwen controller and model
70
+ email:
71
+ - galaspiotrek@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - example/archive.rb
88
+ - example/auction_controller.rb
89
+ - lib/use_case_flow.rb
90
+ - lib/use_case_flow/result.rb
91
+ - lib/use_case_flow/version.rb
92
+ - use_case_flow.gemspec
93
+ homepage: https://github.com/piotr-galas/use_case_flow
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.6.11
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: It provide readable control flow for use cases / services
117
+ test_files: []