class Dill::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/dill/widgets/widget_name.rb, line 10
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/dill/widgets/widget_name.rb, line 16
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/dill/widgets/widget_name.rb, line 27
def to_sym
  @canonical.to_sym
end

Private Instance Methods

camel_case_from_snake_case(name) click to toggle source
# File lib/dill/widgets/widget_name.rb, line 46
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/dill/widgets/widget_name.rb, line 33
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