Class: BasicSettings::Type
- Inherits:
-
Object
- Object
- BasicSettings::Type
- Defined in:
- lib/basic_settings.rb
Overview
A type object that checks values.
A Type includes a description string and a testing function. The testing function takes a proposed value and returns either the value itself if it is valid, a converted value if the value can be converted to a valid value, or ILLEGAL_VALUE if the type check failed.
Instance Attribute Summary collapse
-
#description ⇒ String
readonly
The name of the type.
Class Method Summary collapse
-
.for_default_value(value) ⇒ Type
Create and return a Type given a default value.
-
.for_type_spec(type_spec) ⇒ Type
Create and return a Type given a type specification.
Instance Method Summary collapse
-
#call(val) ⇒ Object
Test a value, possibly converting to a legal value.
-
#initialize(description, &block) ⇒ Type
constructor
Create a new Type.
Constructor Details
#initialize(description, &block) ⇒ Type
Create a new Type.
386 387 388 389 |
# File 'lib/basic_settings.rb', line 386 def initialize(description, &block) @description = description.freeze @tester = block end |
Instance Attribute Details
#description ⇒ String (readonly)
The name of the type.
395 396 397 |
# File 'lib/basic_settings.rb', line 395 def description @description end |
Class Method Details
.for_default_value(value) ⇒ Type
Create and return a Type given a default value. See the BasicSettings class documentation for the rules.
445 446 447 448 449 450 451 452 453 454 |
# File 'lib/basic_settings.rb', line 445 def for_default_value(value) case value when nil for_module(::Object) when true, false for_union([true, false]) else for_module(value.class) end end |
.for_type_spec(type_spec) ⇒ Type
Create and return a Type given a type specification. See the BasicSettings class documentation for valid type specifications.
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/basic_settings.rb', line 417 def for_type_spec(type_spec) case type_spec when Type type_spec when ::Module for_module(type_spec) when ::Range for_range(type_spec) when ::Regexp for_regexp(type_spec) when ::Array for_union(type_spec) when ::Proc new("(opaque proc)", &type_spec) when nil, true, false, ::String, ::Symbol, ::Numeric for_scalar(type_spec) else raise ::ArgumentError, "Illegal type spec: #{type_spec.inspect}" end end |
Instance Method Details
#call(val) ⇒ Object
Test a value, possibly converting to a legal value.
404 405 406 |
# File 'lib/basic_settings.rb', line 404 def call(val) @tester.call(val) end |