Class: Sycsvpro::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/sycsvpro/filter.rb

Overview

Creates a new filter that can be extended by sub-classes. A sub-class needs to override the process method

Direct Known Subclasses

ColumnFilter, Header, RowFilter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, options = {}) ⇒ Filter

Creates a new filter



20
21
22
23
24
25
26
# File 'lib/sycsvpro/filter.rb', line 20

def initialize(values, options={})
  @date_format = options[:df] || "%Y-%m-%d"
  @filter  = []
  @pattern = []
  @pivot   = {}
  create_filter(values)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object

Creates the filters based on the given patterns



29
30
31
32
33
34
35
36
37
# File 'lib/sycsvpro/filter.rb', line 29

def method_missing(id, *args, &block)
  return equal($1, args, block)              if id =~ /^(\d+)$/
  return range($1, $2, args, block)          if id =~ /^(\d+)-(\d+)$/
  return regex($1, args, block)              if id =~ /^\/(.*)\/$/
  return col_regex($1, $2, args, block)      if id =~ /^(\d+):\/(.*)\/$/
  return date($1, $2, $3, args, block)       if id =~ /^(\d+):(<|=|>)(\d+.\d+.\d+)/
  return date_range($1, $2, $3, args, block) if id =~ /^(\d+):(\d+.\d+.\d+.)-(\d+.\d+.\d+)$/
  super
end

Instance Attribute Details

#date_formatObject (readonly)

When date are used as filters the date format has to be provided



11
12
13
# File 'lib/sycsvpro/filter.rb', line 11

def date_format
  @date_format
end

#filterObject (readonly)

Filter for rows and columns



13
14
15
# File 'lib/sycsvpro/filter.rb', line 13

def filter
  @filter
end

#patternObject (readonly)

Pattern that is used as a filter



15
16
17
# File 'lib/sycsvpro/filter.rb', line 15

def pattern
  @pattern
end

#pivotObject (readonly)

Comparison that is used as a filter



17
18
19
# File 'lib/sycsvpro/filter.rb', line 17

def pivot
  @pivot
end

Instance Method Details

#has_filter?Boolean

Checks whether a filter has been set. Returns true if filter has been set otherwise false

Returns:

  • (Boolean)


59
60
61
# File 'lib/sycsvpro/filter.rb', line 59

def has_filter?
  return !(filter.empty? and pattern.empty?)
end

#pivot_each_column(values = []) ⇒ Object

Yields the column value and whether the filter matches the column



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sycsvpro/filter.rb', line 45

def pivot_each_column(values=[])
  pivot.each do |column, parameters|
    value = values[parameters[:col].to_i]
    value = value.strip.gsub(/^"|"$/, "") unless value.nil?
    match = false
    begin
      match = eval(parameters[:operation].gsub('[value]', value))
    rescue
    end
    yield column, match
  end
end

#process(object, options = {}) ⇒ Object

Processes the filter. Needs to be overridden by the sub-class



40
41
42
# File 'lib/sycsvpro/filter.rb', line 40

def process(object, options={})
  raise 'Needs to be overridden by sub class'
end