class with default values

U

Une bévue

i'd like to have a class, saying "Preferences", with values varying in
number over the time (application updates)

for example at version 0.00001 i have the following :

@default_index=0


for version 0.00002 i had a new field to the class with the
corresponding default value :

@time_out=30

then, i want for any version to any newer version the default values are
set-up only they don't exist as a defined value...

hopping i'm clear enough, how could i do that ???
 
R

Robert Klemme

Une said:
i'd like to have a class, saying "Preferences", with values varying in
number over the time (application updates)

for example at version 0.00001 i have the following :

@default_index=0


for version 0.00002 i had a new field to the class with the
corresponding default value :

@time_out=30

then, i want for any version to any newer version the default values are
set-up only they don't exist as a defined value...

I don't exactly understand this last sentence. Can you elaborate?
hopping i'm clear enough, how could i do that ???

Honestly, I'm not really sure what you're up to. When you speak about
"version", do you mean version in some kind of version control system
(CVS)? Do you need several versions of your class to coexist at
runtime? Your Preferences class might actually be OpenStruct. You just
need a mechanism to set default values (i.e. a constructor method)


$ irb -r ostruct
irb(main):001:0> os=OpenStruct.new :foo => "foo", :bar => "bar"
=> #<OpenStruct bar="bar", foo="foo">
irb(main):002:0> os.foo
=> "foo"
irb(main):003:0> os.bar
=> "bar"
irb(main):004:0>

Kind regards

robert
 
U

Une bévue

Robert Klemme said:
I don't exactly understand this last sentence. Can you elaborate?


first the versions i'm speaking about are the versions of my
application.

for example the first release might have those attributes :

attr_accessor :roundTripTimeValues, :roundTripTimeDefaultValue
attr_accessor :defaultSettingsDir


while the second version might have more attributes :

attr_accessor :roundTripTimeValues, :roundTripTimeDefaultValue
attr_accessor :defaultSettingsDir
attr_accessor :firstRun, :defaultVolumeName


the users i have might have a second version directly (new user) or the
first one (old user) which i want to updates automatically.

for the time being i've done that thru the usage of a defaults method

(in my case the preferences are saved into a yaml file)

here is the first version class :
-----------------------------------------------------------------------
#!/usr/bin/env ruby

HOME=ENV['HOME']
PREFS_FILE="#{HOME}/Desktop/org.ytho.MacSOUPSwitcher.yml"

class Preferences
attr_accessor :roundTripTimeValues, :roundTripTimeDefaultValue
attr_accessor :defaultSettingsDir

def initialize
@roundTripTimeValues=[]
@roundTripTimeDefaultValue
@defaultSettingsDir
end

def defaults
@roundTripTimeValues=[ 5, 15, 30, 60] if @roundTripTimeValues == []
@roundTripTimeDefaultValue=15 if @roundTripTimeDefaultValue == nil
@defaultSettingsDir="#{HOME}" if @defaultSettingsDir == nil
self.save
end

def save
File.open("#{PREFS_FILE}", "w") {|f| f.write(self.to_yaml)}
end

def to_s
s="Préférences :\n"
s+=" roundTripTimeValues = #{self.roundTripTimeValues}\n"
s+=" roundTripTimeDefaultValue =
#{self.roundTripTimeDefaultValue}\n"
s+=" defaultSettingsDir = #{self.defaultSettingsDir}\n"
return s
end

end
-----------------------------------------------------------------------


the second version with added attributes :
-----------------------------------------------------------------------
#!/usr/bin/env ruby

HOME=ENV['HOME']
VOLUMES="/volumes"
PREFS_FILE="#{HOME}/Desktop/org.ytho.MacSOUPSwitcher.yml"

class Preferences
attr_accessor :roundTripTimeValues, :roundTripTimeDefaultValue
attr_accessor :defaultSettingsDir
attr_accessor :firstRun, :defaultVolumeName

def initialize
@roundTripTimeValues=[]
@roundTripTimeDefaultValue
@defaultSettingsDir
@firstRun
@defaultVolumeName
end

def defaults
@roundTripTimeValues=[ 5, 15, 30, 60] if @roundTripTimeValues == []
@roundTripTimeDefaultValue=15 if @roundTripTimeDefaultValue == nil
@defaultSettingsDir="#{HOME}" if @defaultSettingsDir == nil
@firstRun=true if @firstRun == nil
@defaultVolumeName=self.findDefaultVolume if @defaultVolumeName ==
nil
self.save
end

def save
File.open("#{PREFS_FILE}", "w") {|f| f.write(self.to_yaml)}
end

def to_s
s="Préférences :\n"
s+=" roundTripTimeValues = #{self.roundTripTimeValues}\n"
s+=" roundTripTimeDefaultValue =
#{self.roundTripTimeDefaultValue}\n"
s+=" defaultSettingsDir = #{self.defaultSettingsDir}\n"
s+=" firstRun = #{self.firstRun}\n"
s+=" defaultVolumeName = #{self.defaultVolumeName}\n"
return s
end

def findDefaultVolume
Dir.open("#{VOLUMES}") do |dir|
dir.each do |file|
if File.symlink? "#{VOLUMES}/#{file}" and `readlink -n
"#{VOLUMES}/#{file}"`.chomp == "/"
return file
end
end
end
end

end

-----------------------------------------------------------------------


then when reading a yaml preferences file of any version, if the
software of of the second, those preferences will be updates
automatically by using :


def prefsSetUp
if FileTest.exists?("#{PREFS_FILE}")
prefs=YAML::load(File.open("#{PREFS_FILE}"))
if prefs.class == Preferences
prefs.defaults # set new default values if not allready assignied
prefs.save
return prefs
end
else
prefs=Preferences.new
prefs.defaults
prefs.save
return prefs
end
end


then, i want to know if their are other better and/or clever way to do
the same things ???
 
R

Robert Klemme

Une said:
then, i want to know if their are other better and/or clever way to do
the same things ???

The easiest solution is to use a Hash. Initialize with default values
for all settings of the current version, then update with a Hash read
from persistent storage:

irb(main):002:0> h={:foo=>:bar, :bar => :foo}
=> {:foo=>:bar, :bar=>:foo}
irb(main):003:0> h.update:)foo=>1)
=> {:foo=>1, :bar=>:foo}
irb(main):004:0> h
=> {:foo=>1, :bar=>:foo}

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,206
Messages
2,571,075
Members
47,681
Latest member
hanrywillsonnn

Latest Threads

Top