Class: BitGirder::Core::BitGirderAttribute

Inherits:
Object
  • Object
show all
Includes:
BitGirderMethods
Defined in:
lib/bitgirder/core.rb

Defined Under Namespace

Classes: AttributeError, InvalidModifier, RedefinitionError

Constant Summary collapse

PROCESSOR_BOOLEAN =
lambda { |v| BitGirderMethods.to_bool( v ) }
PROCESSOR_SYMBOL =
lambda { |v| v == nil ? nil : v.to_sym }
PROCESSOR_INTEGER =

If v is not nil we return calling to_i() on it; if it is nil we return it as-is since nil is used to communicate to the user of the attribute that no value whatsoever was provided (nil.to_i otherwise would return 0)

lambda { |v| v == nil ? nil : v.to_i }
PROCESSOR_FLOAT =

See PROCESSOR_INTEGER

lambda { |v| v == nil ? nil : v.to_f }
PROCESSOR_EXPAND_PATH =
lambda { |v| File.expand_path( v ) }
VALIDATION_NOT_NIL =
lambda { |val| raise "Missing value" if val == nil }
VALIDATION_NOT_EMPTY =

Implies not nil

lambda { |val| 

    VALIDATION_NOT_NIL.call( val )
    raise "Need at least one" if val.empty? 
}
VALIDATION_NONNEGATIVE =
lambda { |val| val >= 0 or raise "value < 0" }
VALIDATION_POSITIVE =
lambda { |val| 

    VALIDATION_NOT_NIL.call( val )
    val > 0 or raise "value <= 0" 
}
VALIDATION_FILE_EXISTS =
lambda { |val|
    
    VALIDATION_NOT_NIL.call( val )

    unless File.exist?( val )
        raise "File or directory #{val} does not exist" 
    end
}
VALIDATION_OPT_FILE_EXISTS =
lambda { |val|
    val && VALIDATION_FILE_EXISTS.call( val )
}
ATTR_MODIFIERS =
[
    :identifier, 
    :default, 
    :description, 
    :validation, 
    :processor, 
    :is_list,
    :list_validation,
    :required,
    :mutable
]

Constants included from BitGirderMethods

BitGirder::Core::BitGirderMethods::PARAM_TYPE_ARG, BitGirder::Core::BitGirderMethods::PARAM_TYPE_ENVVAR, BitGirder::Core::BitGirderMethods::PARAM_TYPE_KEY

Instance Method Summary collapse

Methods included from BitGirderMethods

argv_to_argh, check_fail_prefix, class_name_to_sym, code, compares_to, console, ext_to_class_name, ext_to_sym, has_env, has_key, has_keys, nonnegative, not_nil, positive, raisef, set_from_key, set_var, split_argv, sym_to_cli_switch, sym_to_ext_id, to_bool, unpack_argv_array, unpack_argv_hash, warn

Constructor Details

#initialize(argh) ⇒ BitGirderAttribute

Returns a new instance of BitGirderAttribute.



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/bitgirder/core.rb', line 522

def initialize( argh )

    check_valid_modifiers( argh.keys )

    set_from_key( argh, :identifier )
    
    [ :default, :description, :validation, 
      :is_list, :list_validation, :required, :mutable ].each do |sym|
        set_var( sym, argh[ sym ] )
    end

    @validation = get_validation( @validation )
    @list_validation = get_validation( @list_validation )
    
    @processor = get_processor( argh[ :processor ] )

    @id_sym = :"@#@identifier"
end

Instance Method Details

#get_default_valueObject



547
548
549
550
551
552
553
554
555
# File 'lib/bitgirder/core.rb', line 547

def get_default_value
    
    case d = @default
        when Proc then d.call
        when Class then d.new
        when Array, Hash then d.clone
        else d == nil && @is_list ? [] : d
    end
end

#get_instance_value(inst) ⇒ Object



542
543
544
# File 'lib/bitgirder/core.rb', line 542

def get_instance_value( inst )
    inst.instance_variable_get( @id_sym )
end