Module: TableStepsHelper::ArrayMethods

Defined in:
lib/spreewald/table_steps.rb

Instance Method Summary collapse

Instance Method Details

#character(code) ⇒ Object

Only cross-Ruby way to get a character for its UTF-16 (hex) representation



30
31
32
# File 'lib/spreewald/table_steps.rb', line 30

def character(code)
  [code.to_i(16)].pack('U*')
end

#find_row(expected_row) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/spreewald/table_steps.rb', line 9

def find_row(expected_row)
  find_index do |row|
    expected_row.all? do |expected_column|
      first_column = row.find_index do |column|
        content = normalize_content(column.content)
        expected_content = normalize_content(expected_column)
        matching_parts = expected_content.split(/\s*\*\s*/, -1).collect { |part| Regexp.escape(part) }
        matching_expression = /\A#{matching_parts.join(".*")}\z/
        content =~ matching_expression
      end
      if first_column.nil?
        false
      else
        row = row[(first_column + 1)..-1]
        true
      end
    end
  end
end

#normalize_content(content) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/spreewald/table_steps.rb', line 34

def normalize_content(content)
  shy = character("00ad") # soft hyphen
  nbsp = character("00a0") # non-breaking space
  zwsp = character("200b") # zero-width space
  content = content.gsub(/[#{shy}#{zwsp}]/, '')
  content = content.gsub(/[\r\n\t#{nbsp}]+/, ' ')
  content = content.gsub(/ {2,}/, ' ')
  content = content.strip
  content
end