Class: Object

Inherits:
BasicObject
Defined in:
lib/tdriver/util/common/object.rb

Overview

extend Ruby Object class functionality

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

TODO: document me

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/tdriver/util/common/object.rb', line 24

def blank?

  respond_to?( :empty? ) ? empty? : !self

end

#check_type(types, message = "wrong argument type $1 (expected $2)") ⇒ Object

Compare receiver object type with given types. Raises exception is class name does not equal.

Raises:

  • (TypeError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
# File 'lib/tdriver/util/common/object.rb', line 61

def check_type( types, message = "wrong argument type $1 (expected $2)" )

  # raise exception if message is not type of String
  raise TypeError, "wrong argument type #{ message.class } for message (expected String)", caller unless message.kind_of?( String )

  # create array of types
  type_array = Array( types )

  # default result value
  found = false

  # collect verbose type list
  verbose_type_list = type_array.each_with_index.collect{ | type, index | 

    raise TypeError, "invalid argument type #{ type } for check_type. Did you mean #{ type.class }?", caller unless type.kind_of?( Class )

    if kind_of?( type )

      found = true 

      break

    end

    # result string, separate types if multiple types given
    "#{ ( ( index > 0 ) ? ( index + 1 < type_array.count ? ", " : " or " ) : "" ) }#{ type.to_s }"
        
  }

  # raise exception if type did not match
  unless found

    # convert macros
    [ self.class, verbose_type_list.join, inspect ].each_with_index{ | param, index | message.gsub!( "$#{ index + 1 }", param.to_s ) }

    # raise the exception
    raise TypeError, message, caller

  end

  # pass self as return value
  self

end

#false?Boolean

TODO: document me

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/tdriver/util/common/object.rb', line 38

def false?

  false

end

#meta_def(method_name, &block) ⇒ Object

define method to class instance



54
55
56
57
58
# File 'lib/tdriver/util/common/object.rb', line 54

def meta_def( method_name, &block )

  ( class << self; self; end ).instance_eval{ define_method method_name, &block }
  
end

#not_blank(message = "object must not be blank", exception = ArgumentError) ⇒ Object

TODO: document me

Raises:

  • (exception)


45
46
47
48
49
50
51
# File 'lib/tdriver/util/common/object.rb', line 45

def not_blank( message = "object must not be blank", exception = ArgumentError )

  raise exception, message, caller if blank? 

  self

end

#not_nil(message = "Value must not be nil", exception = ArgumentError) ⇒ Object

Raises:

  • (exception)


106
107
108
109
110
111
112
# File 'lib/tdriver/util/common/object.rb', line 106

def not_nil( message = "Value must not be nil", exception = ArgumentError )

  raise exception, message, caller unless self

  self

end

#true?Boolean

TODO: document me

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/tdriver/util/common/object.rb', line 31

def true?

  false

end

#validate(values, message = "Unexpected value $3 for $1 (expected $2)") ⇒ Object

Raises:

  • (TypeError)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/tdriver/util/common/object.rb', line 114

def validate( values, message = "Unexpected value $3 for $1 (expected $2)" )

  # raise exception if message is not type of String
  raise TypeError, "wrong argument type #{ message.class } for message (expected String)", caller unless message.kind_of?( String )

  # create array of values
  values_array = Array( values )

  # default result value
  found = false

  # collect verbose type list
  verbose_values_list = values_array.each_with_index.collect{ | value, index | 

    raise TypeError, "Invalid argument type #{ value.class } for value (expected #{ self.class })", caller unless value.kind_of?( self.class )

    if self == value
    
      found = true 
      
      break
      
    end

    # result string, separate types if multiple types given
    "#{ ( ( index > 0 ) ? ( index + 1 < values_array.count ? ", " : " or " ) : "" ) }#{ value.inspect }"
        
  }

  # raise exception if value was not found
  unless found

    # convert macros
    [ self.class, verbose_values_list.join, inspect ].each_with_index{ | param, index | message.gsub!( "$#{ index + 1 }", param.to_s ) }

    # raise the exception
    raise ArgumentError, message, caller

  end

  # pass self as return value
  self
      
end