Skip to content

Commit

Permalink
Clean up method_missing and friends
Browse files Browse the repository at this point in the history
In particular, filter_attr_accessor was kind of getting out of hand.

I don't like how the setup helpers all take almost the same arguments.
  • Loading branch information
tfausak committed Jul 15, 2013
1 parent b3642a6 commit 596081b
Showing 1 changed file with 35 additions and 31 deletions.
66 changes: 35 additions & 31 deletions lib/active_interaction/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,57 +106,61 @@ def self.run!(options = {})
end

# @private
def self.method_missing(filter_type, *args, &block)
klass = Filter.factory(filter_type)
def self.method_missing(type, *args, &block)
filter = Filter.factory(type)
options = args.last.is_a?(Hash) ? args.pop : {}
args.each do |attribute|
filter_attr_accessor(klass, attribute, options, &block)

filter_validator(attribute, filter_type, klass, options, &block)
set_up_reader(attribute, filter, options, &block)
set_up_writer(attribute, filter, options, &block)
set_up_validator(attribute, type, filter, options, &block)
end
end
private_class_method :method_missing

# @private
def self.filter_attr_accessor(filter, attribute, options, &block)
filter_attr_writer = "_filter__#{attribute}="
define_method(filter_attr_writer) do |value|
filtered_value =
begin
filter.prepare(attribute, value, options, &block)
rescue InvalidValue, MissingValue
value
end

instance_variable_set("@#{attribute}", filtered_value)
end
private filter_attr_writer

attr_writer attribute

default_value = nil
def self.set_up_reader(attribute, filter, options, &block)
default = nil
if options.has_key?(:default)
begin
default_value = filter.prepare(
attribute, options.delete(:default), options, &block)
default = filter.
prepare(attribute, options.delete(:default), options, &block)
rescue InvalidValue
raise InvalidDefaultValue
end
end

define_method(attribute) do
instance_variable_name = "@#{attribute}"
if instance_variable_defined?(instance_variable_name)
instance_variable_get(instance_variable_name)
symbol = "@#{attribute}"
if instance_variable_defined?(symbol)
instance_variable_get(symbol)
else
default_value
default
end
end
end
private_class_method :filter_attr_accessor
private_class_method :set_up_reader

# @private
def self.set_up_writer(attribute, filter, options, &block)
attr_writer attribute

writer = "_filter__#{attribute}="

define_method(writer) do |value|
value =
begin
filter.prepare(attribute, value, options, &block)
rescue InvalidValue, MissingValue
value
end
instance_variable_set("@#{attribute}", value)
end
private writer
end
private_class_method :set_up_writer

# @private
def self.filter_validator(attribute, type, filter, options, &block)
def self.set_up_validator(attribute, type, filter, options, &block)
validator = "_validate__#{attribute}__#{type}"

validate validator
Expand All @@ -173,6 +177,6 @@ def self.filter_validator(attribute, type, filter, options, &block)
end
private validator
end
private_class_method :filter_validator
private_class_method :set_up_validator
end
end

0 comments on commit 596081b

Please sign in to comment.