class BitGirder::Core::BitGirderAttribute
Constants
- ATTR_MODIFIERS
- PROCESSOR_BOOLEAN
- PROCESSOR_EXPAND_PATH
- PROCESSOR_FLOAT
- 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)
- PROCESSOR_SYMBOL
- VALIDATION_FILE_EXISTS
- VALIDATION_NONNEGATIVE
- VALIDATION_NOT_EMPTY
Implies not nil
- VALIDATION_NOT_NIL
- VALIDATION_OPT_FILE_EXISTS
- VALIDATION_POSITIVE
Public Class Methods
new( argh )
click to toggle source
# 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
Public Instance Methods
get_default_value()
click to toggle source
# 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 )
click to toggle source
# File lib/bitgirder/core.rb, line 542 def get_instance_value( inst ) inst.instance_variable_get( @id_sym ) end
Private Instance Methods
check_valid_modifiers( supplied )
click to toggle source
Rather than have a class def silently fail on a mis-typed attribute modifier (“:processr” instead of “:processor”) we explicitly check and fail if any of the supplied mods are not one expected by this class
# File lib/bitgirder/core.rb, line 476 def check_valid_modifiers( supplied ) supplied.each do |key| ATTR_MODIFIERS.include?( key ) or raise InvalidModifier.new( "Invalid attribute modifier: #{key.inspect}" ) end end
get_processor( p )
click to toggle source
# File lib/bitgirder/core.rb, line 486 def get_processor( p ) case p when nil, Proc then p when :boolean then PROCESSOR_BOOLEAN when :symbol then PROCESSOR_SYMBOL when :integer then PROCESSOR_INTEGER when :float then PROCESSOR_FLOAT when :expand_path then PROCESSOR_EXPAND_PATH when Class if p.ancestors.include?( BitGirderClass ) lambda { |o| p.as_instance( o ) } else raise ArgumentError, "Not a #{BitGirderClass}: #{p}" end else raise "Unrecognized processor: #{p}" end end
get_validation( v )
click to toggle source
# File lib/bitgirder/core.rb, line 508 def get_validation( v ) case v when :not_nil then VALIDATION_NOT_NIL when :nonnegative then VALIDATION_NONNEGATIVE when :positive then VALIDATION_POSITIVE when :file_exists then VALIDATION_FILE_EXISTS when :not_empty then VALIDATION_NOT_EMPTY when :opt_file_exists then VALIDATION_OPT_FILE_EXISTS else v end end