Module: Filigree::Configuration::ClassMethods

Defined in:
lib/filigree/configuration.rb

Overview

Class Methods #

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#options_longHash<String, Option> (readonly)

Returns Hash of options with long names used as keys.

Returns:

  • (Hash<String, Option>)

    Hash of options with long names used as keys



189
190
191
# File 'lib/filigree/configuration.rb', line 189

def options_long
  @options_long
end

#options_shortHash<String, Option> (readonly)

Returns hash of options with short name used as keys.

Returns:

  • (Hash<String, Option>)

    hash of options with short name used as keys



191
192
193
# File 'lib/filigree/configuration.rb', line 191

def options_short
  @options_short
end

Class Method Details

.extended(klass) ⇒ Object

Callbacks #



330
331
332
# File 'lib/filigree/configuration.rb', line 330

def self.extended(klass)
	klass.install_icvars
end

Instance Method Details

#add_option(opt) ⇒ void

This method returns an undefined value.

Add an option to the necessary data structures.

Parameters:

  • opt (Option)

    Option to add



198
199
200
201
202
203
# File 'lib/filigree/configuration.rb', line 198

def add_option(opt)
	attr_accessor opt.long

	@options_long[opt.long]   = opt
	@options_short[opt.short] = opt unless opt.short.nil?
end

#auto(name, &block) ⇒ void

This method returns an undefined value.

Define an automatic configuration variable.

Parameters:

  • name (Symbol)

    Name of the configuration variable

  • block (Proc)

    Block to be executed to generate the value



211
212
213
# File 'lib/filigree/configuration.rb', line 211

def auto(name, &block)
	define_method(name, &block)
end

#bool_option(long, short = nil) ⇒ void

This method returns an undefined value.

Define a boolean option. The variable will be set to true if the flag is seen and be false otherwise.

Parameters:

  • long (String)

    Long name of the option

  • short (String) (defaults to: nil)

    Short name of the option



222
223
224
225
# File 'lib/filigree/configuration.rb', line 222

def bool_option(long, short = nil)
	@next_default = false
	option(long, short) { true }
end

#default(val = nil, &block) ⇒ void

This method returns an undefined value.

Sets the default value for the next command. If a block is provided it will be used. If not, the val parameter will be.

Parameters:

  • val (Object) (defaults to: nil)

    Default value

  • block (Proc)

    Default value generator block



234
235
236
# File 'lib/filigree/configuration.rb', line 234

def default(val = nil, &block)
	@next_default = block ? block : val
end

#help(str) ⇒ void

This method returns an undefined value.

Sets the help string for the next command.

Parameters:

  • str (String)

    Command help string



243
244
245
# File 'lib/filigree/configuration.rb', line 243

def help(str)
	@help_string = str
end

#install_icvarsvoid

This method returns an undefined value.

Install the instance class variables in the including class.



250
251
252
253
254
255
256
257
258
# File 'lib/filigree/configuration.rb', line 250

def install_icvars
	@help_string   = ''
	@next_default  = nil
	@next_required = false
	@options_long  = Hash.new
	@options_short = Hash.new
	@required      = Array.new
	@usage         = ''
end

#option(long, short = nil, conversions: nil, &block) ⇒ void

This method returns an undefined value.

Define a new option.

Parameters:

  • long (String)

    Long option name

  • short (String) (defaults to: nil)

    Short option name

  • conversions (Array<Symbol>) (defaults to: nil)

    List of methods used to convert string arguments

  • block (Proc)

    Block used when the option is encountered



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/filigree/configuration.rb', line 268

def option(long, short = nil, conversions: nil, &block)

	attr_accessor long.to_sym

	long  = long.to_s
	short = short.to_s if short

	add_option Option.new(long, short, @help_string, @next_default,
		                  conversions.nil? ? block : conversions)

	@required << long.to_sym if @next_required

	# Reset state between option declarations.
	@help_string   = ''
	@next_default  = nil
	@next_required = false
end

#required(*names) ⇒ void

This method returns an undefined value.

Mark some options as required. If no names are provided then the next option to be defined is required; if names are provided they are all marked as required.

Parameters:

  • names (Symbol)

    Options to be marked as required.



293
294
295
296
297
298
299
# File 'lib/filigree/configuration.rb', line 293

def required(*names)
	if names.empty?
		@next_required = true
	else
		@required += names
	end
end

#required_optionsArray<Symbol>

Returns Options that need to be marked as required.

Returns:

  • (Array<Symbol>)

    Options that need to be marked as required



302
303
304
# File 'lib/filigree/configuration.rb', line 302

def required_options
	@required
end

#string_option(long, short = nil) ⇒ void

This method returns an undefined value.

Define an option that takes a single string argument.

Parameters:

  • long (String)

    Long option name

  • short (String) (defaults to: nil)

    Short option name



312
313
314
# File 'lib/filigree/configuration.rb', line 312

def string_option(long, short = nil)
	option(long, short) { |str| str }
end

#usage(str = nil) ⇒ String

Add’s a usage string to the entire configuration object. If no string is provided the current usage string is returned.

Parameters:

  • str (String, nil) (defaults to: nil)

    Usage string

Returns:

  • (String)

    Current or new usage string



322
323
324
# File 'lib/filigree/configuration.rb', line 322

def usage(str = nil)
	if str then @usage = str else @usage end
end