module Filigree::Configuration

Constants

HELP_OPTION

The default help option. This can be added to your class via add_option.

Attributes

rest[RW]

@return [Array<String>] Remaining strings that weren't used in configuration

Public Class Methods

new(overloaded = ARGV.clone) click to toggle source

Configures the object based on the overloaded parameter.

@overload initialize(args)

Configure the object from an array of strings.
@param [Array<String>]  args  String arguments

@overload initialize(source)

Configure the object from a serialized source.  If source is a
string then it will be treated as a file name and the
configuration will be loaded from the specified string.  If it
an IO object then that will be used as the source.
@param [String, IO]  source  Serialized configuration source

@return [void]

# File lib/filigree/configuration.rb, line 96
def initialize(overloaded = ARGV.clone)
        set_opts = Array.new

        case overloaded
        when Array
                handle_array_options(overloaded.clone, set_opts)

        when String, IO
                handle_serialized_options(overloaded, set_opts)
        end

        (self.class.options_long.keys - set_opts).each do |opt_name|
                default = self.class.options_long[opt_name].default
                default = self.instance_exec(&default) if default.is_a? Proc

                self.send("#{opt_name}=", default)
        end

        # Check to make sure all the required options are set.
        self.class.required_options.each do |option|
                raise ArgumentError, "Option #{option} not set." if self.send(option).nil?
        end

        # Initialize the auto options
        self.class.auto_blocks.each do |name, block|
                result = self.instance_exec(&block)

                self.define_singleton_method(name) { result }
        end
end

Public Instance Methods

dump(io = nil, *fields) click to toggle source

Dump the state of the Configuration object. This will dump the state, encoded in YAML, to different destinations depending on the io parameter.

@overload dump(io, *fields)

Dump the state to stdout.
@param [nil]     io      Tells the method to serialize to stdout
@param [Symbol]  fields  Fields to serialize

@overload dump(str, *fields)

Dump the state to a file.
@param [String]  io      Name of file to serialize to
@param [Symbol]  fields  Fields to serialize

@overload dump(io, *fields)

Dump the state to the provided IO instance.
@param [IO]      io      IO object to serialize to
@param [Symbol]  fields  Fields to serialize

@return [void]

# File lib/filigree/configuration.rb, line 61
def dump(io = nil, *fields)
        require 'yaml'

        vals =
        if fields.empty? then self.class.options_long.keys else fields end.inject(Hash.new) do |hash, field|
                hash.tap { hash[field.to_s] = self.send(field) }
        end

        case io
        when nil
                YAML.dump vals

        when String
                File.open(io, 'w') { |file| YAML.dump vals, file }

        when IO
                YAML.dump vals, io
        end
end
Also aliased as: serialize
find_option(str) click to toggle source

Find the appropriate option object given a string.

@param [String] str Search string

@return [Option, nil] Desired option or nil if it wasn't found

# File lib/filigree/configuration.rb, line 132
def find_option(str)
        if str[0,2] == '--'
                self.class.options_long[str[2..-1]]

        elsif str[0,1] == '-'
                self.class.options_short[str[1..-1]]
        end
end
handle_array_options(argv, set_opts) click to toggle source

Configure the object from an array of strings.

TODO: Improve the way arguments are pulled out of the ARGV array.

@param [Array<String>] argv String options @param [Array<String>] set_opts List of names of options already added

@return [void]

# File lib/filigree/configuration.rb, line 149
def handle_array_options(argv, set_opts)
        while str = argv.shift

                break if str == '--'

                if option = find_option(str)
                        args = argv.shift(option.arity == -1 ? argv.index { |sub_str| sub_str[0,1] == '-' } : option.arity)

                        case option.handler
                        when Array
                                tmp = args.zip(option.handler).map { |arg, sym| arg.send sym }
                                self.send("#{option.long}=", (option.arity == 1 and tmp.length == 1) ? tmp.first : tmp)

                        when Proc
                                self.send("#{option.long}=", self.instance_exec(*args, &option.handler))
                        end

                        set_opts << option.long
                end
        end

        # Save the rest of the command line for later.
        self.rest = argv
end
handle_serialized_options(overloaded, set_opts) click to toggle source

Configure the object from a serialization source.

@param [String, IO] overloaded Serialization source @param [Array<String>] set_opts List of names of options already added

@return [void]

# File lib/filigree/configuration.rb, line 180
def handle_serialized_options(overloaded, set_opts)
        options =
        if overloaded.is_a? String
                if File.exist? overloaded
                        YAML.load_file overloaded
                else
                        YAML.load overloaded
                end
        else
                YAML.load overloaded
        end

        options.each do |option, val|
                set_opts << option
                self.send "#{option}=", val
        end
end
serialize(io = nil, *fields)
Alias for: dump