Class: Omnizip::FilterPipeline

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

Overview

Pipeline for chaining multiple filters together.

Filters are applied in sequence during encoding, and in reverse order during decoding. Position tracking is maintained across the entire pipeline.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFilterPipeline

Initialize an empty filter pipeline.



29
30
31
32
# File 'lib/omnizip/filter_pipeline.rb', line 29

def initialize
  @filters = []
  @position = 0
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



26
27
28
# File 'lib/omnizip/filter_pipeline.rb', line 26

def filters
  @filters
end

Instance Method Details

#add_filter(filter) ⇒ self

Add a filter to the pipeline.

Filters are applied in the order they are added during encoding, and in reverse order during decoding.

Parameters:

Returns:

  • (self)

    For method chaining



41
42
43
44
# File 'lib/omnizip/filter_pipeline.rb', line 41

def add_filter(filter)
  @filters << filter
  self
end

#clearvoid

This method returns an undefined value.

Clear all filters from the pipeline.



103
104
105
106
# File 'lib/omnizip/filter_pipeline.rb', line 103

def clear
  @filters.clear
  @position = 0
end

#decode(data, position = 0) ⇒ String

Decode (postprocess) data by applying all filters in reverse order.

Filters are applied in reverse order with the same position value to undo the encoding transformation.

Parameters:

  • data (String)

    Binary data to decode

  • position (Integer) (defaults to: 0)

    Current stream position

Returns:

  • (String)

    Decoded binary data



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/omnizip/filter_pipeline.rb', line 88

def decode(data, position = 0)
  return data.dup if @filters.empty?

  result = data
  # Apply filters in reverse order
  @filters.reverse_each do |filter|
    result = filter.decode(result, position)
  end

  result
end

#empty?Boolean

Check if pipeline has any filters.

Returns:

  • (Boolean)

    True if pipeline contains filters



49
50
51
# File 'lib/omnizip/filter_pipeline.rb', line 49

def empty?
  @filters.empty?
end

#encode(data, position = 0) ⇒ String

Encode (preprocess) data by applying all filters in order.

Filters are applied sequentially with the same position value. Position represents the current stream position for address calculations.

Parameters:

  • data (String)

    Binary data to encode

  • position (Integer) (defaults to: 0)

    Current stream position

Returns:

  • (String)

    Encoded binary data



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

def encode(data, position = 0)
  return data.dup if @filters.empty?

  result = data
  @filters.each do |filter|
    result = filter.encode(result, position)
  end

  result
end

#sizeInteger

Get number of filters in pipeline.

Returns:

  • (Integer)

    Number of filters



56
57
58
# File 'lib/omnizip/filter_pipeline.rb', line 56

def size
  @filters.size
end