Class: Opto::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/opto/resolver.rb

Overview

Base for resolvers.

Resolvers are scripts that can retrieve or generate a value for an option. Such resolvers are for example Env, which can try to find the value for the option from an environment variable. An example of generators is RandomString, which can generate random strings of defined length.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hint = nil, option = nil) ⇒ Opto::Resolver

Initialize an instance of a resolver.

Parameters:

  • hint (defaults to: nil)

    A “hint” for the resolver, for example. the environment variable name or a set of rules for generators.

  • option (Opto::Option) (defaults to: nil)

    The option parent of this resolver instance


43
44
45
46
# File 'lib/opto/resolver.rb', line 43

def initialize(hint = nil, option = nil)
  @hint = hint
  @option = option
end

Instance Attribute Details

#hintObject

Returns the value of attribute hint


15
16
17
# File 'lib/opto/resolver.rb', line 15

def hint
  @hint
end

#optionObject

Returns the value of attribute option


16
17
18
# File 'lib/opto/resolver.rb', line 16

def option
  @option
end

Class Method Details

.for(origin) ⇒ Object

Find a resolver using an origin_name definition, such as :env or :file

Parameters:

  • origin (Symbol, String)

Raises:

  • (NameError)

21
22
23
24
# File 'lib/opto/resolver.rb', line 21

def for(origin)
  raise NameError, "Unknown resolver: #{origin}" unless resolvers[origin]
  resolvers[origin]
end

.inherited(where) ⇒ Object


26
27
28
# File 'lib/opto/resolver.rb', line 26

def inherited(where)
  resolvers[where.origin] = where
end

.originObject


34
35
36
# File 'lib/opto/resolver.rb', line 34

def origin
  name.to_s.split('::').last.snakecase.to_sym
end

.resolversObject


30
31
32
# File 'lib/opto/resolver.rb', line 30

def resolvers
  @resolvers ||= {}
end

Instance Method Details

#originObject

The origin “tag” of this resolver, for example: 'random_string' or 'env'


54
55
56
# File 'lib/opto/resolver.rb', line 54

def origin
  self.class.origin
end

#resolveObject

This is a “base” class, you're supposed to inherit from this in your resolver and define a #resolve method.

Raises:

  • (RuntimeError)

49
50
51
# File 'lib/opto/resolver.rb', line 49

def resolve
  raise RuntimeError, "#{self.class}.resolve not defined"
end