Class: CukeModeler::Example

Inherits:
Model
  • Object
show all
Includes:
Described, Named, Parsed, Parsing, Sourceable, Taggable
Defined in:
lib/cuke_modeler/models/example.rb

Overview

A class modeling an example table of an outline.

Instance Attribute Summary collapse

Attributes included from Taggable

#tags

Attributes included from Sourceable

#source_line

Attributes included from Described

#description

Attributes included from Named

#name

Attributes included from Parsed

#parsing_data

Attributes included from Nested

#parent_model

Instance Method Summary collapse

Methods included from Taggable

#all_tags, #applied_tags

Methods included from Parsing

dialects, parse_text

Methods included from Containing

#each_descendant, #each_model

Methods included from Nested

#get_ancestor

Constructor Details

#initialize(source_text = nil) ⇒ Example

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



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cuke_modeler/models/example.rb', line 27

def initialize(source_text = nil)
  @tags = []
  @rows = []

  super(source_text)

  return unless source_text

  parsed_example_data = parse_source(source_text)
  populate_example(self, parsed_example_data)
end

Instance Attribute Details

#keywordObject

The example’s keyword



19
20
21
# File 'lib/cuke_modeler/models/example.rb', line 19

def keyword
  @keyword
end

#rowsObject

The row models in the example table



22
23
24
# File 'lib/cuke_modeler/models/example.rb', line 22

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.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cuke_modeler/models/example.rb', line 42

def add_row(row)
  raise('Cannot add a row. No parameters have been set.') if rows.empty?

  # A quick 'deep clone' so that the input isn't modified
  row = Marshal.load(Marshal.dump(row))

  values = if row.is_a?(Array)
             row
           elsif row.is_a?(Hash)
             # There is no guarantee that the user built up their hash with the keys in the same order as
             # the parameter row and so the values have to be ordered by us. Additionally, the hash needs
             # to have string keys in order for #order_row_values to work
             ordered_row_values(stringify_keys(row))
           else
             raise(ArgumentError, "Can only add row from a Hash or an Array but received #{row.class}")
           end

  @rows << Row.new("|#{values.join('|')}|")
end

#argument_rowsObject

The argument rows in the example table



83
84
85
# File 'lib/cuke_modeler/models/example.rb', line 83

def argument_rows
  rows[1..rows.count] || []
end

#childrenObject

Returns the model objects that belong to this model.



98
99
100
# File 'lib/cuke_modeler/models/example.rb', line 98

def children
  rows + tags
end

#parameter_rowObject

The parameter row for the example table



88
89
90
# File 'lib/cuke_modeler/models/example.rb', line 88

def parameter_row
  rows.first
end

#parametersObject

Returns the parameters of the example table



93
94
95
# File 'lib/cuke_modeler/models/example.rb', line 93

def parameters
  parameter_row ? parameter_row.cells.map(&:value) : []
end

#remove_row(row_removed) ⇒ 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.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cuke_modeler/models/example.rb', line 65

def remove_row(row_removed)
  return if argument_rows.empty?

  values = if row_removed.is_a?(Array)
             row_removed
           elsif row_removed.is_a?(Hash)
             # There is no guarantee that the user built up their hash with the keys in the same order as
             # the parameter row and so the values have to be ordered by us.
             ordered_row_values(row_removed)
           else
             raise(ArgumentError, "Can only remove row from a Hash or an Array but received #{row_removed.class}")
           end

  location = index_for_values(values.map(&:to_s).map(&:strip))
  @rows.delete_at(location + 1) if location
end

#to_sObject

Returns a string representation of this model. For an example model, this will be Gherkin text that is equivalent to the example being modeled.



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cuke_modeler/models/example.rb', line 107

def to_s
  text = ''

  text << "#{tag_output_string}\n" unless tags.empty?
  text << "#{@keyword}:#{name_output_string}"
  text << "\n#{description_output_string}" unless no_description_to_output?
  text << "\n" unless rows.empty? || no_description_to_output?
  text << "\n#{parameters_output_string}" if parameter_row
  text << "\n#{rows_output_string}" unless argument_rows.empty?

  text
end