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 ⇒ Object
The argument rows in the example table.
-
#children ⇒ Object
Returns the model objects that belong to this model.
-
#initialize(source_text = nil) ⇒ Example
constructor
Creates a new Example object and, if source_text is provided, populates the object.
-
#parameter_row ⇒ Object
The parameter row for the example table.
-
#parameters ⇒ Object
Returns the parameters of the example table.
-
#remove_row(row_removed) ⇒ Object
Removes a row from the example table.
-
#to_s ⇒ Object
Returns a string representation of this model.
Methods included from Taggable
Methods included from Parsing
Methods included from Containing
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.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/cuke_modeler/models/example.rb', line 24 def initialize(source_text = nil) @tags = [] @rows = [] super(source_text) if source_text parsed_example_data = parse_source(source_text) populate_example(self, parsed_example_data) end end |
Instance Attribute Details
#keyword ⇒ Object
The example’s keyword
16 17 18 |
# File 'lib/cuke_modeler/models/example.rb', line 16 def keyword @keyword end |
#rows ⇒ Object
The row models in the example table
19 20 21 |
# File 'lib/cuke_modeler/models/example.rb', line 19 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.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cuke_modeler/models/example.rb', line 39 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)) case when row.is_a?(Array) # 'stringify' input row.collect! { |value| value.to_s } @rows << Row.new("|#{row.join('|')}|") when row.is_a?(Hash) # 'stringify' input row = row.inject({}) { |hash, (key, value)| hash[key.to_s] = value.to_s; hash } @rows << 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 |
#argument_rows ⇒ Object
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 |
#children ⇒ Object
Returns the model objects that belong to this model.
98 99 100 |
# File 'lib/cuke_modeler/models/example.rb', line 98 def children rows + end |
#parameter_row ⇒ Object
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 |
#parameters ⇒ Object
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.collect { |cell| cell.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.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cuke_modeler/models/example.rb', line 64 def remove_row(row_removed) return unless argument_rows case when row_removed.is_a?(Array) location = argument_rows.index { |row| row.cells.collect { |cell| cell.value } == row_removed.collect { |value| value.strip } } when row_removed.is_a?(Hash) # Note: the hash value order has to be manually calculated because Ruby 1.8.7 does not have ordered # hash keys. Alternatively, the hash may have simply been built up 'willy nilly' by the user instead # of being built up in order according to the parameter order. location = argument_rows.index { |row| row.cells.collect { |cell| cell.value } == ordered_row_values(row_removed.each_value { |value| value.strip! }) } else raise(ArgumentError, "Can only remove row from a Hash or an Array but received #{row_removed.class}") end @rows.delete_at(location + 1) if location end |
#to_s ⇒ Object
Returns a string representation of this model. For an example model, this will be Gherkin text that is equivalent to the example being modeled.
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/cuke_modeler/models/example.rb', line 104 def to_s text = '' text << tag_output_string + "\n" unless .empty? text << "#{@keyword}:#{name_output_string}" text << "\n" + description_output_string unless (description.nil? || description.empty?) text << "\n" unless (rows.empty? || description.nil? || description.empty?) text << "\n" + parameters_output_string if parameter_row text << "\n" + rows_output_string unless argument_rows.empty? text end |