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

#auto_blocksHash<Symbol, Block> (readonly)



204
205
206
# File 'lib/filigree/configuration.rb', line 204

def auto_blocks
  @auto_blocks
end

#options_longHash<String, Option> (readonly)



206
207
208
# File 'lib/filigree/configuration.rb', line 206

def options_long
  @options_long
end

#options_shortHash<String, Option> (readonly)



208
209
210
# File 'lib/filigree/configuration.rb', line 208

def options_short
  @options_short
end

Class Method Details

.extended(klass) ⇒ Object

Callbacks #



345
346
347
# File 'lib/filigree/configuration.rb', line 345

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.



215
216
217
218
219
220
# File 'lib/filigree/configuration.rb', line 215

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.



228
229
230
# File 'lib/filigree/configuration.rb', line 228

def auto(name, &block)
  @auto_blocks[name.to_sym] = 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.



239
240
241
242
# File 'lib/filigree/configuration.rb', line 239

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.



251
252
253
# File 'lib/filigree/configuration.rb', line 251

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.



260
261
262
# File 'lib/filigree/configuration.rb', line 260

def help(str)
  @help_string = str
end

#install_icvarsvoid

This method returns an undefined value.

Install the instance class variables in the including class.



267
268
269
270
271
272
273
274
275
276
# File 'lib/filigree/configuration.rb', line 267

def install_icvars
  @auto_blocks   = Hash.new
  @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.



286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/filigree/configuration.rb', line 286

def option(long, short = nil, conversions: nil, &block)
  long  = long.to_s.gsub('-', '_')
  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.



308
309
310
311
312
313
314
# File 'lib/filigree/configuration.rb', line 308

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

#required_optionsArray<Symbol>



317
318
319
# File 'lib/filigree/configuration.rb', line 317

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.



327
328
329
# File 'lib/filigree/configuration.rb', line 327

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.



337
338
339
# File 'lib/filigree/configuration.rb', line 337

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