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_column, #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, #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.

Examples:

Example.new
Example.new("|param_1|param_2|\n|value_1|value_2|")

Parameters:

  • source_text (String) (defaults to: nil)

    The Gherkin text that will be used to populate the model

Raises:

  • (ArgumentError)

    If source_text is not a String



34
35
36
37
38
39
# File 'lib/cuke_modeler/models/example.rb', line 34

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

  super(source_text)
end

Instance Attribute Details

#keywordObject

The example’s keyword



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

def keyword
  @keyword
end

#rowsObject

The row models in the example table



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

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.

Examples:

example.add_row({'param1' => 'value1', 'param2' => 'value2'})
example.add_row({param1: 'value1', param2: 'value2'})
example.add_row(['value1', 'value2'])

Parameters:

  • row (Hash, Array<String>)

    The the cell values to use for the added row

Raises:

  • (ArgumentError)

    If row is not a Hash or Array

  • (Exception)

    If the model has no initial parameter row



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cuke_modeler/models/example.rb', line 55

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_rowsArray<Row>

Returns the Row models associated with the argument rows in the example table

Examples:

example.argument_rows

Returns:

  • (Array<Row>)

    The argument row models



111
112
113
# File 'lib/cuke_modeler/models/example.rb', line 111

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

#childrenArray<Row, Tag>

Returns the model objects that are children of this model. For an Example model, these would be any associated Row or Tag models.

Examples:

example.children

Returns:

  • (Array<Row, Tag>)

    A collection of child models



143
144
145
# File 'lib/cuke_modeler/models/example.rb', line 143

def children
  rows + tags
end

#inspect(verbose: false) ⇒ String

See ‘Object#inspect`. Returns some basic information about the object, including its class, object ID, and its most meaningful attribute. For an Example model, this will be the name of the example. If verbose is true, provides default Ruby inspection behavior instead.

Examples:

example.inspect
example.inspect(verbose: true)

Parameters:

  • verbose (Boolean) (defaults to: false)

    Whether or not to return the full details of the object. Defaults to false.

Returns:

  • (String)

    A string representation of this model



185
186
187
188
189
# File 'lib/cuke_modeler/models/example.rb', line 185

def inspect(verbose: false)
  return super(verbose: verbose) if verbose

  "#{super.chop} @name: #{name.inspect}>"
end

#parameter_rowRow?

Returns the Row model associated with the parameter row in the example table

Examples:

example.parameter_row

Returns:

  • (Row, nil)

    The parameter row model



122
123
124
# File 'lib/cuke_modeler/models/example.rb', line 122

def parameter_row
  rows.first
end

#parametersArray<String>

Returns the parameters of the example table

Examples:

example.parameters #=> ['param_1', 'param_2']

Returns:

  • (Array<String>)

    The parameters



132
133
134
# File 'lib/cuke_modeler/models/example.rb', line 132

def parameters
  parameter_row ? parameter_row.cells.map(&:value) : []
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.

Examples:

example.remove_row({'param1' => 'value1', 'param2' => 'value2'})
example.remove_row(['value1', 'value2'])

Parameters:

  • row (Hash, Array<String>)

    The the cell values to use for the added row

Raises:

  • (ArgumentError)

    If row is not a Hash or Array



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cuke_modeler/models/example.rb', line 87

def remove_row(row)
  return if argument_rows.empty?

  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.
             ordered_row_values(row)
           else
             raise(ArgumentError, "Can only remove row from a Hash or an Array but received #{row.class}")
           end

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

#to_sString

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

Examples:

example.to_s

Returns:

  • (String)

    A string representation of this model



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/cuke_modeler/models/example.rb', line 157

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