lib/alexandria/preferences.rb in alexandria-book-collection-manager-0.7.5 vs lib/alexandria/preferences.rb in alexandria-book-collection-manager-0.7.6
- old
+ new
@@ -55,34 +55,28 @@
generic_save_setting(variable_name, @alexandria_settings[variable_name])
end
@changed_settings.clear
end
- def method_missing(id, *args)
- method = id.id2name
- if (match = /(.*)=$/.match(method))
- if args.length != 1
- raise "Set method #{method} should be called with " \
- "only one argument (was called with #{args.length})"
- end
- variable_name = match[1]
- new_value = args.first
- generic_setter(variable_name, new_value)
- else
- unless args.empty?
- raise "Get method #{method} should be called " \
- "without argument (was called with #{args.length})"
- end
- generic_getter(method)
- end
- end
-
def remove_preference(variable_name)
@alexandria_settings.delete(variable_name)
@changed_settings << variable_name
end
+ DEFAULT_VALUES.each_key do |var|
+ define_method(var) { generic_getter var }
+ define_method("#{var}=") { |val| generic_setter var, val }
+ end
+
+ def get_variable(variable_name)
+ generic_getter(variable_name.to_s)
+ end
+
+ def set_variable(variable_name, value)
+ generic_setter(variable_name.to_s, value)
+ end
+
private
##
## GENERIC GETTER and SETTER CODE
##
@@ -138,15 +132,16 @@
##
## GCONFTOOL SET and SET LIST and SET PAIR and UNSET
##
def get_gconf_type(value)
- if value.is_a?(String)
+ case value
+ when String
"string"
- elsif value.is_a?(Integer)
+ when Integer
"int"
- elsif value.is_a?(TrueClass) || value.is_a?(FalseClass)
+ when TrueClass, FalseClass
"bool"
else
"string"
end
end
@@ -166,28 +161,28 @@
`gconftool-2 --type list --list-type #{list_type} --set #{var_path} "#{list}"`
end
end
def make_list_string(list)
- list.map! { |x| x.gsub(/\"/, '\\"') } if get_gconf_type(list.first) == "string"
+ list.map! { |x| x.gsub(/"/, '\\"') } if get_gconf_type(list.first) == "string"
contents = list.join(",")
"[" + contents + "]"
end
def exec_gconf_set(var_path, new_value)
if /cols_width/.match?(var_path)
- puts new_value
+ log.debug { new_value }
# new_value = {}
end
type = get_gconf_type(new_value)
value_str = new_value
if new_value.is_a? String
- new_value = new_value.gsub(/\"/, '\\"')
+ new_value = new_value.gsub(/"/, '\\"')
value_str = "\"#{new_value}\""
end
- puts value_str if /cols_width/.match?(var_path)
+ log.debug { value_str } if /cols_width/.match?(var_path)
`gconftool-2 --type #{type} --set #{var_path} #{value_str}`
end
def exec_gconf_unset(variable_name)
`#{GCONFTOOL} --unset #{APP_DIR + "/" + variable_name}`
@@ -240,18 +235,19 @@
# Make a judgement about the type of the settings we get back from
# gconftool. This is not fool-proof, but it *does* work for the
# range of values used by Alexandria.
def discriminate(value)
- if value == "true" # bool
+ case value
+ when "true" # bool
true
- elsif value == "false" # bool
+ when "false" # bool
false
- elsif /^[0-9]+$/.match?(value) # int
+ when /^[0-9]+$/ # int
value.to_i
- elsif value =~ /^\[(.*)\]$/ # list (assume of type String)
+ when /^\[(.*)\]$/ # list (assume of type String)
Regexp.last_match[1].split(",")
- elsif value =~ /^\((.*)\)$/ # pair (assume of type int)
+ when /^\((.*)\)$/ # pair (assume of type int)
begin
pair = Regexp.last_match[1].split(",")
[discriminate(pair.first), discriminate(pair.last)]
rescue StandardError
[0, 0]