Class: Params::Param
- Inherits:
-
Object
- Object
- Params::Param
- Defined in:
- lib/params.rb
Overview
Represents a parameter.
Instance Attribute Summary collapse
-
#desc ⇒ Object
Returns the value of attribute desc.
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(name, value, type, desc) ⇒ Param
constructor
supported types are: String, Integer, Float, Boolean, Path or Complex.
-
#value_type_check(value) ⇒ Object
value_type_check method: 1.
Constructor Details
#initialize(name, value, type, desc) ⇒ Param
supported types are: String, Integer, Float, Boolean, Path or Complex
133 134 135 136 137 138 |
# File 'lib/params.rb', line 133 def initialize(name, value, type, desc) @name = name @type = type @desc = desc @value = value_type_check value end |
Instance Attribute Details
#desc ⇒ Object
Returns the value of attribute desc.
66 67 68 |
# File 'lib/params.rb', line 66 def desc @desc end |
#name ⇒ Object
Returns the value of attribute name.
64 65 66 |
# File 'lib/params.rb', line 64 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
67 68 69 |
# File 'lib/params.rb', line 67 def type @type end |
#value ⇒ Object
Returns the value of attribute value.
65 66 67 |
# File 'lib/params.rb', line 65 def value @value end |
Instance Method Details
#value_type_check(value) ⇒ Object
value_type_check method:
-
Check if member:‘type’ is one of:Integer, Float, String or Boolean.
-
input parameter:‘value’ class type is valid to override this parameter.
-
Return value. The value to override with correct type. A cast from integer to Float will be made for Float parameters which are set with integer values.
-
Check will be skipped for nil value.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/params.rb', line 75 def value_type_check(value) if value.nil? return value end case( @type ) when 'Integer' then if not @value.nil? if not ((value.class.eql? Integer) or (value.class.eql? Fixnum)) raise "Parameter:'#{@name}' type:'Integer' but value type to override " \ "is:'#{value.class}'." end end when 'Float' then if not @value.nil? if not value.class.eql? Float if not ((value.class.eql? Integer) or (value.class.eql? Fixnum)) raise("Parameter:'#{@name}' type:'Float' but value type to override " \ "is:'#{value.class}'.") else return value.to_f end end end when 'String' then when 'Path' then # TODO(kolman): Override the type check with regexp of path in Linux and/or Windows if not @value.nil? if not value.class.eql? String raise("Parameter:'#{@name}' type:'String' but value type to override " \ "is:'#{value.class}'.") end end when 'Boolean' then if not @value.nil? if not((value.class.eql? TrueClass) or (value.class.eql? FalseClass)) raise("Parameter:'#{@name}' type:'Boolean' but value type to override " \ "is:'#{value.class}'.") end end when 'Complex' then unless @value.nil? unless (value.class.eql? Hash) or (value.class.eql? Array) raise("Parameter:'#{@name}' type:'Complex' but value type to override " \ "is:'#{value.class}'.") end end else raise("Parameter:'#{@name}' type:'#{@value.class}' but parameter " \ "type to override:'#{value.class}' is not supported. " + \ "Supported types are:Integer, Float, String or Boolean.") end return value end |