Class: Rtml::Test::Screen

Inherits:
Object
  • Object
show all
Defined in:
lib/rtml/test/screen.rb

Overview

Used for modeling the current screen by Rtml::Test::TmlApplication.

Instance Method Summary collapse

Constructor Details

#initialize(tml) ⇒ Screen

Returns a new instance of Screen.



5
6
7
# File 'lib/rtml/test/screen.rb', line 5

def initialize(tml)
  @screen = tml
end

Instance Method Details

#==(screen) ⇒ Object



9
10
11
# File 'lib/rtml/test/screen.rb', line 9

def ==(screen)
  id == screen.id
end

Returns the hyperlink Hpricot element, if there’s only one; nil if there are none; and raises an Rtml::Errors::ApplicationError if there are multiple links (because there’s no inference as to which link to follow).



46
47
48
49
50
51
52
53
54
55
# File 'lib/rtml/test/screen.rb', line 46

def autoselect_hyperlink
  if !(links = hyperlinks).empty?
    if links.length > 1
      raise Rtml::Errors::ApplicationError, "Cannot auto-select hyperlink: too many choices on screen #{id.inspect}"
    end
    links.shift
  else
    nil
  end
end

#card_readersObject



26
27
28
# File 'lib/rtml/test/screen.rb', line 26

def card_readers
  ((@screen / "card") || [])
end

#choicesObject

Returns an array of URIs that can be chosen by the user at this time, or an empty array if none can be chosen. This inversely coincides with #next_screen in that choices are only available if #next_screen returns an empty array, just as #next_screen will only return an empty array if choices are available.



89
90
91
92
93
94
95
96
97
# File 'lib/rtml/test/screen.rb', line 89

def choices
  choices = (@screen / "a").collect do |a|
    a['href']
  end
  ((@screen / "variant") || []).each do |variant|
    choices << variant['uri'] if variant['key']
  end
  choices
end

#display?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/rtml/test/screen.rb', line 17

def display?
  !((@screen / "display") || []).empty?
end


57
58
59
# File 'lib/rtml/test/screen.rb', line 57

def hyperlinks
  (@screen / "a") || []
end

#idObject



13
14
15
# File 'lib/rtml/test/screen.rb', line 13

def id
  @screen['id']
end

#input?Boolean

Returns true if this screen is an input screen (that is, if it will halt program flow waiting for user input).

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/rtml/test/screen.rb', line 63

def input?
  return true if !choices.empty?
  Rtml::Test::TmlApplication::USER_INPUT_ELEMENTS.each { |ele| return true if !(@screen / ele).empty? }
  false
end

#next_elementObject

Returns the next element for this screen, or nil if there is none.



39
40
41
# File 'lib/rtml/test/screen.rb', line 39

def next_element
  ((@screen / "next") || []).shift
end

#possible_variantsObject

Returns an array of Hashes representing all possible non-user interaction-requiring variants from this screen. These are returned regardless of whether user interation is required at this time.

The hashes are laid out thusly:

{ :uri => "destination_uri", :key => "hotkey", :timeout => "seconds", :lo => "left_operand",
  :op => "operation", :ro => "right_operand" }

Note that some of these keys may be omitted, according to the TML rules for the variant element.

For TML next elements, all keys except :uri are omitted.



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rtml/test/screen.rb', line 111

def possible_variants
  variants = ((@screen / "variant") || []).collect do |variant|
    { :uri => variant['uri'], :key => variant['key'], :timeout => variant['timeout'],
      :lo  => variant['lo'],  :op  => variant['op'],  :ro      => variant['ro'] }.optionalize
  end
  if !(nxt = (@screen / "next") || []).empty?
    variants << { :uri => nxt.first['uri'] }
  else
    variants << { :uri => @screen['next'] }
  end
  variants
end

#setvarsObject



81
82
83
# File 'lib/rtml/test/screen.rb', line 81

def setvars
  ((@screen / "setvar") || [])
end

#timeoutObject



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rtml/test/screen.rb', line 69

def timeout
  return @screen['timeout'].to_i if @screen['timeout']
  nxt = ((@screen / "next") || []).shift
  if nxt
    return nxt['timeout'].to_i if nxt['timeout']
    ((nxt / "variant") || []).each do |var|
      return var['timeout'].to_i if var['timeout']
    end
  end
  0
end

#uri_for_hotkey(which) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/rtml/test/screen.rb', line 30

def uri_for_hotkey(which)
  if variant = variant_with_hotkey(which)
    return variant
  end
  return @screen[which] if @screen[which]
  nil
end

#variant_with_hotkey(which) ⇒ Object



21
22
23
24
# File 'lib/rtml/test/screen.rb', line 21

def variant_with_hotkey(which)
  possible_variants.each { |variant| return variant[:uri] if variant[:key] == which }
  nil
end