Class: TDriver::StackableValue

Inherits:
Object
  • Object
show all
Defined in:
lib/tdriver/util/common/stackable.rb

Instance Method Summary collapse

Constructor Details

#initialize(value, explicit_format = []) ⇒ StackableValue

TODO: document me



25
26
27
28
29
30
31
32
33
# File 'lib/tdriver/util/common/stackable.rb', line 25

def initialize( value, explicit_format = [] )

  @explicit_format = explicit_format.kind_of?( Array ) ? explicit_format : [ explicit_format ]

  @stack = []

  push( value )

end

Instance Method Details

#==(value) ⇒ Object Also known as: eql?



158
159
160
161
162
# File 'lib/tdriver/util/common/stackable.rb', line 158

def ==( value )

  @stack.last == value

end

#[](value) ⇒ Object

TODO: document me



132
133
134
135
136
137
138
139
# File 'lib/tdriver/util/common/stackable.rb', line 132

def []( value )

  # return last one if index is too high
  value = -1 if ( value > @stack.count - 1 )

  @stack[ value ]

end

#[]=(position, value) ⇒ Object

TODO: document me



142
143
144
145
146
147
148
149
# File 'lib/tdriver/util/common/stackable.rb', line 142

def []=( position, value )

  # return last one if index is too high
  value = -1 if ( position > @stack.count - 1 )

  @stack[ position ] = value

end

#countObject Also known as: size

TODO: document me



108
109
110
111
112
113
# File 'lib/tdriver/util/common/stackable.rb', line 108

def count

  # return size of the stack array
  @stack.count

end

#firstObject

TODO: document me



116
117
118
119
120
121
# File 'lib/tdriver/util/common/stackable.rb', line 116

def first

  # return first value in stack array
  @stack.first

end

#inspectObject

TODO: document me



100
101
102
103
104
105
# File 'lib/tdriver/util/common/stackable.rb', line 100

def inspect

  # return inspect of last value in stack array
  @stack.last.inspect

end

#kind_of?(klass) ⇒ Boolean

TODO: document me

Returns:

  • (Boolean)


165
166
167
168
169
170
# File 'lib/tdriver/util/common/stackable.rb', line 165

def kind_of?( klass )

  # compary stacked value class 
  @stack.last.kind_of?( klass ) or klass == TDriver::StackableValue

end

#lastObject Also known as: value

TODO: document me



124
125
126
127
128
129
# File 'lib/tdriver/util/common/stackable.rb', line 124

def last

  # return last value in stack array
  @stack.last

end

#popObject

TODO: document me



64
65
66
67
68
69
70
71
# File 'lib/tdriver/util/common/stackable.rb', line 64

def pop

  @stack.pop unless ( @stack.count == 1 )

  # return last value from stack
  @stack.last

end

#push(value) ⇒ Object Also known as: <<

TODO: document me



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tdriver/util/common/stackable.rb', line 36

def push( value )

  unless @explicit_format.empty?

    found = false

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

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

      found = true if value.kind_of?( type )

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

    raise TypeError, "wrong variable format #{ value.class } for stackable value (expected #{ verbose_type_list })" unless found

  end

  # add to stack
  @stack << value

end

#restoreObject

TODO: document me



74
75
76
77
78
79
80
81
# File 'lib/tdriver/util/common/stackable.rb', line 74

def restore

  @stack = [ @stack.first ]

  # return first value in stack array
  @stack.last

end

#set(value) ⇒ Object Also known as: value=

TODO: document me



152
153
154
155
156
# File 'lib/tdriver/util/common/stackable.rb', line 152

def set( value )

  @stack[ -1 ] = value

end

#stacked?Boolean

TODO: document me

Returns:

  • (Boolean)


84
85
86
87
88
89
# File 'lib/tdriver/util/common/stackable.rb', line 84

def stacked?

  # determine if there is values in stack
  @stack.count > 1

end

#to_sObject

TODO: document me



92
93
94
95
96
97
# File 'lib/tdriver/util/common/stackable.rb', line 92

def to_s

  # return last value in stack array as string
  @stack.last.to_s

end