class Capybara::UI::WidgetName
The name of a widget in a format-independent representation.
Constants
- CAMEL_CASE_FORMAT
- SNAKE_CASE_FORMAT
Public Class Methods
new(name)
click to toggle source
Constructs the widget name.
@param name [String, Symbol] the name of the widget in primitive form
# File lib/capybara/ui/widgets/widget_name.rb, line 11 def initialize(name) @name = name @canonical = canonical(@name) end
Public Instance Methods
to_class(scope = Object)
click to toggle source
Returns the class for this widget name, in the given scope.
# File lib/capybara/ui/widgets/widget_name.rb, line 17 def to_class(scope = Object) const = scope.const_get(@canonical) raise TypeError, "`#{@canonical}' is not a widget in this scope" \ unless const < Widget const rescue NameError raise Missing, "couldn't find `#{@name}' widget in this scope" end
to_sym()
click to toggle source
# File lib/capybara/ui/widgets/widget_name.rb, line 28 def to_sym @canonical.to_sym end
Private Instance Methods
camel_case_from_snake_case(name)
click to toggle source
# File lib/capybara/ui/widgets/widget_name.rb, line 47 def camel_case_from_snake_case(name) capitalize_word = ->(word) { word[0].upcase + word[1..-1] } word_separator = '_' name .split(word_separator) .map(&capitalize_word) .join end
canonical(name)
click to toggle source
# File lib/capybara/ui/widgets/widget_name.rb, line 34 def canonical(name) str = name.to_s case str when SNAKE_CASE_FORMAT camel_case_from_snake_case(str) when CAMEL_CASE_FORMAT str else raise ArgumentError, "`#{str.inspect}' is an unrecognized format. Try snake case or camel case." end end