Class: RUTL::View

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

Overview

Base view class. It’s used to call the magical method_messing stuff to parse the view object files into actual objects.

Constant Summary collapse

@@loaded_views =

Intentionally use a class variable to hald views. Once they’re all loaded they are all loaded for everyone. rubocop:disable Style/ClassVars

[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface) ⇒ View

rubocop:enable Style/ClassVars



43
44
45
46
47
48
49
50
51
# File 'lib/rutl/view.rb', line 43

def initialize(interface)
  @interface = interface
  # Dirty trick because we're loading all of view classes from files and
  # then initializing them, calling their layout methods to do magic.
  # The view class knows what views are loaded.
  return if @@loaded_views.include?(self.class)
  layout
  @@loaded_views << self.class
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(element, *args, &_block) ⇒ Object

This creates a new element instance whenever it’s called. Because of that we can’t keep state in any element objects. That seems like a good thing, actually. Called by layout method on views.

Hard to make shorter. rubocop:disable Metrics/MethodLength



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rutl/view.rb', line 93

def method_missing(element, *args, &_block)
  name, selectors, rest = args
  context = RUTL::Element::ElementContext.new(destinations: rest,
                                              interface: @interface,
                                              selectors: selectors)
  case element
  when /button/, /checkbox/, /element/, /link/
    add_method(name: name, context: context, klass: element)
  when /text/
    add_method(name: name, context: context, klass: element)
    add_method(name: name, context: context, klass: element, setter: true)
  else
    # TODO: replace with a super call. This is useful for debugging for now.
    raise "#{element} NOT FOUND WITH ARGS #{args}!!!"
  end
end

Instance Attribute Details

#interface=(value) ⇒ Object (writeonly)

Written by Application and only used internally.



54
55
56
# File 'lib/rutl/view.rb', line 54

def interface=(value)
  @interface = value
end

Class Method Details

.urlObject

BUGBUG #1: Some view in a generic app should not have URL.

BUGBUG: Kludgy. What do I really want to do here? Make it easy to define a view’s default url and also matchers for view urls for views with variable urls? rubocop:disable Style/TrivialAccessors



17
18
19
# File 'lib/rutl/view.rb', line 17

def self.url
  @url
end

Instance Method Details

#go_to_hereObject

rubocop:enable Style/TrivialAccessors



26
27
28
29
30
# File 'lib/rutl/view.rb', line 26

def go_to_here
  # Ovveride this in base view to have something more
  # complicated than this.
  @interface.driver.navigate.to(url)
end

#loaded?Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/rutl/view.rb', line 32

def loaded?
  # Default to only checking url to see if view loaded.
  url == @interface.driver.current_url
end

#respond_to_missing?(*args) ⇒ Boolean

rubocop:enable Metrics/MethodLength

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rutl/view.rb', line 111

def respond_to_missing?(*args)
  # Is this right at all???
  case args[0].to_s
  when /button/, /checkbox/, /element/, /link/, /text/,
       'driver', 'children', 'loaded?'
    true
  when 'ok_link'
    raise 'OK LINK WAY DOWN HERE IN BASE VIEW!!!'
  else
    # I think it's good to raise but change the message.
    raise 'TODO: BETTER ERROR MESSAGE, PLEASE. I AM SHOUTING!!!\n' \
          'Drew, you hit this most often when checking current view ' \
          "rather than current view class:\n\n #{args}"
    # I think I want to raise instead of returningn false.
  end
end

#urlObject



21
22
23
# File 'lib/rutl/view.rb', line 21

def url
  self.class.url
end