Class: Opto::Setter

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

Overview

Base for Setters.

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.

Direct Known Subclasses

Opto::Setters::Env

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 setter.

Parameters:

  • hint (defaults to: nil)

    A “hint” for the setter, for example. the environment variable name

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

    The option parent of this resolver instance


43
44
45
46
# File 'lib/opto/setter.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/setter.rb', line 15

def hint
  @hint
end

#optionObject

Returns the value of attribute option


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

def option
  @option
end

Class Method Details

.for(target) ⇒ Object

Find a setter using a target_name definition, such as :env or :file

Parameters:

  • target (Symbol, String)

Raises:

  • (NameError)

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

def for(target)
  raise NameError, "Unknown setter: #{target}" unless targets[target]
  targets[target]
end

.inherited(where) ⇒ Object


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

def inherited(where)
  targets[where.target] = where
end

.targetObject


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

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

.targetsObject


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

def targets
  @targets ||= {}
end

Instance Method Details

#set(value) ⇒ Object

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

Raises:

  • (RuntimeError)

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

def set(value)
  raise RuntimeError, "#{self.class}.set not defined"
end

#targetObject

The target “tag” of this resolver, for example: 'file' or 'env'


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

def target
  self.class.target
end