Class: Params::Param

Inherits:
Object
  • Object
show all
Defined in:
lib/params.rb

Overview

Represents a parameter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, type, desc) ⇒ Param

supported types are: String, Integer, Float, Boolean, Path or Complex



140
141
142
143
144
145
# File 'lib/params.rb', line 140

def initialize(name, value, type, desc)
  @name = name
  @type = type
  @desc = desc
  @value = value_type_check value
end

Instance Attribute Details

#descObject

Returns the value of attribute desc.



73
74
75
# File 'lib/params.rb', line 73

def desc
  @desc
end

#nameObject

Returns the value of attribute name.



71
72
73
# File 'lib/params.rb', line 71

def name
  @name
end

#typeObject

Returns the value of attribute type.



74
75
76
# File 'lib/params.rb', line 74

def type
  @type
end

#valueObject

Returns the value of attribute value.



72
73
74
# File 'lib/params.rb', line 72

def value
  @value
end

Instance Method Details

#value_type_check(value) ⇒ Object

value_type_check method:

  1. Check if member:‘type’ is one of:Integer, Float, String or Boolean.

  2. input parameter:‘value’ class type is valid to override this parameter.

  3. 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.

  4. Check will be skipped for nil value.



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
131
132
133
134
135
136
137
# File 'lib/params.rb', line 82

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