Sha256: 53fd623ad61c060fc13f333977365f2994522cdf97f1296bb04525c86aebe71b

Contents?: true

Size: 841 Bytes

Versions: 5

Compression:

Stored size: 841 Bytes

Contents

# encoding: utf-8

module Okei

  # Finds all units:
  #
  #   use_case = GetUnits.new
  #   use_case.run
  #   # => list of all units
  #
  # ...or units of given measure (in Russian):
  #
  #   use_case = GetUnits.new measure: "ДЛИНА"
  #   use_case.run
  #   # => list of units of length
  #
  # Any other parameters are ignored.
  #
  # Allows subscription before running:
  #
  #   use_case.subscribe logger
  #   use_case.subscribe controller
  #   use_case.run
  #
  class GetUnits < Hexx::UseCase

    allow_params :measure

    # Publishes `success(units)` and returns a list of units.
    def run!
      publish :success, units
      units
    end

    private

    def units
      @units ||= (show_all? ? Unit.all : Unit.where(params)).order(:code)
    end

    def show_all?
      params["measure"].blank?
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

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