Class: String

Inherits:
Object
  • Object
show all
Includes:
Enum
Defined in:
lib/cucumber/helpers/core_ext.rb,
lib/cucumber/helpers/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#camel_caseObject

Converts a string to camelCase.



92
93
94
95
96
97
# File 'lib/cucumber/helpers/core_ext.rb', line 92

def camel_case
  s = self
  s = s.snake_case unless /^[a-z0-9]+$/i =~ s
  s = s.downcase if s == s.upcase # stop "CVV" -> "cVV" when it should be "cvv"
  s.camelize(:lower)
end

#snake_case(casing = :lower) ⇒ Object

Converts a string to snake_case or SNAKE_CASE.



100
101
102
103
104
105
106
107
# File 'lib/cucumber/helpers/core_ext.rb', line 100

def snake_case(casing = :lower)
  s = self.tr(" ", "_")
  case casing
  when :lower then s.downcase 
  when :upper then s.upcase
  else raise ArgumentError, "unsupported casing"
  end
end

#to_type(type) ⇒ Object

Converts a string to the specified primitive type. Useful because all Gherkin values are treated as strings.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cucumber/helpers/core_ext.rb', line 110

def to_type(type)
  # note: cannot use 'case type' as that expands to === which checks for instances of rather than type equality
  if type == Boolean
    self == "true"
  elsif type == Date
    Date.parse(self)
  elsif type == DateTime
    DateTime.parse(self)
  elsif type == Enum
    self.snake_case(:upper)
  elsif type == Float
    self.to_f
  elsif type == Integer
    self.to_i
  else
    self
  end
end