Module: Filigree::Configuration

Includes:
ClassMethodsModule
Defined in:
lib/filigree/configuration.rb

Defined Under Namespace

Modules: ClassMethods Classes: Option

Constant Summary collapse

HELP_OPTION =

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

Option.new('help', 'h', 'Prints this help message.', nil, Proc.new do
	puts "Usage: #{self.class.usage}"
	puts
	puts 'Options:'

	options = self.class.options_long.values.sort { |a, b| a.long <=> b.long }
	puts Option.to_s(options, 2)

	# Quit the application after printing the help message.
	exit
end)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethodsModule

included

Instance Attribute Details

#restArray<String>

Returns Remaining strings that weren’t used in configuration.

Returns:

  • (Array<String>)

    Remaining strings that weren’t used in configuration



33
34
35
# File 'lib/filigree/configuration.rb', line 33

def rest
  @rest
end

Instance Method Details

#dump(io, *fields) ⇒ void #dump(str, *fields) ⇒ void #dump(io, *fields) ⇒ void Also known as: serialize

This method returns an undefined value.

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

Overloads:

  • #dump(io, *fields) ⇒ void

    Dump the state to stdout.

    Parameters:

    • io (nil)

      Tells the method to serialize to stdout

    • fields (Symbol)

      Fields to serialize

  • #dump(str, *fields) ⇒ void

    Dump the state to a file.

    Parameters:

    • io (String)

      Name of file to serialize to

    • fields (Symbol)

      Fields to serialize

  • #dump(io, *fields) ⇒ void

    Dump the state to the provided IO instance.

    Parameters:

    • io (IO)

      IO object to serialize to

    • fields (Symbol)

      Fields to serialize



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/filigree/configuration.rb', line 55

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

#find_option(str) ⇒ Option?

Find the appropriate option object given a string.

Parameters:

  • str (String)

    Search string

Returns:

  • (Option, nil)

    Desired option or nil if it wasn’t found



119
120
121
122
123
124
125
126
# File 'lib/filigree/configuration.rb', line 119

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) ⇒ void

This method returns an undefined value.

Configure the object from an array of strings.

Parameters:



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/filigree/configuration.rb', line 134

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 { |str| 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) ⇒ void

This method returns an undefined value.

Configure the object from a serialization source.

Parameters:

  • overloaded (String, IO)

    Serialization source

  • set_opts (Array<String>)

    List of names of options already added



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/filigree/configuration.rb', line 165

def handle_serialized_options(overloaded, set_opts)
	options =
	if overloaded.is_a? String
		if File.exists? 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

#initialize(args) ⇒ void #initialize(source) ⇒ void

Configures the object based on the overloaded parameter.

Overloads:

  • #initialize(args) ⇒ void

    Configure the object from an array of strings.

    Parameters:

  • #initialize(source) ⇒ void

    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.

    Parameters:

    • source (String, IO)

      Serialized configuration source



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/filigree/configuration.rb', line 90

def initialize(overloaded = ARGV.clone)
	set_opts = Array.new

	case overloaded
	when Array
		handle_array_options(overloaded, 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
end