Skip to content

Instantly share code, notes, and snippets.

@rickychilcott
Created May 5, 2025 20:53
Show Gist options
  • Save rickychilcott/366f64dccab8a1338086c3c2923ce19e to your computer and use it in GitHub Desktop.
Save rickychilcott/366f64dccab8a1338086c3c2923ce19e to your computer and use it in GitHub Desktop.
ActiveInteraction w/ Literal
require "literal"
require "active_interaction"
class ActiveInteractionTest < ActiveInteraction::Base
string :full_name
string :email, default: -> { "#{full_name}@developer.com" }
def execute = "#{full_name} has an email of #{email}!"
end
class LiteralInteractionTest < ActiveInteraction::Base
extend Literal::Properties
prop :full_name, _String, reader: :public
prop :email, _String?, default: -> { "#{full_name}@developer.com" }, reader: :public
def execute = "#{full_name} has an email of #{email}!"
end
# Shim to get Literal::Properties to work with ActiveInteraction. I think...
module ActiveInteraction::Runnable::ClassMethods
def new(*args, **kwargs, &block)
if ancestors.include?(Literal::Properties::DocString)
super(**kwargs)
else
super
end.tap do |instance|
{
:@_interaction_errors => ActiveInteraction::Errors.new(instance),
:@_interaction_result => nil
}.each do |symbol, obj|
instance.instance_variable_set(symbol, obj)
end
end
end
def run(...)
new(...).tap { |instance| instance.send(:run) }
end
def run!(...)
new(...).send(:run!)
end
end
puts ActiveInteractionTest.run!(full_name: "John", email: "[email protected]")
puts ActiveInteractionTest.run!(full_name: "John")
puts LiteralInteractionTest.run!(full_name: "John", email: "[email protected]") # fails beccause `full_name` is not callable from default
puts LiteralInteractionTest.run!(full_name: "John")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment