Module: Yarr::Command::Concern::Responder::KlassMethods

Defined in:
lib/yarr/command/concern/responder.rb

Overview

DSL to define response method

Instance Method Summary collapse

Instance Method Details

#define_multi_item_responder(&block) {|dataset| ... } ⇒ Object

Defines multi item dataset responder

When the dataset is empty our response would report back that to the user, otherwise the client code defines what the response should be.

Parameters:

  • block (Proc)

    user hook to transform the dataset to response string

Yield Parameters:

  • dataset (Array)

    query result


84
85
86
87
88
89
90
91
# File 'lib/yarr/command/concern/responder.rb', line 84

def define_multi_item_responder(&block)
  define_method(:response) do |dataset|
    case dataset.count
    when 0 then zero_response
    else block[dataset]
    end
  end
end

#define_single_item_responder(&block) {|dataset| ... } ⇒ Object

Defines a single item dataset responder

When the dataset is empty or has many items our response would report back that to the user, otherwise the client code defines what the response should be.

Parameters:

  • block (Proc)

    user hook to transform the dataset to response string

Yield Parameters:

  • dataset (Array)

    query result


65
66
67
68
69
70
71
72
73
74
# File 'lib/yarr/command/concern/responder.rb', line 65

def define_single_item_responder(&block)
  define_method(:response) do |dataset|
    count = dataset.count
    case count
    when 0 then zero_response
    when 1 then block[dataset]
    else multi_response(count)
    end
  end
end