Class: Sycsvpro::Header

Inherits:
Filter
  • Object
show all
Includes:
Dsl
Defined in:
lib/sycsvpro/header.rb

Overview

Creates a header

Constant Summary

Constants included from Dsl

Dsl::COMMA_SPLITTER_REGEX

Instance Attribute Summary collapse

Attributes inherited from Filter

#boolean_filter, #date_format, #filter, #pattern, #pivot, #types

Instance Method Summary collapse

Methods included from Dsl

#clean_up, #params, #rows, #split_by_comma_regex, #str2utf8, #unstring, #write_to

Methods inherited from Filter

#has_filter?, #match_boolean_filter?, #pivot_each_column

Constructor Details

#initialize(header, options = {}) ⇒ Header

Create a new header



22
23
24
25
26
27
# File 'lib/sycsvpro/header.rb', line 22

def initialize(header, options = {})
  @header_cols = split_by_comma_regex(header || "")
  @insert_cols = (options[:insert] || "").split(/,|;/)
  @positions   = options[:pos] || []
  @sort        = options[:sort]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



29
30
31
32
# File 'lib/sycsvpro/header.rb', line 29

def method_missing(id, *args, &block)
  return @row_cols[$1.to_i] if id =~ /^c(\d+)$/
  super
end

Instance Attribute Details

#header_colsObject (readonly)

Header columns



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

def header_cols
  @header_cols
end

#insert_colsObject (readonly)

Columns that will be inserted into the header at the defined positions



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

def insert_cols
  @insert_cols
end

#positionsObject (readonly)

Positions where to insert the insert_cols



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

def positions
  @positions
end

#sortObject (readonly)

Columns position from that on the header will be sorted



19
20
21
# File 'lib/sycsvpro/header.rb', line 19

def sort
  @sort
end

Instance Method Details

#clear_header_colsObject

Returns @header_cols without pattern. Will be sorted if sorted is not nil



66
67
68
69
70
71
72
73
# File 'lib/sycsvpro/header.rb', line 66

def clear_header_cols
  clear_header = @header_cols.select { |col| col !~ /^\(?c\d+[=~+]{,2}/ }
  if @sort
    clear_header.slice!(0, @sort.to_i) + clear_header.sort
  else
    clear_header
  end
end

#column_of(value) ⇒ Object

Returns the index of the column



76
77
78
# File 'lib/sycsvpro/header.rb', line 76

def column_of(value)
  clear_header_cols.index(value)
end

#process(line, values = true) ⇒ Object

Returns the header



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sycsvpro/header.rb', line 35

def process(line, values = true)
  return "" if @header_cols.empty? && @insert_cols.empty?
  header_patterns = {}
  @row_cols = unstring(line).split(';')
  if @header_cols[0] == '*'
    @header_cols[0] = @row_cols
  else
    @header_cols.each_with_index do |h,i|
      if h =~ /^\(?c\d+(?:[=~]{,2}).*$/ 
        if col = eval(h)
          last_eval = $1
          unless @header_cols.index(last_eval) || @header_cols.index(col)
            if values
              @header_cols[i] = (h =~ /^\(?c\d+=~/) ? last_eval : col
              header_patterns[i+1] = h if h =~ /^\(?c\d+[=~+-.]{1,2}/
            else
              @header_cols[i] = col if h =~ /^\(?c\d+$/
            end
          end
        end
      else
        @header_cols[i] = h
      end
    end
  end
  insert_header_cols
  header_patterns.each { |i,h| @header_cols.insert(i,h) }
  to_s
end

#to_sObject

Returns the header



86
87
88
# File 'lib/sycsvpro/header.rb', line 86

def to_s
  clear_header_cols.join(';')
end

#value_of(column) ⇒ Object

Returns the value of column number



81
82
83
# File 'lib/sycsvpro/header.rb', line 81

def value_of(column)
  clear_header_cols[column]
end