Class: CucumberAnalytics::Example

Inherits:
FeatureElement show all
Includes:
Containing, Taggable
Defined in:
lib/cucumber_analytics/example.rb

Overview

A class modeling a Cucumber Examples table.

Instance Attribute Summary collapse

Attributes included from Taggable

#tag_elements, #tags

Attributes inherited from FeatureElement

#description, #description_text, #name

Attributes included from Nested

#parent_element

Attributes included from Raw

#raw_element

Attributes included from Sourceable

#source_line

Instance Method Summary collapse

Methods included from Taggable

#all_tag_elements, #all_tags, #applied_tag_elements, #applied_tags

Methods included from Nested

#get_ancestor

Constructor Details

#initialize(source = nil) ⇒ Example

Creates a new Example object and, if source is provided, populates the object.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cucumber_analytics/example.rb', line 29

def initialize(source = nil)
  parsed_example = process_source(source)

  super(parsed_example)

  @tags = []
  @tag_elements = []
  @rows = []
  @parameters = []
  @row_elements = []

  build_example(parsed_example) if parsed_example
end

Instance Attribute Details

#parametersObject

The parameters for the example table

todo - Make this a read only method that derives the parameters from the row elements



21
22
23
# File 'lib/cucumber_analytics/example.rb', line 21

def parameters
  @parameters
end

#row_elementsObject

The row elements in the example table



24
25
26
# File 'lib/cucumber_analytics/example.rb', line 24

def row_elements
  @row_elements
end

#rowsObject

The argument rows in the example table

todo - Make this a read only method that derives the rows from the row elements



15
16
17
# File 'lib/cucumber_analytics/example.rb', line 15

def rows
  @rows
end

Instance Method Details

#add_row(row) ⇒ Object

Adds a row to the example table. The row can be given as a Hash of parameters and their corresponding values or as an Array of values which will be assigned in order.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cucumber_analytics/example.rb', line 46

def add_row(row)
  case
    when row.is_a?(Array)
      @rows << Hash[@parameters.zip(row.collect { |value| value.strip })]
      @row_elements << Row.new("|#{row.join('|')}|")
    when row.is_a?(Hash)
      @rows << row.each_value { |value| value.strip! }
      @row_elements << Row.new("|#{ordered_row_values(row).join('|')}|")
    else
      raise(ArgumentError, "Can only add row from a Hash or an Array but received #{row.class}")
  end
end

#containsObject

Returns the immediate child elements of the element.



78
79
80
# File 'lib/cucumber_analytics/example.rb', line 78

def contains
  @row_elements
end

#remove_row(row) ⇒ Object

Removes a row from the example table. The row can be given as a Hash of parameters and their corresponding values or as an Array of values which will be assigned in order.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cucumber_analytics/example.rb', line 62

def remove_row(row)
  case
    when row.is_a?(Array)
      location = @rows.index { |row_hash| row_hash.values_at(*@parameters) == row.collect { |value| value.strip } }
    when row.is_a?(Hash)
      location = @rows.index { |row_hash| row_hash == row.each_value { |value| value.strip! } }
    else
      raise(ArgumentError, "Can only remove row from a Hash or an Array but received #{row.class}")
  end

  #todo - remove once Hash rows are no longer supported
  @rows.delete_at(location) if location
  @row_elements.delete_at(location + 1) if location
end

#to_sObject

Returns a gherkin representation of the example.



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cucumber_analytics/example.rb', line 83

def to_s
  text = ''

  text << tag_output_string + "\n" unless tags.empty?
  text << "Examples:#{name_output_string}"
  text << "\n" + description_output_string unless description_text.empty?
  text << "\n" unless description_text.empty?
  text << "\n" + parameters_output_string
  text << "\n" + rows_output_string unless rows.empty?

  text
end