class ActiveSet::Filtering::Enumerable::Strategy

Public Class Methods

new(set, attribute_instruction) click to toggle source
# File lib/active_set/filtering/enumerable/strategy.rb, line 20
def initialize(set, attribute_instruction)
  @set = set
  @attribute_instruction = attribute_instruction
  @set_instruction = SetInstruction.new(attribute_instruction, set)
end

Public Instance Methods

execute() click to toggle source
# File lib/active_set/filtering/enumerable/strategy.rb, line 26
def execute
  return false unless @set.respond_to? :select

  if execute_filter_operation?
    set = filter_operation
  elsif execute_intersect_operation?
    begin
      set = intersect_operation
    rescue TypeError # thrown if intersecting with a non-Array
      return false
    end
  else
    return false
  end

  set
end

Private Instance Methods

execute_filter_operation?() click to toggle source
# File lib/active_set/filtering/enumerable/strategy.rb, line 46
def execute_filter_operation?
  return false unless attribute_instance
  return false unless attribute_instance.respond_to?(attribute)
  return false if attribute_instance.method(attribute).arity.positive?

  true
end
execute_intersect_operation?() click to toggle source
# File lib/active_set/filtering/enumerable/strategy.rb, line 54
def execute_intersect_operation?
  return false unless attribute_class
  return false unless attribute_class.respond_to?(attribute)
  return false if attribute_class.method(attribute).arity.zero?

  true
end
filter_operation() click to toggle source
# File lib/active_set/filtering/enumerable/strategy.rb, line 62
def filter_operation
  @set.select do |item|
    @set_instruction.item_matches_query?(item)
  end
end
intersect_operation() click to toggle source
# File lib/active_set/filtering/enumerable/strategy.rb, line 68
def intersect_operation
  @set & other_set
end
other_set() click to toggle source
# File lib/active_set/filtering/enumerable/strategy.rb, line 72
def other_set
  other_set = attribute_class.public_send(
    attribute,
    instruction_value
  )
  if attribute_class != set_item.class
    other_set = begin
                @set.select { |item| resource_for(item: item)&.presence_in other_set }
                rescue ArgumentError # thrown if other_set is doesn't respond to #include?, like when nil
                  nil
              end
  end

  other_set
end