Sha256: 4bfad7c3bb66759fe47b7ef5cf530d21ac0683eed8fa54b719edf7e33349b404

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

# encoding: utf-8

module Okei

  # Finds an unit by name:
  #
  #   use_case = GetUnit.new name: "1000 человеко-часов"
  #   use_case.run
  #   # => #<Okei::Unit @code: "ТЫС ЧЕЛ-ЧАС" ...>
  #
  # Any other parameters are ignored.
  #
  # Allows subscription before running:
  #
  #   use_case.subscribe logger
  #   use_case.subscribe controller
  #   use_case.run
  #
  class FindUnit < Hexx::UseCase
    include Wisper::Publisher

    allow_params :text

    # Runs the method:
    #
    # * оn success publishes `found(unit)` and returns an `unit`
    # * on error raises `Hexx::NotFoundError` with `self`
    #
    def run!
      unit_found?
      publish :found, unit
      unit
    end

    private

    def text
      @text ||= params["text"]
    end

    def code
      @code ||= Corrector::Parse.new text, scope: "okei"
    end

    def unit
      @unit ||= Unit.where(code: code).first
    end

    def unit_found?
      return if unit
      errors.add :text, :not_found
      fail Hexx::NotFoundError.new self
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
okei-1.0.2 app/use_cases/okei/find_unit.rb
okei-1.0.1 app/use_cases/okei/find_unit.rb
okei-1.0.0 app/use_cases/okei/find_unit.rb
okei-1.0.0.pre.rc app/use_cases/okei/find_unit.rb