Class: Cucumber::CucumberExpressions::ParameterType

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/cucumber_expressions/parameter_type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, regexp, type, transformer, use_for_snippets, prefer_for_regexp_match) ⇒ ParameterType

Create a new Parameter

Parameters:

  • name

    the name of the parameter type

  • regexp (Array)

    list of regexps for capture groups. A single regexp can also be used

  • type

    the return type of the transformed

  • transformer

    lambda that transforms a String to (possibly) another type

  • use_for_snippets

    true if this should be used for snippet generation

  • prefer_for_regexp_match

    true if this should be preferred over similar types



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cucumber/cucumber_expressions/parameter_type.rb', line 25

def initialize(name, regexp, type, transformer, use_for_snippets, prefer_for_regexp_match)
  raise "name can't be nil" if name.nil?
  raise "regexp can't be nil" if regexp.nil?
  raise "type can't be nil" if type.nil?
  raise "transformer can't be nil" if transformer.nil?
  raise "use_for_snippets can't be nil" if use_for_snippets.nil?
  raise "prefer_for_regexp_match can't be nil" if prefer_for_regexp_match.nil?

  @name, @type, @transformer, @use_for_snippets, @prefer_for_regexp_match = name, type, transformer, use_for_snippets, prefer_for_regexp_match
  @regexps = string_array(regexp)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/cucumber/cucumber_expressions/parameter_type.rb', line 6

def name
  @name
end

#regexpsObject (readonly)

Returns the value of attribute regexps.



6
7
8
# File 'lib/cucumber/cucumber_expressions/parameter_type.rb', line 6

def regexps
  @regexps
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/cucumber/cucumber_expressions/parameter_type.rb', line 6

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Object



48
49
50
51
52
# File 'lib/cucumber/cucumber_expressions/parameter_type.rb', line 48

def <=>(other)
  return -1 if prefer_for_regexp_match? && !other.prefer_for_regexp_match?
  return 1  if other.prefer_for_regexp_match? && !prefer_for_regexp_match?
  name <=> other.name
end

#prefer_for_regexp_match?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/cucumber/cucumber_expressions/parameter_type.rb', line 8

def prefer_for_regexp_match?
  @prefer_for_regexp_match
end

#transform(group_values) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/cucumber/cucumber_expressions/parameter_type.rb', line 37

def transform(group_values)
  if @transformer.arity == 1
    non_nil_group_values = group_values.compact
    raise CucumberExpressionError.new(
        "Single transformer unexpectedly matched 2 values - \"#{non_nil_group_values[0]}\" and \"${non_nil_group_values[1]}\""
    ) if non_nil_group_values.length >= 2
    return @transformer.call(non_nil_group_values[0])
  end
  @transformer.call(*group_values)
end

#use_for_snippets?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/cucumber/cucumber_expressions/parameter_type.rb', line 12

def use_for_snippets?
  @use_for_snippets
end