class Opto::Setter

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.

Attributes

hint[RW]
option[RW]

Public Class Methods

for(target) click to toggle source

Find a setter using a target_name definition, such as :env or :file @param [Symbol, String] target

# File lib/opto/setter.rb, line 21
def for(target)
  raise NameError, "Unknown setter: #{target}" unless targets[target]
  targets[target]
end
inherited(where) click to toggle source
# File lib/opto/setter.rb, line 26
def inherited(where)
  targets[where.target] = where
end
new(hint = nil, option = nil) click to toggle source

Initialize an instance of a setter. @param hint A “hint” for the setter, for example. the environment variable name @param [Opto::Option] option The option parent of this resolver instance @return [Opto::Resolver]

# File lib/opto/setter.rb, line 43
def initialize(hint = nil, option = nil)
  @hint = hint
  @option = option
end
target() click to toggle source
# File lib/opto/setter.rb, line 34
def target
  name.to_s.split('::').last.snakecase.to_sym
end
targets() click to toggle source
# File lib/opto/setter.rb, line 30
def targets
  @targets ||= {}
end

Public Instance Methods

set(value) click to toggle source

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

# File lib/opto/setter.rb, line 49
def set(value)
  raise RuntimeError, "#{self.class}.set not defined"
end
target() click to toggle source

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

# File lib/opto/setter.rb, line 54
def target
  self.class.target
end