Class: OperaWatir::Preferences

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/operawatir/preferences.rb

Overview

OperaWatir::Preferences enables you to access the browser preferences in the Opera web browser.

Examples:


The Preferences object is created automatically when creating a new
Browser object and is exposed as an interface on that object:

  browser.preferences
  => <OperaWatir::Preferences>

You can interact with this object as you would with any Ruby object.
Sections and entries are exposed as Rubyized method names, which
means that you can access them like this:

  browser.preferences.interface_colors             # a section
  browser.preferences.interface_colors.background  # an entry

Over the preference object itself or over a section you can also use
built-in Ruby convenience methods such as:

  browser.preferences.first             # first section
  browser.preferences.interface_colors  # first entry in section
  browser.preferences.size              # number of sections

This is a full list of available convenience methods:

  * []
  * each
  * length
  * size
  * first
  * last
  * empty?

Essentially, the preference and section objects are collections
(kind of like arrays) that allows you to also iterate over them:

  browser.preferences.each_with_index do |section, index|
    puts "Section No. #{index} is called `#{section.key}'"
  end

  browser.preferences.interface_colors.each { |e| e.value = '#cccccc' }

  browser.preferences.interface_colors[5].value
  => '#cccccc'

On each section, the follow getters are available in addition to the
convenience methods mentioned above:

  * section?  # Returns true/false based on whether it's a section
  * exists?   # Returns true/false based on whether it exists

On each entry, the following getters and setters are available in
addition to the convenience methods mentioned above:

  * value     # Returns current preference's value
  * value=    # Sets current preference's value to provided string
  * default   # Returns current preference's default
  * default!  # Sets current preference to its default

This means you can do crazy stuff like resetting all the preferences
in `opera:config` to their standard value like this:

  browser.preferences.each do |section|
    section.each do |entry|
      entry.default!
    end
  end

Defined Under Namespace

Classes: Section

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser) ⇒ Preferences

The OperaWatir::Preferences object is created automatically when you create an OperaWatir::Browser object, and is available as Browser#prefrences.

Examples:


browser.preferences.method_name

Parameters:

  • browser (Object)

    An OperaWatir::Browser object.



121
122
123
124
125
126
127
128
129
# File 'lib/operawatir/preferences.rb', line 121

def initialize(browser)
  self.browser, self.driver = browser, browser.driver

  raw_prefs = driver.listAllPrefs.to_a
  @_prefs = {}
  @_prefs = raw_prefs.map { |s| Section.new(self, s) }.sort_by { |s| s.key }

  @old_prefs = _prefs
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

When calling Preferences#any_method_name, the ‘any_method_name` will be caught by this method.

This is the standard way of looking up sections. ‘any_method_name` should be replaced by a methodized version of the section as it appears in the Opera preferences list which you can find at `opera:config`.

Examples:


browser.preferences.interface_colors
# will return section “Interface Colors”

Parameters:

  • section

    method to look up in preferences

Returns:

  • (Object)

    a Preferences::Section object



157
158
159
160
161
162
163
# File 'lib/operawatir/preferences.rb', line 157

def method_missing(method)
  method = method.to_s

  if _prefs.any? { |s| s.method == method }
    _prefs.find { |s| s.method == method }
  end
end

Instance Attribute Details

#browserObject

Returns the value of attribute browser.



107
108
109
# File 'lib/operawatir/preferences.rb', line 107

def browser
  @browser
end

#driverObject

Returns the value of attribute driver.



107
108
109
# File 'lib/operawatir/preferences.rb', line 107

def driver
  @driver
end

Instance Method Details

#exists?Boolean Also known as: exist?

Checks if any preferences exists in your Opera build. If true, there are preferences available, false otherwise.

Returns:

  • (Boolean)

    whether any preference exists



214
215
216
# File 'lib/operawatir/preferences.rb', line 214

def exists?
  !_prefs.empty?
end

#to_aArray

Returns a list of all preferences in array form. This can be used for external parsing. If you wish to manipulate or iterate through the list of preferences, consider using the built-in Ruby iterator #each (with friends).

Returns:

  • (Array)

    list of preferences



203
204
205
# File 'lib/operawatir/preferences.rb', line 203

def to_a
  _prefs.dup
end

#to_sString

Retrieves a human-readable list of Opera preferences available. This is used for convenience and should never be parsed. Consider using the built-in Ruby iterator #each (with friends) or Preferences#to_a for that.

Returns:

  • (String)

    list of preferences



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/operawatir/preferences.rb', line 174

def to_s
  text = ''

  _prefs.each do |s|
    text << "#{s.method}\n"

    s.each do |k|
      text << "  #{k.method}\n"
      text << "    type:     #{k.type.inspect}\n"
      text << "    value:    #{k.value.inspect}\n"
      text << "    default:  #{k.default.inspect}\n"
      text << "    enabled:  #{k.enabled?.inspect}\n"
    end

    text << "\n"
  end

  text
end