diff --git a/lib/active_interaction/base.rb b/lib/active_interaction/base.rb index 9f49aa94..3ba0f944 100644 --- a/lib/active_interaction/base.rb +++ b/lib/active_interaction/base.rb @@ -98,6 +98,18 @@ def execute raise NotImplementedError end + # TODO: Documentation + def self.attributes + # TODO: Add nested attributes. + @filter_methods.map do |fm| + { + name: fm.attribute, + options: fm.options, + type: fm.method_name + } + end + end + # @private def self.transaction return unless block_given? @@ -154,6 +166,9 @@ def self.method_missing(type, *args, &block) options = args.last.is_a?(Hash) ? args.pop : {} args.each do |attribute| + # REVIEW: This should probably be outside the loop, but FilterMethod + # doesn't like multiple attributes. + add_attribute(type, *[attribute, options], &block) set_up_reader(attribute, filter, options, &block) set_up_writer(attribute, filter, options, &block) set_up_validator(attribute, type, filter, options, &block) @@ -161,6 +176,14 @@ def self.method_missing(type, *args, &block) end private_class_method :method_missing + # @private + def self.add_attribute(type, *args, &block) + # REVIEW: @filter_methods is not an instance of FilterMethods. + @filter_methods ||= [] + @filter_methods.push(FilterMethod.new(type, *args, &block)) + end + private_class_method :add_attribute + # @private def self.set_up_reader(attribute, filter, options, &block) default = nil diff --git a/lib/active_interaction/filter_method.rb b/lib/active_interaction/filter_method.rb index 4c2506ad..fe299ba0 100644 --- a/lib/active_interaction/filter_method.rb +++ b/lib/active_interaction/filter_method.rb @@ -6,6 +6,7 @@ class FilterMethod def initialize(method_name, *args, &block) @method_name, @block = method_name, block + # TODO: What if there are multiple attributes? @attribute = args.shift if args.first.is_a?(Symbol) @options = (args.first || {}).dup end