Skip to content

Commit

Permalink
use InputProcessor in Base
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronLasseigne committed Apr 29, 2014
1 parent e1487d5 commit 60d0d02
Showing 1 changed file with 4 additions and 29 deletions.
33 changes: 4 additions & 29 deletions lib/active_interaction/base.rb
Expand Up @@ -31,9 +31,6 @@ class Base
include ActiveModelable
include Runnable

GROUPED_INPUT_PATTERN = /\A(.+)\((\d+)i\)\z/
private_constant :GROUPED_INPUT_PATTERN

validate :input_errors

class << self
Expand Down Expand Up @@ -129,7 +126,7 @@ def method_missing(*args, &block)
# @param name [Symbol]
# @param options [Hash]
def add_filter(klass, name, options, &block)
fail InvalidFilterError, name.inspect if reserved?(name)
fail InvalidFilterError, name.inspect if InputProcessor.reserved?(name)

initialize_filter(klass.new(name, options, &block))
end
Expand Down Expand Up @@ -171,13 +168,6 @@ def initialize_filter(filter)

filter.default if filter.default?
end

# @param symbol [Symbol]
#
# @return [Boolean]
def reserved?(symbol)
symbol.to_s.start_with?('_interaction_')
end
end

# @param inputs [Hash{Symbol => Object}] Attribute values to set.
Expand Down Expand Up @@ -248,33 +238,18 @@ def inputs
# @param inputs [Hash{Symbol => Object}]
def process_inputs(inputs)
inputs.each do |key, value|
fail_if_reserved(key)
fail InvalidValueError, key.inspect if InputProcessor.reserved?(key)

populate_reader(key, value)
end

populate_filters(merge_inputs(inputs))
end

def fail_if_reserved(key)
fail InvalidValueError, key.inspect if self.class.send(:reserved?, key)
populate_filters(InputProcessor.process(inputs))
end

def populate_reader(key, value)
instance_variable_set("@#{key}", value) if respond_to?(key)
end

def merge_inputs(inputs)
inputs.sort.each_with_object({}) do |(k, v), h|
if (match = GROUPED_INPUT_PATTERN.match(k))
k, i = match.captures

(h[k.to_sym] ||= GroupedInput.new)[i] = v
else
h[k] = v
end
end
end

def populate_filters(inputs)
self.class.filters.each do |name, filter|
begin
Expand Down

0 comments on commit 60d0d02

Please sign in to comment.