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

Get values of filters in match block #19

Open
feruzoripov opened this issue Feb 28, 2024 · 1 comment
Open

Get values of filters in match block #19

feruzoripov opened this issue Feb 28, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@feruzoripov
Copy link

feruzoripov commented Feb 28, 2024

Is it possible to get the actual value of matching objects in the match block?
For example, let's say we have this code:

class SomePlane < Rubanok::Plane
  process :filter do
    match :some_value do
      having true do
        # ...
      end
    end
  end
end

In this case some_value should include true to match the condition. But what if it contains some other value, and we want to use some_value.is_a?(TrueClass) or any other custom condition for some_value to proceed with the having block?
For example:

class SomePlane < Rubanok::Plane
  process :filter do
    match :some_value do |some_value:|
      having some_value.is_a?(TrueClass) || some_value.blank? do
        # ...
      end
    end
  end
end

Maybe it's already possible to use it like this, but I am not sure, it's not included in README, and I tried to write a code like this and it doesn't work.

@feruzoripov feruzoripov added the enhancement New feature or request label Feb 28, 2024
@palkan
Copy link
Owner

palkan commented Mar 5, 2024

The block you pass to the match method is executed at build time (once), it's not executed again during the params processing. It's used to define a schema.

You should probably use map here, if you want to deal with arbitrary values:

map :some_value do |some_value:|
  # Check and apply transformations
end

Or you can add the default clause:

match :some_value do
  having true do
    # ...
  end

  default do |some_value:|
    # ...
  end
end

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

No branches or pull requests

2 participants