Module: CukePatterns::RbLanguageExt

Defined in:
lib/cuke-patterns/rb_language_ext.rb

Overview

For extending the Cucumber::RbSupport::RbLanguage class

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(rb_language_class) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/cuke-patterns/rb_language_ext.rb', line 6

def self.included(rb_language_class)
  rb_language_class.class_eval do
    alias_method :register_rb_step_definition_without_cuke_patterns, :register_rb_step_definition
    alias_method :register_rb_step_definition, :register_rb_step_definition_with_cuke_patterns
  end

  super
end

Instance Method Details

#apply_cuke_patterns_to_delayed_step_definition_registrations!Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cuke-patterns/rb_language_ext.rb', line 15

def apply_cuke_patterns_to_delayed_step_definition_registrations!
  count = 0
  cuke_pattern_delayed_step_definition_registrations.each do |matcher, proc|
    regexp, converted_proc = convert_cuke_patterns_and_proc(matcher, proc)
    register_rb_step_definition(regexp, converted_proc)
    count += 1
  end

  # Clean up our mess!
  remove_instance_variable('@cuke_pattern_delayed_step_definition_registrations')
end

#apply_rb_cuke_pattern(name, string, world) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/cuke-patterns/rb_language_ext.rb', line 45

def apply_rb_cuke_pattern(name, string, world)
  name = ":#{name}" if name.is_a?(Symbol)
  regexp, proc = lookup_cuke_pattern(name)
  match = regexp.match(string)
  raise "Pattern #{regexp.to_s} does not match #{string.inspect}" unless match
  return world.instance_exec(*match.captures, &proc) if proc
  return match.to_s
end

#cuke_patternsObject



27
28
29
# File 'lib/cuke-patterns/rb_language_ext.rb', line 27

def cuke_patterns
  @cuke_patterns ||= {}
end

#default_cuke_pattern(key) ⇒ Object

Hook this method to automatically generate regexp matches for words in step definition strings that *don’t* match to registered patterns.



33
34
35
36
37
38
39
# File 'lib/cuke-patterns/rb_language_ext.rb', line 33

def default_cuke_pattern(key)
  default_cuke_pattern_generators.each do |generator|
    regexp, proc = generator[key]
    return regexp, proc if regexp
  end
  return nil
end

#default_cuke_pattern_generatorsObject



41
42
43
# File 'lib/cuke-patterns/rb_language_ext.rb', line 41

def default_cuke_pattern_generators
  @default_cuke_pattern_generators ||= []
end

#lookup_cuke_pattern(name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cuke-patterns/rb_language_ext.rb', line 54

def lookup_cuke_pattern(name)
  keys, regexp, proc = [], nil, nil
  cuke_patterns.each do |key, value|
    if key === name
      keys << key
      regexp, proc = value
    end
  end
  return default_cuke_pattern(name) if keys.empty?
  return regexp, proc if keys.length == 1
  raise "Ambiguous Pattern for #{name.inspect}: #{keys.inspect}"
end

#register_rb_cuke_pattern(*args, &conversion_proc) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cuke-patterns/rb_language_ext.rb', line 67

def register_rb_cuke_pattern(*args, &conversion_proc)
  if args.length == 1
    if args.first.is_a?(Hash)
      args.first.each do |key, value|
        register_rb_cuke_pattern(key, value, &conversion_proc)
      end
      return
    else
      # We wrap the key form of the regexp in begin/end
      # so that we match only whole tokens when doing lookup
      name = Regexp.new("^(?:#{args.first})$")
      regexp = args.first
    end
  elsif args.length == 2
    name, regexp = args
    name = ":#{name}" if name.is_a?(Symbol) # so :user becomes ':user'
  end

  if conversion_proc
    # Count the capturing '(' characters to get the pattern arity
    pattern_capture_count = capture_count_for(regexp)

    if pattern_capture_count != conversion_proc.arity
      raise "There are #{pattern_capture_count} of capture(s) in Pattern #{name} but block arity is #{conversion_proc.arity}"
    end
  end

  return cuke_patterns[name] = [regexp, conversion_proc]
end

#register_rb_cuke_pattern_generator(&proc) ⇒ Object



97
98
99
# File 'lib/cuke-patterns/rb_language_ext.rb', line 97

def register_rb_cuke_pattern_generator(&proc)
  default_cuke_pattern_generators << proc
end

#register_rb_step_definition_with_cuke_patterns(matcher, proc) ⇒ Object



101
102
103
104
105
# File 'lib/cuke-patterns/rb_language_ext.rb', line 101

def register_rb_step_definition_with_cuke_patterns(matcher, proc)
  return register_rb_step_definition_without_cuke_patterns(matcher, proc) unless matcher.is_a?(String)
  cuke_pattern_delayed_step_definition_registrations << [matcher, proc]
  return :registration_delayed_by_cuke_patterns
end