Class: Omnizip::Extraction::FilterChain

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/extraction/filter_chain.rb

Overview

Combines multiple pattern matchers with AND/OR logic

Supports include and exclude patterns, allowing complex filtering scenarios like "all .rb files except in test/".

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFilterChain

Initialize a new filter chain



14
15
16
17
18
19
# File 'lib/omnizip/extraction/filter_chain.rb', line 14

def initialize
  @include_patterns = []
  @exclude_patterns = []
  @include_predicates = []
  @exclude_predicates = []
end

Instance Attribute Details

#exclude_patternsObject (readonly)

Returns the value of attribute exclude_patterns.



10
11
12
# File 'lib/omnizip/extraction/filter_chain.rb', line 10

def exclude_patterns
  @exclude_patterns
end

#exclude_predicatesObject (readonly)

Returns the value of attribute exclude_predicates.



10
11
12
# File 'lib/omnizip/extraction/filter_chain.rb', line 10

def exclude_predicates
  @exclude_predicates
end

#include_patternsObject (readonly)

Returns the value of attribute include_patterns.



10
11
12
# File 'lib/omnizip/extraction/filter_chain.rb', line 10

def include_patterns
  @include_patterns
end

#include_predicatesObject (readonly)

Returns the value of attribute include_predicates.



10
11
12
# File 'lib/omnizip/extraction/filter_chain.rb', line 10

def include_predicates
  @include_predicates
end

Instance Method Details

#any?Boolean

Check if chain has any conditions

Returns:

  • (Boolean)


90
91
92
93
94
95
# File 'lib/omnizip/extraction/filter_chain.rb', line 90

def any?
  !@include_patterns.empty? ||
    !@exclude_patterns.empty? ||
    !@include_predicates.empty? ||
    !@exclude_predicates.empty?
end

#countInteger

Get count of all conditions

Returns:

  • (Integer)


100
101
102
103
104
105
# File 'lib/omnizip/extraction/filter_chain.rb', line 100

def count
  @include_patterns.size +
    @exclude_patterns.size +
    @include_predicates.size +
    @exclude_predicates.size
end

#exclude {|entry| ... } ⇒ self

Add an exclude predicate

Yields:

  • (entry)

    Entry to test

Yield Returns:

  • (Boolean)

    Whether to exclude entry

Returns:

  • (self)


54
55
56
57
# File 'lib/omnizip/extraction/filter_chain.rb', line 54

def exclude(&predicate)
  @exclude_predicates << predicate if predicate
  self
end

#exclude_pattern(pattern) ⇒ self

Add an exclude pattern

Parameters:

  • pattern (String, Regexp, Proc)

    Pattern to exclude

Returns:

  • (self)


34
35
36
37
# File 'lib/omnizip/extraction/filter_chain.rb', line 34

def exclude_pattern(pattern)
  @exclude_patterns << PatternMatcher.new(pattern)
  self
end

#filter(entries) ⇒ Array

Filter an array of entries

Parameters:

  • entries (Array)

    Entries to filter

Returns:

  • (Array)

    Matching entries



83
84
85
# File 'lib/omnizip/extraction/filter_chain.rb', line 83

def filter(entries)
  entries.select { |entry| match?(entry) }
end

#include {|entry| ... } ⇒ self

Add an include predicate

Yields:

  • (entry)

    Entry to test

Yield Returns:

  • (Boolean)

    Whether to include entry

Returns:

  • (self)


44
45
46
47
# File 'lib/omnizip/extraction/filter_chain.rb', line 44

def include(&predicate)
  @include_predicates << predicate if predicate
  self
end

#include_pattern(pattern) ⇒ self

Add an include pattern

Parameters:

  • pattern (String, Regexp, Proc)

    Pattern to include

Returns:

  • (self)


25
26
27
28
# File 'lib/omnizip/extraction/filter_chain.rb', line 25

def include_pattern(pattern)
  @include_patterns << PatternMatcher.new(pattern)
  self
end

#match?(entry, filename: nil) ⇒ Boolean

Check if an entry matches the filter chain

Logic:

  • If no includes, everything passes (except excludes)
  • If includes exist, entry must match at least one include
  • Entry must not match any exclude

Parameters:

  • entry (Object)

    Entry to check (filename or entry object)

  • filename (String, nil) (defaults to: nil)

    Explicit filename (if entry is object)

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
# File 'lib/omnizip/extraction/filter_chain.rb', line 69

def match?(entry, filename: nil)
  name = filename || extract_filename(entry)

  # Check excludes first (faster to reject)
  return false if excluded?(entry, name)

  # Check includes
  included?(entry, name)
end