class Cranium::TestFramework::CucumberTable

Public Class Methods

from_ast_table(ast_table) click to toggle source
# File lib/cranium/test_framework/cucumber_table.rb, line 3
def self.from_ast_table(ast_table)
  column_types, hashes = process_ast_table ast_table
  new remove_comment_columns(hashes), column_types
end
new(array_of_hashes, column_types = {}) click to toggle source
# File lib/cranium/test_framework/cucumber_table.rb, line 10
def initialize(array_of_hashes, column_types = {})
  @pattern_replacements, @data, @column_types = {}, array_of_hashes, column_types
end

Private Class Methods

column_type(type_specifier) click to toggle source
# File lib/cranium/test_framework/cucumber_table.rb, line 119
def self.column_type(type_specifier)
  case type_specifier
    when "i"
      :integer
    when "n"
      :numeric
    when "s", nil
      :string
    else
      raise StandardError, "Invalid type specified: #{type_specifier}"
  end
end
comment_field?(field_name) click to toggle source
# File lib/cranium/test_framework/cucumber_table.rb, line 113
def self.comment_field?(field_name)
  field_name.to_s.start_with? "#"
end
process_ast_table(ast_table) click to toggle source
# File lib/cranium/test_framework/cucumber_table.rb, line 97
def self.process_ast_table(ast_table)
  column_types = {}
  ast_table = ast_table.map_headers do |header|
    header.match /^(?<name>.*?)(\s+\((?<type>\w{1,2})\))?$/ do |match|
      match[:name].to_sym.tap do |field_name|
        next if comment_field? field_name
        column_types[field_name] = column_type match[:type]
      end
    end
  end

  return column_types, ast_table.hashes
end
remove_comment_columns(hashes) click to toggle source
# File lib/cranium/test_framework/cucumber_table.rb, line 134
def self.remove_comment_columns(hashes)
  hashes.map do |hash|
    hash.delete_if { |key| key.to_s.start_with? "#" }
  end
end

Public Instance Methods

accept(_) click to toggle source
# File lib/cranium/test_framework/cucumber_table.rb, line 28
def accept(_)
end
data() click to toggle source
# File lib/cranium/test_framework/cucumber_table.rb, line 39
def data
  column_count = @column_types.count

  evaluate_cells.tap do |array_of_hashes|
    array_of_hashes.define_singleton_method(:columns) do

      result = self.reduce(Hash.new { |hash, key| hash[key] = [] }) do |result, current_hash|
        current_hash.each { |key, value| result[key] << value }
        result
      end.values

      result == [] ? Array.new(column_count) { [] } : result
    end
  end
end
data_array() click to toggle source
# File lib/cranium/test_framework/cucumber_table.rb, line 57
def data_array
  data.map { |hash| hash.values.first }
end
fields() click to toggle source
# File lib/cranium/test_framework/cucumber_table.rb, line 33
def fields
  @data.first.keys
end
to_step_definition_arg() click to toggle source
# File lib/cranium/test_framework/cucumber_table.rb, line 22
def to_step_definition_arg
  dup
end
with_patterns(pattern_replacements) click to toggle source
# File lib/cranium/test_framework/cucumber_table.rb, line 16
def with_patterns(pattern_replacements)
  self.tap { @pattern_replacements = pattern_replacements }
end

Private Instance Methods

evaluate_cells() click to toggle source
# File lib/cranium/test_framework/cucumber_table.rb, line 65
def evaluate_cells
  @data.map do |row|
    Hash.new.tap do |evaluated_row|

      row.each do |key, value|
        evaluated_value = evaluate_field(key, value)
        evaluated_row[key] = evaluated_value
      end
    end
  end

end
evaluate_field(key, value) click to toggle source
# File lib/cranium/test_framework/cucumber_table.rb, line 80
def evaluate_field(key, value)
  if @pattern_replacements.keys.include? value
    (@pattern_replacements[value].is_a? Proc) ? @pattern_replacements[value].() : @pattern_replacements[value]
  else
    case @column_types[key]
      when :integer
        value.to_i
      when :numeric
        BigDecimal.new value
      else
        value
    end
  end
end