Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Made Preferences a singleton and added #preferences to Kernel as a co…

…nvenience method. Replaced all section classes with subclasses of AbstractPreferencesSection.

Moved AbstractPreferencesSection into it's own file with explanations.
Refactored a bit more.
  • Loading branch information...
commit ff0623337acd30589675d9b355f08ec9d9c35a9d 1 parent 5ef527f
Eloy Durán alloy authored
62 ruby/model/abstract_preferences_section.rb
... ... @@ -0,0 +1,62 @@
  1 +class Preferences
  2 + class << self
  3 + # A hash of all default values for the user defaults
  4 + def default_values
  5 + @default_values ||= {}
  6 + end
  7 +
  8 + # Registers the default values with NSUserDefaults.standardUserDefaults
  9 + # Called at the end of evaluating model/preferences.rb
  10 + def register_default_values!
  11 + NSUserDefaults.standardUserDefaults.registerDefaults(:pref => default_values)
  12 + end
  13 + end
  14 +
  15 + class AbstractPreferencesSection
  16 + class << self
  17 + # The key in the preferences that represents the section class.
  18 + #
  19 + # Preferences::General.section_defaults_key # => :General
  20 + def section_defaults_key
  21 + @section_defaults_key ||= name.split('::').last.to_sym
  22 + end
  23 +
  24 + # The default values defined by this section.
  25 + def section_default_values
  26 + Preferences.default_values[section_defaults_key] ||= {}
  27 + end
  28 +
  29 + # Defines a reader and writer method for a user defaults key for this section.
  30 + #
  31 + # # Defines #confirm_quit and #confirm_quit= and <tt>true</tt> as it's default value.
  32 + # defaults_accessor :confirm_quit, true
  33 + def defaults_accessor(name, default_value)
  34 + section_default_values[name] = default_value
  35 +
  36 + class_eval do
  37 + define_method(name) do
  38 + section_user_defaults[name].to_ruby
  39 + end
  40 +
  41 + define_method("#{name}=") do |value|
  42 + defaults = section_user_defaults.to_ruby
  43 + defaults[name] = value
  44 + self.section_user_defaults = defaults
  45 + value
  46 + end
  47 + end
  48 + end
  49 + end
  50 +
  51 + # The reader method for the preferences for this section.
  52 + def section_user_defaults
  53 + NSUserDefaults.standardUserDefaults[:pref][self.class.section_defaults_key]
  54 + end
  55 +
  56 + # The writer method for the preferences for this section.
  57 + def section_user_defaults=(section_user_defaults)
  58 + defaults = NSUserDefaults.standardUserDefaults[:pref].to_ruby.merge(self.class.section_defaults_key => section_user_defaults)
  59 + NSUserDefaults.standardUserDefaults.setObject_forKey(defaults, :pref)
  60 + end
  61 + end
  62 +end
220 ruby/model/preferences.rb
... ... @@ -1,99 +1,34 @@
1 1 # Created by Satoshi Nakagawa.
2 2 # You can redistribute it and/or modify it under the Ruby's license or the GPL2.
3 3
4   -require 'userdefaultsaccess'
5   -require 'persistencehelper'
6 4 require 'utility'
  5 +require 'singleton'
  6 +require 'model/abstract_preferences_section'
7 7
8 8 class Preferences
9   - include UserDefaultsAccess
  9 + include Singleton
10 10
11   - class AbstractPreferencesSection
12   - class << self
13   - def section_defaults_key
14   - @section_defaults_key ||= name.sub(/Preferences::/, '').to_sym
15   - end
16   -
17   - def section_default_values
18   - Preferences.default_values[section_defaults_key] ||= {}
19   - end
20   -
21   - def defaults_accessor(name, default_value)
22   - section_default_values[name] = default_value
23   -
24   - class_eval do
25   - define_method(name) do
26   - section_user_defaults[name].to_ruby
27   - end
28   -
29   - define_method("#{name}=") do |value|
30   - defaults = section_user_defaults.to_ruby
31   - defaults[name] = value
32   - self.section_user_defaults = defaults
33   - value
34   - end
35   - end
36   - end
37   - end
38   -
39   - def section_user_defaults
40   - NSUserDefaults.standardUserDefaults[:pref][self.class.section_defaults_key]
41   - end
42   -
43   - def section_user_defaults=(section_user_defaults)
44   - defaults = NSUserDefaults.standardUserDefaults[:pref].to_ruby.merge(self.class.section_defaults_key => section_user_defaults)
45   - NSUserDefaults.standardUserDefaults.setObject_forKey(defaults, :pref)
46   - end
47   - end
48   -
49   - class << self
50   - attr_reader :models
51   - def model_attr(*args)
52   - @models ||= []
53   - @models += args
54   - attr_reader(*args)
55   - end
56   -
57   - def default_values
58   - @default_values ||= {}
59   - end
60   -
61   - def register_default_values!
62   - NSUserDefaults.standardUserDefaults.registerDefaults(:pref => default_values)
63   - end
64   - end
65   -
66   - class Keyword
67   - include PersistenceHelper
68   - persistent_attr :words, :dislike_words, :whole_line, :current_nick, :matching_method
  11 + class Keyword < AbstractPreferencesSection
  12 + defaults_accessor :words, []
  13 + defaults_accessor :dislike_words, []
  14 + defaults_accessor :whole_line, false
  15 + defaults_accessor :current_nick, true
69 16
70 17 MATCH_PARTIAL = 0
71 18 MATCH_EXACT_WORD = 1
72   -
73   - def initialize
74   - @words = []
75   - @dislike_words = []
76   - @whole_line = false
77   - @current_nick = true
78   - @matching_method = MATCH_PARTIAL
79   - end
  19 + defaults_accessor :matching_method, MATCH_PARTIAL
80 20 end
81 21
82   - class Dcc
83   - include PersistenceHelper
84   - persistent_attr :first_port, :last_port, :address_detection_method, :myaddress, :auto_receive
  22 + class Dcc < AbstractPreferencesSection
  23 + defaults_accessor :first_port, 1096
  24 + defaults_accessor :last_port, 1115
  25 + defaults_accessor :myaddress, ''
  26 + defaults_accessor :auto_receive, false
85 27
86 28 ADDR_DETECT_JOIN = 0
87 29 ADDR_DETECT_NIC = 1
88 30 ADDR_DETECT_SPECIFY = 2
89   -
90   - def initialize
91   - @first_port = 1096
92   - @last_port = 1115
93   - @address_detection_method = ADDR_DETECT_JOIN
94   - @myaddress = ''
95   - @auto_receive = false
96   - end
  31 + defaults_accessor :address_detection_method, ADDR_DETECT_JOIN
97 32 end
98 33
99 34 class General < AbstractPreferencesSection
@@ -125,98 +60,52 @@ class General < AbstractPreferencesSection
125 60 defaults_accessor :max_log_lines, 300
126 61
127 62 defaults_accessor :paste_syntax, (LanguageSupport.primary_language == 'ja' ? 'notice' : 'privmsg')
128   -
129   - #include PersistenceHelper
130   - def set_persistent_attrs(*args)
131   - end
132 63 end
133 64
134   - class Sound
135   - include PersistenceHelper
136   - persistent_attr :login, :disconnect, :highlight, :newtalk, :kicked, :invited, :channeltext, :talktext
137   - persistent_attr :file_receive_request, :file_receive_success, :file_receive_failure, :file_send_success, :file_send_failure
138   -
139   - def initialize
140   - @login = @disconnect = @highlight = @newtalk = @kicked = @invited = @channeltext = @talktext = ''
141   - @file_receive_request = @file_receive_success = @file_receive_failure = ''
142   - @file_send_success = @file_send_failure = ''
143   - end
  65 + class Sound < AbstractPreferencesSection
  66 + [ :login, :disconnect, :highlight, :newtalk, :kicked, :invited, :channeltext, :talktext,
  67 + :file_receive_request, :file_receive_success, :file_receive_failure, :file_send_success, :file_send_failure
  68 + ].each { |attr| defaults_accessor attr, '' }
144 69 end
145 70
146   - class Theme
147   - include PersistenceHelper
148   - persistent_attr :name, :override_log_font, :log_font_name, :log_font_size, :override_nick_format, :nick_format, :override_timestamp_format, :timestamp_format
149   -
150   - def initialize
151   - @name = 'resource:Default'
152   - @override_log_font = false
153   - @log_font_name = 'Lucida Grande'
154   - @log_font_size = 12
155   - @override_nick_format = false
156   - @nick_format = '%n: '
157   - @override_timestamp_format = false
158   - @timestamp_format = '%H:%M'
159   - end
  71 + class Theme < AbstractPreferencesSection
  72 + defaults_accessor :name, 'resource:Default'
  73 + defaults_accessor :override_log_font, false
  74 + defaults_accessor :log_font_name, 'Lucida Grande'
  75 + defaults_accessor :log_font_size, 12
  76 + defaults_accessor :override_nick_format, false
  77 + defaults_accessor :nick_format, '%n: '
  78 + defaults_accessor :override_timestamp_format, false
  79 + defaults_accessor :timestamp_format, '%H:%M'
160 80 end
161 81
162   - model_attr :key, :dcc, :gen, :sound, :theme
163   -
164   - # TODO: For now alias these, but should replace them completely
165   - alias_method :general, :gen
  82 + attr_reader :dcc, :general, :keyword, :sound, :theme
166 83
167 84 def initialize
168   - @key = Keyword.new
169   - @dcc = Dcc.new
170   - @gen = General.new
171   - @sound = Sound.new
172   - @theme = Theme.new
173   -
174   - load
175   - end
176   -
177   - def load
178   - d = read_defaults('pref')
179   - if d
180   - self.class.models.each do |i|
181   - m = instance_variable_get("@#{i}")
182   - m.set_persistent_attrs(d[i])
183   - end
184   - else
185   - self.class.models.each do |i|
186   - m = instance_variable_get("@#{i}")
187   - d = read_defaults(i.to_s)
188   - m.set_persistent_attrs(d)
189   - end
190   - end
191   - end
192   -
193   - def save
194   - h = {}
195   - self.class.models.each do |i|
196   - m = instance_variable_get("@#{i}")
197   - h[i] = m.get_persistent_attrs
198   - end
199   - write_defaults('pref', h)
200   - sync
201   - end
202   -
203   - def load_world
204   - read_defaults('world')
  85 + @keyword = Keyword.new
  86 + @general = General.new
  87 + @sound = Sound.new
  88 + @theme = Theme.new
  89 + @dcc = Dcc.new
205 90 end
206 91
207   - def save_world(c)
208   - write_defaults('world', c)
209   - sync
210   - end
211   -
212   - def load_window(key)
213   - read_defaults(key)
214   - end
215   -
216   - def save_window(key, value)
217   - write_defaults(key, value)
218   - sync
219   - end
  92 + # def load_world
  93 + # read_defaults('world')
  94 + # end
  95 + #
  96 + # def save_world(c)
  97 + # write_defaults('world', c)
  98 + # sync
  99 + # end
  100 + #
  101 + # def load_window(key)
  102 + # read_defaults(key)
  103 + # end
  104 + #
  105 + # def save_window(key, value)
  106 + # write_defaults(key, value)
  107 + # sync
  108 + # end
220 109
221 110 private
222 111
@@ -227,3 +116,10 @@ def sync
227 116 # And register the defaults
228 117 register_default_values!
229 118 end
  119 +
  120 +module Kernel
  121 + # A shortcut method for easy access anywhere to the shared user defaults
  122 + def preferences
  123 + Preferences.instance
  124 + end
  125 +end
38 test/model/preferences_test.rb
... ... @@ -1,6 +1,23 @@
1 1 require File.expand_path('../../test_helper', __FILE__)
2 2 require 'model/preferences'
3 3
  4 +describe "Preferences" do
  5 + it "should be a singleton" do
  6 + Preferences.should.include Singleton
  7 + Preferences.instance.should.be.instance_of Preferences
  8 + end
  9 +
  10 + it "should have defined a shortcut method on Kernel" do
  11 + preferences.should.be Preferences.instance
  12 + end
  13 +
  14 + it "should have instances of all section classes" do
  15 + %w{ keyword dcc general sound theme }.each do |section|
  16 + preferences.send(section).class.name.should == "Preferences::#{section.capitalize}"
  17 + end
  18 + end
  19 +end
  20 +
4 21 class TestDefaults < Preferences::General
5 22 defaults_accessor :an_option, true
6 23 end
@@ -24,19 +41,18 @@ class TestDefaults < Preferences::General
24 41 end
25 42 end
26 43
27   -describe "Preferences::General" do
28   - before do
29   - @preferences = Preferences.new
30   - end
31   -
32   - it "should return the `general' preferences" do
33   - @preferences.general.should.be.instance_of Preferences::General
  44 +describe "Preferences sections" do
  45 + it "should include the `general' preferences" do
  46 + preferences.general.should.be.instance_of Preferences::General
34 47 end
35 48
36   - it "should have set the correct default values" do
37   - Preferences::General.section_default_values.should.not.be.empty
38   - Preferences::General.section_default_values.each do |attr, value|
39   - @preferences.general.send(attr).should == value
  49 + %w{ General Keyword Dcc Sound }.each do |section|
  50 + it "should have set the correct default values for the `#{section}' section" do
  51 + klass = Preferences.const_get(section)
  52 + klass.section_default_values.should.not.be.empty
  53 + klass.section_default_values.each do |attr, value|
  54 + preferences.send(section.downcase).send(attr).should == value
  55 + end
40 56 end
41 57 end
42 58 end

0 comments on commit ff06233

Please sign in to comment.
Something went wrong with that request. Please try again.