Skip to content

Commit

Permalink
Fix #52; add ability to introspect attributes
Browse files Browse the repository at this point in the history
This is not a clean solution. This commit should be considered a spike.
  • Loading branch information
tfausak committed Oct 8, 2013
1 parent 1bc5b0a commit af1d577
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/active_interaction/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -154,13 +166,24 @@ 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)
end
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
Expand Down
1 change: 1 addition & 0 deletions lib/active_interaction/filter_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit af1d577

Please sign in to comment.