Class: Dill::WidgetName

Inherits:
String
  • Object
show all
Defined in:
lib/dill/widgets/widget_name.rb

Overview

The name of a widget in a format-independent representation.

Constant Summary collapse

CAMEL_CASE_FORMAT =
/\A([A-Z][a-z]*)+\Z/
SNAKE_CASE_FORMAT =
/\A\w+\Z/

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ WidgetName

Constructs the widget name.

Parameters:

  • name (String, Symbol)

    the name of the widget in primitive form



10
11
12
13
# File 'lib/dill/widgets/widget_name.rb', line 10

def initialize(name)
  @name      = name
  @canonical = canonical(@name)
end

Instance Method Details

#to_class(scope = Object) ⇒ Object

Returns the class for this widget name, in the given scope.



16
17
18
19
20
21
22
23
24
25
# 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_symObject



27
28
29
# File 'lib/dill/widgets/widget_name.rb', line 27

def to_sym
  @canonical.to_sym
end