Skip to content

Instantly share code, notes, and snippets.

@tfausak
Created September 3, 2014 18:11
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 tfausak/c8dbc5c4890d1400116e to your computer and use it in GitHub Desktop.
Save tfausak/c8dbc5c4890d1400116e to your computer and use it in GitHub Desktop.
diff --git a/lib/active_interaction/base.rb b/lib/active_interaction/base.rb
index 64c70c3..9f5f61d 100644
--- a/lib/active_interaction/base.rb
+++ b/lib/active_interaction/base.rb
@@ -168,7 +168,18 @@ module ActiveInteraction
attr_accessor filter.name
define_method("#{filter.name}?") { !public_send(filter.name).nil? }
- filter.default if filter.default?
+ initialize_default(filter)
+ end
+
+ def initialize_default(filter)
+ return unless filter.default?
+
+ default = filter.options.fetch(:default)
+ return filter.default unless default.is_a?(Proc)
+
+ filter.options[:default] = nil
+ callback = -> { send("#{filter.name}=", instance_exec(&default)) }
+ set_callback(:type_check, :after, callback)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment