Skip to content

Instantly share code, notes, and snippets.

@gastonmorixe
Created July 13, 2021 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gastonmorixe/8c1b068aacf0ab87959526ec964599e9 to your computer and use it in GitHub Desktop.
Save gastonmorixe/8c1b068aacf0ab87959526ec964599e9 to your computer and use it in GitHub Desktop.
active_interaction custom filter
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