Module: SKUI::TypeCheck

Defined in:
src/SKUI/typecheck.rb

Overview

Module collection of ‘Proc`s that can be used to check types when defining properties. The Proc should return the value given. Return unmodified to preserve the value as it was given, or made modifications, such as for a boolean type check you want to convert the value, that might be any value, to a true boolean.

Examples:

Make a property ensure the set value is a valid colour value.

class Container < Control
  include ControlManager
  prop( :background_color, &TypeCheck::COLOR )
  prop( :enabled,          &TypeCheck::BOOLEAN )
 end

Since:

  • 1.0.0

Constant Summary collapse

BOOLEAN =

Since:

  • 1.0.0

Proc.new { |value|
  # Cast the value into true boolean values.
  value ? true : false
}
BUTTON =

Since:

  • 1.0.0

Proc.new { |value|
  unless value.is_a?( Button )
    raise( ArgumentError, 'Not a valid button.' )
  end
  value
}
COLOR =

Since:

  • 1.0.0

Proc.new { |value|
  unless value.is_a?( Sketchup::Color ) || SystemColor.valid?( value )
    raise( ArgumentError, 'Not a valid color.' )
  end
  value
}
CONTAINER =

Since:

  • 1.0.0

Proc.new { |value|
  unless value.is_a?( ControlManager )
    raise( ArgumentError, 'Not a valid container control.' )
  end
  value
}
CONTROL =

Since:

  • 1.0.0

Proc.new { |value|
  unless value.is_a?( Control )
    raise( ArgumentError, 'Not a valid control.' )
  end
  value
}
FONT =

Since:

  • 1.0.0

Proc.new { |value|
  unless value.is_a?( Font ) || SystemFont.valid?( value )
    raise( ArgumentError, 'Not a valid font.' )
  end
  value
}
INTEGER =

Since:

  • 1.0.0

Proc.new { |value|
  unless value.respond_to?( :to_i )
    raise( ArgumentError, 'Not a valid Integer value.' )
  end
  value.to_i
}
TEXTALIGN =

Since:

  • 1.0.0

Proc.new { |value|
  unless [:left, :center, :right].include?( value )
    raise( ArgumentError, 'Not a valid alignment value.' )
  end
  value
}
STRING =

Since:

  • 1.0.0

Proc.new { |value|
  unless value.respond_to?( :to_s )
    raise( ArgumentError, 'Not a valid String value.' )
  end
  value.to_s
}
SYMBOL =

Since:

  • 1.0.0

Proc.new { |value|
  unless value.is_a?( Symbol )
    raise( ArgumentError, 'Not a Symbol.' )
  end
  value
}