Class: CukeModeler::Rule

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

Overview

A class modeling a rule in a Cucumber suite.

Instance Attribute Summary collapse

Attributes included from Sourceable

#source_column, #source_line

Attributes included from Taggable

#tags

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) ⇒ Rule

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

Examples:

Rule.new
Rule.new("Rule:\nThis is a rule")

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/rule.rb', line 34

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

  super
end

Instance Attribute Details

#backgroundObject

The Background object contained by the Rule



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

def background
  @background
end

#keywordObject

The keyword for the rule



15
16
17
# File 'lib/cuke_modeler/models/rule.rb', line 15

def keyword
  @keyword
end

#testsObject

The Scenario and Outline objects contained by the Rule



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

def tests
  @tests
end

Instance Method Details

#background?Boolean Also known as: has_background?

Returns true if the rule contains a background, false otherwise.

Examples:

rule.background?

Returns:

  • (Boolean)

    Whether the rule contains a background



47
48
49
# File 'lib/cuke_modeler/models/rule.rb', line 47

def background?
  !@background.nil?
end

#childrenArray<Background, Scenario, Outline, Tag>

Returns the model objects that are children of this model. For a Rule model, these would be any associated Background, Scenario, Outline, or Tag models.

Examples:

rule.children

Returns:



81
82
83
84
85
86
# File 'lib/cuke_modeler/models/rule.rb', line 81

def children
  models = tests + tags
  models << background if background

  models
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 a Rule model, this will be the name of the rule. If verbose is true, provides default Ruby inspection behavior instead.

Examples:

rule.inspect
rule.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



120
121
122
123
124
# File 'lib/cuke_modeler/models/rule.rb', line 120

def inspect(verbose: false)
  return super if verbose

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

#outlinesArray<Outline>

Returns the outline models contained in the rule.

Examples:

rule.outlines

Returns:

  • (Array<Outline>)

    Child Outline models



69
70
71
# File 'lib/cuke_modeler/models/rule.rb', line 69

def outlines
  @tests.select { |test| test.is_a? Outline }
end

#scenariosArray<Scenario>

Returns the scenario models contained in the rule.

Examples:

rule.scenarios

Returns:

  • (Array<Scenario>)

    Child Scenario models



59
60
61
# File 'lib/cuke_modeler/models/rule.rb', line 59

def scenarios
  @tests.select { |test| test.is_a? Scenario }
end

#to_sString

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

Examples:

rule.to_s

Returns:

  • (String)

    A string representation of this model



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cuke_modeler/models/rule.rb', line 95

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\n#{background_output_string}" if background
  text << "\n\n#{tests_output_string}" unless tests.empty?

  text
end