Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip step in_sequence #39

Open
sphynx79 opened this issue Oct 19, 2017 · 1 comment
Open

skip step in_sequence #39

sphynx79 opened this issue Oct 19, 2017 · 1 comment

Comments

@sphynx79
Copy link

sphynx79 commented Oct 19, 2017

Hi. I have a question about in_sequence!.

require 'deterministic'

class Foo
  include Deterministic::Prelude
  
  def call(id: nil)
    result = in_sequence do
      get(:id)        { get_id(id) }
      get(:ctx)       { id == 10 ? Success(id+1) : Failure(1) } }
      get(:ctx)       { Success(id+2) }
      and_yield       { format_response(ctx) }
    end
  end

  def get_id(id)
    id = 10
    Success(id)
  end

  def format_response(id)
    Success("Your id is #{id}")
  end

end

Foo.new.call(id: 10)

My question is how i can skip one step in sequence, in this simple example i want skip the step:

get(:ctx)       { Success(id+2) }

and return me 11, there is one way to integrate Pattern matching and where with in_sequence

Thanks, and congratulation for your gem.

@deiwin
Copy link
Contributor

deiwin commented Nov 9, 2017

I'm not exactly sure what you're asking, but I'll assume that you want to know how to skip only a single step instead of skipping all the following steps as Failure does. If that's you're question, then I'd suggest doing something like:

require 'deterministic'

class Foo
  include Deterministic::Prelude
  
  def call(id: nil)
    result = in_sequence do
      get(:id)  { get_id(id) }
      get(:ctx) { get_ctx(id) }
      and_yield { format_response(ctx) }
    end
  end

  def get_id(id)
    Success(id)
  end

  def get_ctx(id)
    if id == 10
      in_sequence do
        get(:ctx) { Success(id + 1) }
        and_yield { Success(ctx + 2) } # or id + 2 as you have it
      end
    else
      Success(1)
    end
  end

  def format_response(id)
    Success("Your id is #{id}")
  end

end

Foo.new.call(id: 10)

That is, you can't use Failure, because that will abort the entire chain. You can use standard Ruby if/else, however, and nest Result chains within each other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants