Created
July 13, 2021 03:03
-
-
Save gastonmorixe/8c1b068aacf0ab87959526ec964599e9 to your computer and use it in GitHub Desktop.
active_interaction custom filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "minitest/matchers" | |
require "minitest/autorun" | |
require "active_interaction" | |
module ActiveInteraction | |
class Base | |
end | |
class MixedFilter < Filter | |
register :mixed | |
def allow(&check) | |
@check = check | |
end | |
private | |
def matches?(value) | |
@check.call(value) | |
rescue NoMethodError | |
false | |
end | |
end | |
end | |
class Square < ActiveInteraction::Base | |
float :x | |
mixed :y do | |
allow do |v| | |
[Integer, String].any? { |t| v.is_a?(t) } | |
end | |
end | |
def execute | |
"#{x*2} - #{y}" | |
end | |
end | |
describe Square do | |
it "works" do | |
outcome = Square.run(x: 2.5, y: "a") | |
assert_equal outcome.result, "5.0 - a" | |
assert_equal outcome.valid?, true | |
outcome = Square.run(x: 2.5, y: 100) | |
assert_equal outcome.result, "5.0 - 100" | |
assert_equal outcome.valid?, true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment