base_service 0.0.2
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 +7 -0
- data/Gemfile +3 -0
- data/Guardfile +8 -0
- data/MIT-LICENSE +20 -0
- data/README.md +65 -0
- data/lib/base_service.rb +39 -0
- data/lib/base_service/failure.rb +10 -0
- data/lib/base_service/matcher.rb +41 -0
- data/lib/base_service/version.rb +3 -0
- metadata +163 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: 3c9c6bd5cbf220fa4f8ae5d2cabb042364a43689
         | 
| 4 | 
            +
              data.tar.gz: 49000c5cbadefd476c34b45a5ec65f34b3f70639
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: 3afd229bbe5efe2a7db89cd9fe650a63d63b3b1b003ff2f9d710edcc3c826f233e338892d6b53707d617276a47169dff9a2b56bf42cebdb2588bdd5cfdc0f0d2
         | 
| 7 | 
            +
              data.tar.gz: 2b471bbd8907163752921336c182294da92d2fffcc9081b3c3b40cee45437f7e03bbbec18e91bcceaff284b1723efa0d579956fe46443bf7f2c1622d5421c495
         | 
    
        data/Gemfile
    ADDED
    
    
    
        data/Guardfile
    ADDED
    
    
    
        data/MIT-LICENSE
    ADDED
    
    | @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            Copyright (c) 2011, 2012, 2013 by Grouper
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Permission is hereby granted, free of charge, to any person obtaining
         | 
| 4 | 
            +
            a copy of this software and associated documentation files (the
         | 
| 5 | 
            +
            "Software"), to deal in the Software without restriction, including
         | 
| 6 | 
            +
            without limitation the rights to use, copy, modify, merge, publish,
         | 
| 7 | 
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         | 
| 8 | 
            +
            permit persons to whom the Software is furnished to do so, subject to
         | 
| 9 | 
            +
            the following conditions:
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            The above copyright notice and this permission notice shall be
         | 
| 12 | 
            +
            included in all copies or substantial portions of the Software.
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         | 
| 15 | 
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         | 
| 16 | 
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         | 
| 17 | 
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         | 
| 18 | 
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         | 
| 19 | 
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         | 
| 20 | 
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         | 
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,65 @@ | |
| 1 | 
            +
            BaseService
         | 
| 2 | 
            +
            ===========
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            The client should extend this module, in order to implement this common
         | 
| 5 | 
            +
            service object pattern.
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            ### Advanced Syntax
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            ```ruby
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            MyService.new(param1, param2).call do |on|
         | 
| 12 | 
            +
              on.success { |result| puts "Success #{result}" }
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              on.failure { |error| puts "Generic failure #{error}" }
         | 
| 15 | 
            +
              on.failure(:param1_error) { |error| puts "Problem with param1 #{param1}" }
         | 
| 16 | 
            +
              on.failure(:param2_error) { |error| puts "Problem with param1 #{param1}" }
         | 
| 17 | 
            +
            end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            ```
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            ### Semi-Advanced Syntax
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            ```ruby
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            MyService.new(param1, param2).call do |on|
         | 
| 26 | 
            +
              puts "Result: #{on.result}"
         | 
| 27 | 
            +
            end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            ```
         | 
| 30 | 
            +
             | 
| 31 | 
            +
            ## Support for (depreacted) legacy behaviour
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            No command-query separation. :(
         | 
| 34 | 
            +
             | 
| 35 | 
            +
            ```ruby
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            result = MyService.new(param1, param2).call
         | 
| 38 | 
            +
             | 
| 39 | 
            +
            ```
         | 
| 40 | 
            +
             | 
| 41 | 
            +
            ## Example Service
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            ```ruby
         | 
| 44 | 
            +
             | 
| 45 | 
            +
            class MyService
         | 
| 46 | 
            +
              include BaseService
         | 
| 47 | 
            +
             | 
| 48 | 
            +
              def initialize(param1, param2)
         | 
| 49 | 
            +
                @param1, @param2 = param1, param2
         | 
| 50 | 
            +
              end
         | 
| 51 | 
            +
             | 
| 52 | 
            +
              private
         | 
| 53 | 
            +
              attr_reader :param1, :param2
         | 
| 54 | 
            +
             | 
| 55 | 
            +
              def perform
         | 
| 56 | 
            +
                return failure "no param1", :param1_error  unless param1.present?
         | 
| 57 | 
            +
                return failure "no param2", :param2_error  unless param2.present?
         | 
| 58 | 
            +
                return failure "neither param1 or 2" unless param1.present? && param2.present?
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                return "success!"
         | 
| 61 | 
            +
              end
         | 
| 62 | 
            +
            end
         | 
| 63 | 
            +
             | 
| 64 | 
            +
            ```
         | 
| 65 | 
            +
             | 
    
        data/lib/base_service.rb
    ADDED
    
    | @@ -0,0 +1,39 @@ | |
| 1 | 
            +
            require 'active_support'
         | 
| 2 | 
            +
            require 'active_support/core_ext/object'
         | 
| 3 | 
            +
            require 'base_service/matcher'
         | 
| 4 | 
            +
            require 'base_service/failure'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            module BaseService
         | 
| 7 | 
            +
              def call
         | 
| 8 | 
            +
                perform
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                if block_given?
         | 
| 11 | 
            +
                  self.matcher = Matcher.new self
         | 
| 12 | 
            +
                  yield matcher
         | 
| 13 | 
            +
                  matcher.resolve
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                result
         | 
| 17 | 
            +
              end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
              attr_reader :result, :was_success, :failure_code
         | 
| 20 | 
            +
              alias was_success? was_success
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              private
         | 
| 23 | 
            +
             | 
| 24 | 
            +
              attr_writer :was_success, :result, :failure_code
         | 
| 25 | 
            +
              attr_accessor :matcher
         | 
| 26 | 
            +
             | 
| 27 | 
            +
              def success(result)
         | 
| 28 | 
            +
                self.was_success = true
         | 
| 29 | 
            +
                self.result = result
         | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
              def failure(result, code = nil)
         | 
| 33 | 
            +
                self.was_success = false
         | 
| 34 | 
            +
                self.result = result
         | 
| 35 | 
            +
                self.failure_code = code
         | 
| 36 | 
            +
              end
         | 
| 37 | 
            +
            end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
            require 'base_service/version'
         | 
| @@ -0,0 +1,41 @@ | |
| 1 | 
            +
            module BaseService
         | 
| 2 | 
            +
              class Matcher
         | 
| 3 | 
            +
                def initialize(service)
         | 
| 4 | 
            +
                  @service, @specific_failure_blocks = service, {}
         | 
| 5 | 
            +
                end
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                def success(&block)
         | 
| 8 | 
            +
                  self.generic_success_block = block
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                def failure(code = nil, &block)
         | 
| 12 | 
            +
                  if code.nil?
         | 
| 13 | 
            +
                    self.generic_failure_block = block
         | 
| 14 | 
            +
                  else
         | 
| 15 | 
            +
                    specific_failure_blocks[code] = block
         | 
| 16 | 
            +
                  end
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                def resolve
         | 
| 20 | 
            +
                  return appropriate_failure_block.call result if !was_success?
         | 
| 21 | 
            +
                  generic_success_block.call result if generic_success_block.present?
         | 
| 22 | 
            +
                end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                attr_reader :service, :specific_failure_blocks
         | 
| 25 | 
            +
                attr_accessor :generic_success_block, :generic_failure_block
         | 
| 26 | 
            +
                delegate :result, :was_success?, :failure_code, to: :service
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                def failure_handled?
         | 
| 29 | 
            +
                  generic_failure_block.present? || specific_failure_blocks[failure_code].present?
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                def appropriate_failure_block
         | 
| 33 | 
            +
                  specific_failure_blocks[failure_code] || generic_failure_block || raise_failure_not_handled
         | 
| 34 | 
            +
                end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                def raise_failure_not_handled
         | 
| 37 | 
            +
                  raise Failure.new(service, result, failure_code)
         | 
| 38 | 
            +
                end
         | 
| 39 | 
            +
              end
         | 
| 40 | 
            +
            end
         | 
| 41 | 
            +
             | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,163 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: base_service
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.0.2
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Brett Richardson
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2016-06-24 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies:
         | 
| 13 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 14 | 
            +
              name: activesupport
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 | 
            +
                requirements:
         | 
| 17 | 
            +
                - - "~>"
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: '4.0'
         | 
| 20 | 
            +
              type: :runtime
         | 
| 21 | 
            +
              prerelease: false
         | 
| 22 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 | 
            +
                requirements:
         | 
| 24 | 
            +
                - - "~>"
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            +
                    version: '4.0'
         | 
| 27 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 28 | 
            +
              name: bundler
         | 
| 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: rspec-its
         | 
| 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: rake
         | 
| 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 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 84 | 
            +
              name: guard
         | 
| 85 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 86 | 
            +
                requirements:
         | 
| 87 | 
            +
                - - ">="
         | 
| 88 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 89 | 
            +
                    version: '0'
         | 
| 90 | 
            +
              type: :development
         | 
| 91 | 
            +
              prerelease: false
         | 
| 92 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 93 | 
            +
                requirements:
         | 
| 94 | 
            +
                - - ">="
         | 
| 95 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 96 | 
            +
                    version: '0'
         | 
| 97 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 98 | 
            +
              name: guard-rspec
         | 
| 99 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 100 | 
            +
                requirements:
         | 
| 101 | 
            +
                - - ">="
         | 
| 102 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 103 | 
            +
                    version: '0'
         | 
| 104 | 
            +
              type: :development
         | 
| 105 | 
            +
              prerelease: false
         | 
| 106 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 107 | 
            +
                requirements:
         | 
| 108 | 
            +
                - - ">="
         | 
| 109 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 110 | 
            +
                    version: '0'
         | 
| 111 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 112 | 
            +
              name: pry
         | 
| 113 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 114 | 
            +
                requirements:
         | 
| 115 | 
            +
                - - ">="
         | 
| 116 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 117 | 
            +
                    version: '0'
         | 
| 118 | 
            +
              type: :development
         | 
| 119 | 
            +
              prerelease: false
         | 
| 120 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 121 | 
            +
                requirements:
         | 
| 122 | 
            +
                - - ">="
         | 
| 123 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 124 | 
            +
                    version: '0'
         | 
| 125 | 
            +
            description: Template for service objects in ruby
         | 
| 126 | 
            +
            email:
         | 
| 127 | 
            +
            - brett.richardson.nz@gmail.com
         | 
| 128 | 
            +
            executables: []
         | 
| 129 | 
            +
            extensions: []
         | 
| 130 | 
            +
            extra_rdoc_files: []
         | 
| 131 | 
            +
            files:
         | 
| 132 | 
            +
            - Gemfile
         | 
| 133 | 
            +
            - Guardfile
         | 
| 134 | 
            +
            - MIT-LICENSE
         | 
| 135 | 
            +
            - README.md
         | 
| 136 | 
            +
            - lib/base_service.rb
         | 
| 137 | 
            +
            - lib/base_service/failure.rb
         | 
| 138 | 
            +
            - lib/base_service/matcher.rb
         | 
| 139 | 
            +
            - lib/base_service/version.rb
         | 
| 140 | 
            +
            homepage: http://www.mebrett.com
         | 
| 141 | 
            +
            licenses: []
         | 
| 142 | 
            +
            metadata: {}
         | 
| 143 | 
            +
            post_install_message: 
         | 
| 144 | 
            +
            rdoc_options: []
         | 
| 145 | 
            +
            require_paths:
         | 
| 146 | 
            +
            - lib
         | 
| 147 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 148 | 
            +
              requirements:
         | 
| 149 | 
            +
              - - ">="
         | 
| 150 | 
            +
                - !ruby/object:Gem::Version
         | 
| 151 | 
            +
                  version: '0'
         | 
| 152 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 153 | 
            +
              requirements:
         | 
| 154 | 
            +
              - - ">="
         | 
| 155 | 
            +
                - !ruby/object:Gem::Version
         | 
| 156 | 
            +
                  version: '0'
         | 
| 157 | 
            +
            requirements: []
         | 
| 158 | 
            +
            rubyforge_project: 
         | 
| 159 | 
            +
            rubygems_version: 2.4.5.1
         | 
| 160 | 
            +
            signing_key: 
         | 
| 161 | 
            +
            specification_version: 4
         | 
| 162 | 
            +
            summary: Base Service for Ruby
         | 
| 163 | 
            +
            test_files: []
         |