Class: Rosette::Core::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/rosette/core/resolvers/resolver.rb

Overview

Base class for Rosette's id resolvers that can look up a class constant given a namespaced id (string separated by forward slashes). For example, the extractor id “yaml/rails” resolves to Rosette::Extractors::YamlExtractor::RailsExtractor.

Examples:

class MyResolver < Resolver
  def resolve(id, namespace = MyNamespace::Foo)
    super
  end

  private

  # Must be defined by classes that inherit from Resolver.
  def suffix
    'Stuff'
  end
end

module MyNamespace
  module Foo
    module BarStuff
      class BazStuff
        ...
      end
    end
  end
end

MyResolver.resolve('bar/baz')  # => MyNamespace::Foo::BarStuff::BazStuff

Class Method Summary collapse

Class Method Details

.parse_id(id) ⇒ Array<String>

Splits an id into parts.

Parameters:

  • id (String)

    The id to parse.

Returns:

  • (Array<String>)

    A list of id parts.


65
66
67
# File 'lib/rosette/core/resolvers/resolver.rb', line 65

def parse_id(id)
  id.split('/')
end

.resolve(id, namespace) ⇒ Class

Parses and identifies the class constant for the given id.

Parameters:

  • id (Class, String)

    When given a class, returns the class. When given a string, parses and identifies the corresponding class constant in namespace.

  • namespace (Class)

    The namespace to look in.

Returns:

  • (Class)

    The identified class constant.


46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rosette/core/resolvers/resolver.rb', line 46

def resolve(id, namespace)
  klass = case id
    when Class
      id
    when String
      lookup(id, namespace)
  end

  unless klass
    raise ArgumentError, "#{id} could not be found - have you required it?"
  end

  klass
end