Class: Rudachi::Option

Inherits:
Object
  • Object
show all
Extended by:
Configurable
Defined in:
lib/rudachi/option/config.rb,
lib/rudachi/option/string_option.rb,
lib/rudachi/option/boolean_option.rb

Defined Under Namespace

Classes: BooleanOption, StringOption

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configurable

configure

Constructor Details

#initialize(**hash) ⇒ Option

Returns a new instance of Option.



32
33
34
35
36
37
38
39
40
41
# File 'lib/rudachi/option/config.rb', line 32

def initialize(**hash)
  @opts = hash.each_with_object({}) do |(key, val), _hash|
    raise ArgumentError, %{unknown option "#{key}"} unless self.class.class_variable_defined?("@@#{key}")
    begin
      _hash[key.to_sym] = self.class.class_variable_get("@@#{key}").class.new(val)
    rescue ArgumentError => e
      raise ArgumentError, %{"#{key}" #{e.message}}
    end
  end
end

Class Method Details

.cmds(opts = Option.new) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/rudachi/option/config.rb', line 22

def self.cmds(opts=Option.new)
  class_variables.each_with_object([]) do |name, flags|
    key = name[2..-1].to_sym
    opt = opts.get(key) { class_variable_get(name) }
    next unless opt&.enable?
    flags << "-#{key}"
    flags << opt.to_s if opt.with_arg?
  end
end

Instance Method Details

#get(key, &block) ⇒ Object



43
44
45
# File 'lib/rudachi/option/config.rb', line 43

def get(key, &block)
  @opts.key?(key) ? @opts[key] : block&.call
end