Class: Flags::Flag

Inherits:
Object
  • Object
show all
Defined in:
lib/flags.rb

Overview

A Flag represents everything we know about a flag - its name, value, default value, description, where it was defined, whether the default value has been explicitly modified, and an optional callback to validate the flag.

Direct Known Subclasses

BoolFlag, FloatFlag, IntFlag, StringFlag, SymbolFlag

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, name, default_value, description, definition_file, validators) ⇒ Flag

Initializes the Flag. Arguments:

type - the type of this flag, as one of the symbols [ :string, :symbol, :int, :float, :bool ]
name - the name of this flag, as a symbol
default_value - the default value of the flag. The current value always equals the default when the
    Flag object is initialized.
description - a String describing the flag.
definition_file - a String describing the path to the file where this flag was defined.
validators - a list of FlagValidator objects. Additional validators can be added after the flag is
    constructed, and all of the registered validators are checked whenever a flag assignment is
    performed.


437
438
439
440
441
442
443
444
445
446
# File 'lib/flags.rb', line 437

def initialize(type, name, default_value, description, definition_file, validators)
  @type = type
  @name = name
  @default_value = default_value
  @description = description
  @definition_file = definition_file
  @validators = validators
  self.value = default_value  # use the public setter method which performs type checking
  @is_explicit = false        # @is_explicit must be set to false AFTER calling the value= method
end

Instance Attribute Details

#default_valueObject (readonly)

the default value of the flag



421
422
423
# File 'lib/flags.rb', line 421

def default_value
  @default_value
end

#definition_fileObject (readonly)

The file name where the flag was defined



425
426
427
# File 'lib/flags.rb', line 425

def definition_file
  @definition_file
end

#descriptionObject (readonly)

false unless the flag’s value has been explicitly changed from default. NOTE: if a flag is explicitly set to the default value, this will be true!



424
425
426
# File 'lib/flags.rb', line 424

def description
  @description
end

#is_explicitObject (readonly) Also known as: explicit?

Returns the value of attribute is_explicit.



422
423
424
# File 'lib/flags.rb', line 422

def is_explicit
  @is_explicit
end

#nameObject (readonly)

the name of the flag



419
420
421
# File 'lib/flags.rb', line 419

def name
  @name
end

#typeObject (readonly)

the type of flag, as a symbol



418
419
420
# File 'lib/flags.rb', line 418

def type
  @type
end

#valueObject

the current value of the flag



420
421
422
# File 'lib/flags.rb', line 420

def value
  @value
end

Instance Method Details

#add_validator(validator) ⇒ Object

Adds a new validator object to this flag and immediately validates the current value against it.



470
471
472
473
# File 'lib/flags.rb', line 470

def add_validator(validator)
  @validators.push validator
  validate!(@value)
end

#default?Boolean

Returns true if the flag has not been explicitly set.

Returns:

  • (Boolean)


467
# File 'lib/flags.rb', line 467

def default?; return !explicit?; end

#restore_defaultObject

Restores the default value of the flag.



458
459
460
461
# File 'lib/flags.rb', line 458

def restore_default()
  @is_explicit = false
  @value = @default_value
end