RAA - preferences

preferences / 0.3

Short description: Persistent preferences for applications
Category: Library/Development
Status: beta
Created: 2004-12-06 18:43:30 GMT
Last update: 2006-05-29 21:50:57 GMT
Owner: Joel VanderWerf (Projects of this owner)
Homepage: http://redshift.sourceforge.net/preferences
Download: http://redshift.sourceforge.net/preferences
License: Ruby's
Dependency:
None
Description:

Preferences

The Preferences class is an easy way to make variables in an application persist in a file. The file is human readable, through the magic of YAML. Preferences vary from user to user, so typically the file used for persistence will be chosen based on the user’s environment as well as the name of the app. Any pair of methods that look like a reader/writer pair can be persisted, so existing variables can be stored with little additional fuss and bother.

Synopsis

  require 'preferences'

  PREFS = Preferences.new("/tmp/prefs")

  class Window
    attr_accessor :x, :y, :z
    def initialize
      PREFS.register "my app/window" do |entry|
        entry.var "x", "y" => "default_y"
        entry.var "z"
      end
    end

    def run
      @x = 7 # emulate user's interaction
    end
  end

  Window.new.run
  PREFS.save

After executing this code, /tmp/prefs contains:

  my app:
    window:
      x: 7
      y: default_y
      z:

When the program is run again, these saved preferences will be loaded into the variables when the PREFS.register method is called.

New in preferences 0.3

  • Added the #register API, which combines and simplifies #register_pref_key and #register_pref_var calls. Instead of
      PREFS.register_pref_key self, "my app/window"
      PREFS.register_pref_var self, :x, :y => "default_y"
      PREFS.register_pref_var self, "z"
    

    you simply do

      PREFS.register "my app/window" do |entry|
        entry.var :x, :y => "default_y"
        entry.var "z"
      end
    
  • Changed examples/simple.rb, examples/foursplit-prefs.rb, and intro.txt to use this new API.
  • Added life-cycle.rb example.
  • Updated foursplit-prefs.rb to work with fox14.
  • Added some error checking, in case of a prefs file that is valid YAML but not usable for preferences (e.g., "— foo"). This now raises Preferences::LoadError.
Versions: [0.3 (2006-05-29)] [0.2 (2005-01-05)] [0.1 (2004-12-06)]

Edit this project (for project owner)

back to RAA top