Module: Opto

Defined in:
lib/opto.rb,
lib/opto/type.rb,
lib/opto/group.rb,
lib/opto/option.rb,
lib/opto/setter.rb,
lib/opto/version.rb,
lib/opto/resolver.rb,
lib/opto/types/uri.rb,
lib/opto/types/enum.rb,
lib/opto/types/array.rb,
lib/opto/types/group.rb,
lib/opto/types/string.rb,
lib/opto/types/boolean.rb,
lib/opto/types/integer.rb,
lib/opto/resolvers/yaml.rb,
lib/opto/resolvers/default.rb,
lib/opto/resolvers/evaluate.rb,
lib/opto/resolvers/variable.rb,
lib/opto/resolvers/condition.rb,
lib/opto/extensions/snake_case.rb,
lib/opto/resolvers/interpolate.rb,
lib/opto/resolvers/random_uuid.rb,
lib/opto/resolvers/file_content.rb,
lib/opto/resolvers/random_number.rb,
lib/opto/resolvers/random_string.rb,
lib/opto/extensions/symbolize_keys.rb,
lib/opto/setters/environment_variable.rb,
lib/opto/resolvers/environment_variable.rb,
lib/opto/extensions/hash_string_or_symbol_key.rb

Overview

An option parser/validator/resolver

Defined Under Namespace

Modules: Extension, Resolvers, Setters, Types Classes: Group, Option, Resolver, Setter, Type

Constant Summary collapse

VERSION =
"1.9.1"

Class Method Summary collapse

Class Method Details

.new(opts) ⇒ Object

Initialize a new Opto::Option (when input is hash) or an Opto::Group (when input is an array of hashes)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/opto.rb', line 17

def self.new(opts)
  case opts
  when Hash
    if opts.has_key?('name') || opts.has_key?(:name)
      Option.new(opts)
    else
      Group.new(opts)
    end
  when ::Array
    if opts.all? {|o| o.kind_of?(Hash) }
      Group.new(opts)
    else
      raise TypeError, "Invalid input, an option hash or an array of option hashes required"
    end
  else
    raise TypeError, "Invalid input, an option hash or an array of option hashes required"
  end
end

.read(yaml_path, key = nil) ⇒ Object

Read an option (or option group) from a YAML file

Examples:

Opto.read('/tmp/foo.yml', :options)

Parameters:

  • path_to_file (String)
  • a (String, Symbol)

    key in the hash representation of the file, such as :variables to read the options from (instead of using the root)


41
42
43
44
# File 'lib/opto.rb', line 41

def self.read(yaml_path, key=nil)
  opts = YAML.load(File.read(yaml_path))
  new(key.nil? ? opts : opts[key])
end