Class: CukeModeler::Example
- Defined in:
- lib/cuke_modeler/models/example.rb
Overview
A class modeling an example table of an outline.
Instance Attribute Summary collapse
-
#keyword ⇒ Object
The example’s keyword.
-
#rows ⇒ Object
The row models in the example table.
Attributes included from Taggable
Attributes included from Sourceable
Attributes included from Described
Attributes included from Named
Attributes included from Parsed
Attributes included from Nested
Instance Method Summary collapse
-
#add_row(row) ⇒ Object
Adds a row to the example table.
-
#argument_rows ⇒ Array<Row>
Returns the Row models associated with the argument rows in the example table.
-
#children ⇒ Array<Row, Tag>
Returns the model objects that are children of this model.
-
#initialize(source_text = nil) ⇒ Example
constructor
Creates a new Example object and, if source_text is provided, populates the object.
-
#inspect(verbose: false) ⇒ String
See ‘Object#inspect`.
-
#parameter_row ⇒ Row?
Returns the Row model associated with the parameter row in the example table.
-
#parameters ⇒ Array<String>
Returns the parameters of the example table.
-
#remove_row(row) ⇒ Object
Removes a row from the example table.
-
#to_s ⇒ String
Returns a string representation of this model.
Methods included from Taggable
Methods included from Parsing
Methods included from Containing
#each, #each_descendant, #each_model
Methods included from Nested
Constructor Details
#initialize(source_text = nil) ⇒ Example
Creates a new Example object and, if source_text is provided, populates the object.
34 35 36 37 38 39 |
# File 'lib/cuke_modeler/models/example.rb', line 34 def initialize(source_text = nil) @tags = [] @rows = [] super end |
Instance Attribute Details
#keyword ⇒ Object
The example’s keyword
18 19 20 |
# File 'lib/cuke_modeler/models/example.rb', line 18 def keyword @keyword end |
#rows ⇒ Object
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.
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_rows ⇒ Array<Row>
Returns the Row models associated with the argument rows in the example table
111 112 113 |
# File 'lib/cuke_modeler/models/example.rb', line 111 def argument_rows rows[1..rows.count] || [] end |
#children ⇒ Array<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.
143 144 145 |
# File 'lib/cuke_modeler/models/example.rb', line 143 def children rows + 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.
185 186 187 188 189 |
# File 'lib/cuke_modeler/models/example.rb', line 185 def inspect(verbose: false) return super if verbose "#{super.chop} @name: #{name.inspect}>" end |
#parameter_row ⇒ Row?
Returns the Row model associated with the parameter row in the example table
122 123 124 |
# File 'lib/cuke_modeler/models/example.rb', line 122 def parameter_row rows.first end |
#parameters ⇒ Array<String>
Returns the parameters of the example table
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.
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_s ⇒ String
Returns a string representation of this model. For an Example model, this will be Gherkin text that is equivalent to the example being modeled.
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 .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 |