class ActiveInteraction::InterfaceFilter

@private

Public Class Methods

new(name, options = {}, &block) click to toggle source
Calls superclass method ActiveInteraction::Filter::new
# File lib/active_interaction/filters/interface_filter.rb, line 31
def initialize(name, options = {}, &block)
  if options.key?(:methods) && options.key?(:from)
    raise InvalidFilterError,
      'method and from options cannot both be passed'
  end

  super
end

Private Instance Methods

checking_class_inheritance?(object, from) click to toggle source
# File lib/active_interaction/filters/interface_filter.rb, line 68
def checking_class_inheritance?(object, from)
  object.is_a?(Class) && from.is_a?(Class)
end
class_inherits_from?(klass, inherits_from) click to toggle source
# File lib/active_interaction/filters/interface_filter.rb, line 72
def class_inherits_from?(klass, inherits_from)
  klass != inherits_from && klass.ancestors.include?(inherits_from)
end
from() click to toggle source
# File lib/active_interaction/filters/interface_filter.rb, line 42
def from
  const_name = options.fetch(:from, name).to_s.camelize
  Object.const_get(const_name)
rescue NameError
  raise InvalidNameError,
    "constant #{const_name.inspect} does not exist"
end
matches?(object) click to toggle source
# File lib/active_interaction/filters/interface_filter.rb, line 50
def matches?(object)
  return false if object.nil?
  return matches_methods?(object) if options.key?(:methods)

  const = from
  if checking_class_inheritance?(object, const)
    class_inherits_from?(object, const)
  else
    singleton_ancestor?(object, const)
  end
rescue NoMethodError
  false
end
matches_methods?(object) click to toggle source
# File lib/active_interaction/filters/interface_filter.rb, line 64
def matches_methods?(object)
  options[:methods].all? { |method| object.respond_to?(method) }
end
singleton_ancestor?(object, from) click to toggle source
# File lib/active_interaction/filters/interface_filter.rb, line 76
def singleton_ancestor?(object, from)
  object.class != from && object.singleton_class.ancestors.include?(from)
end