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?(value, from) click to toggle source
# File lib/active_interaction/filters/interface_filter.rb, line 68
def checking_class_inheritance?(value, from)
  value.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?(value) click to toggle source
# File lib/active_interaction/filters/interface_filter.rb, line 50
def matches?(value)
  return false if value == nil # rubocop:disable Style/NilComparison
  return matches_methods?(value) if options.key?(:methods)

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