Browse files

Rename #set_preference to #write_preference

  • Loading branch information...
1 parent d2fce18 commit fa57073f5c902c24e318a91764461bb027ed38f7 @obrie obrie committed Mar 7, 2010
Showing with 43 additions and 42 deletions.
  1. +1 −0 CHANGELOG.rdoc
  2. +3 −3 README.rdoc
  3. +8 −8 lib/preferences.rb
  4. +31 −31 test/functional/preferences_test.rb
View
1 CHANGELOG.rdoc
@@ -1,5 +1,6 @@
== master
+* Rename #set_preference to #write_preference
* Add caching of preference lookups
* Fix preferences being stored even if they didn't change
* Release gems via rake-gemcutter instead of rubyforge
View
6 README.rdoc
@@ -112,8 +112,8 @@ Reader method:
user.preferred(:language) # => "English"
Write method:
- user.set_preference(:hot_salsa, false) # => false
- user.set_preference(:language, "English") # => "English"
+ user.write_preference(:hot_salsa, false) # => false
+ user.write_preference(:language, "English") # => "English"
=== Accessing all preferences
@@ -146,7 +146,7 @@ through an example:
car = Car.find(:first)
user.preferred_color = 'red', car
- # user.set_preference(:color, 'red', car) # The generic way
+ # user.write_preference(:color, 'red', car) # The generic way
This will create a color preference of "red" for the given car. In this way,
you can have "color" preferences for different records.
View
16 lib/preferences.rb
@@ -108,10 +108,10 @@ module MacroMethods
# ...generates the following methods:
# * <tt>prefers_notifications?</tt> - Whether a value has been specified, i.e. <tt>record.prefers?(:notifications)</tt>
# * <tt>prefers_notifications</tt> - The actual value stored, i.e. <tt>record.prefers(:notifications)</tt>
- # * <tt>prefers_notifications=(value)</tt> - Sets a new value, i.e. <tt>record.set_preference(:notifications, value)</tt>
+ # * <tt>prefers_notifications=(value)</tt> - Sets a new value, i.e. <tt>record.write_preference(:notifications, value)</tt>
# * <tt>preferred_notifications?</tt> - Whether a value has been specified, i.e. <tt>record.preferred?(:notifications)</tt>
# * <tt>preferred_notifications</tt> - The actual value stored, i.e. <tt>record.preferred(:notifications)</tt>
- # * <tt>preferred_notifications=(value)</tt> - Sets a new value, i.e. <tt>record.set_preference(:notifications, value)</tt>
+ # * <tt>preferred_notifications=(value)</tt> - Sets a new value, i.e. <tt>record.write_preference(:notifications, value)</tt>
#
# Notice that there are two tenses used depending on the context of the
# preference. Conventionally, <tt>prefers_notifications?</tt> is better
@@ -179,7 +179,7 @@ def preference(name, *args)
# Writer
define_method("preferred_#{name}=") do |*args|
- set_preference(*args.flatten.unshift(name))
+ write_preference(*args.flatten.unshift(name))
end
alias_method "prefers_#{name}=", "preferred_#{name}="
@@ -331,7 +331,7 @@ def preferences(*args)
# user.preferred?(:color, 'cars') # => true
# user.preferred?(:color, Car.first) # => true
#
- # user.set_preference(:color, nil)
+ # user.write_preference(:color, nil)
# user.preferred(:color) # => nil
# user.preferred?(:color) # => false
def preferred?(name, group = nil)
@@ -357,7 +357,7 @@ def preferred?(name, group = nil)
# user.preferred(:color, 'cars') # => "red"
# user.preferred(:color, Car.first) # => "red"
#
- # user.set_preference(:color, 'blue')
+ # user.write_preference(:color, 'blue')
# user.preferred(:color) # => "blue"
def preferred(name, group = nil)
name = name.to_s
@@ -393,12 +393,12 @@ def preferred(name, group = nil)
# == Examples
#
# user = User.find(:first)
- # user.set_preference(:color, 'red') # => "red"
+ # user.write_preference(:color, 'red') # => "red"
# user.save!
#
- # user.set_preference(:color, 'blue', Car.first) # => "blue"
+ # user.write_preference(:color, 'blue', Car.first) # => "blue"
# user.save!
- def set_preference(name, value, group = nil)
+ def write_preference(name, value, group = nil)
name = name.to_s
group = group.is_a?(Symbol) ? group.to_s : group
View
62 test/functional/preferences_test.rb
@@ -424,29 +424,29 @@ def setup
end
def test_should_have_same_value_if_not_changed
- @user.set_preference(:notifications, true)
+ @user.write_preference(:notifications, true)
assert_equal true, @user.preferred(:notifications)
end
def test_should_use_new_value_if_changed
- @user.set_preference(:notifications, false)
+ @user.write_preference(:notifications, false)
assert_equal false, @user.preferred(:notifications)
end
def test_should_not_save_record_after_changing_preference
@user.login = 'test'
- @user.set_preference(:notifications, false)
+ @user.write_preference(:notifications, false)
assert_equal 'admin', User.find(@user.id).login
end
def test_should_not_create_stored_preferences_immediately
- @user.set_preference(:notifications, false)
+ @user.write_preference(:notifications, false)
assert @user.stored_preferences.empty?
end
def test_should_not_create_stored_preference_if_value_not_changed
- @user.set_preference(:notifications, true)
+ @user.write_preference(:notifications, true)
@user.save!
assert_equal 0, @user.stored_preferences.count
@@ -455,28 +455,28 @@ def test_should_not_create_stored_preference_if_value_not_changed
def test_should_not_create_stored_integer_preference_if_typecast_not_changed
User.preference :age, :integer
- @user.set_preference(:age, '')
+ @user.write_preference(:age, '')
@user.save!
assert_equal 0, @user.stored_preferences.count
end
def test_should_create_stored_preference_if_value_changed
- @user.set_preference(:notifications, false)
+ @user.write_preference(:notifications, false)
@user.save!
assert_equal 1, @user.stored_preferences.count
end
def test_should_reset_unsaved_preferences_after_reload
- @user.set_preference(:notifications, false)
+ @user.write_preference(:notifications, false)
@user.reload
assert_equal true, @user.preferred(:notifications)
end
def test_should_not_save_reset_preferences_after_reload
- @user.set_preference(:notifications, false)
+ @user.write_preference(:notifications, false)
@user.reload
@user.save!
@@ -486,7 +486,7 @@ def test_should_not_save_reset_preferences_after_reload
def test_should_overwrite_existing_stored_preference_if_value_changed
preference = create_preference(:owner => @user, :name => 'notifications', :value => true)
- @user.set_preference(:notifications, false)
+ @user.write_preference(:notifications, false)
@user.save!
preference.reload
@@ -496,7 +496,7 @@ def test_should_overwrite_existing_stored_preference_if_value_changed
def test_should_not_remove_preference_if_set_to_default
create_preference(:owner => @user, :name => 'notifications', :value => false)
- @user.set_preference(:notifications, true)
+ @user.write_preference(:notifications, true)
@user.save!
@user.reload
@@ -508,7 +508,7 @@ def test_should_not_remove_preference_if_set_to_default
def test_should_not_remove_preference_if_set_to_nil
create_preference(:owner => @user, :name => 'notifications', :value => false)
- @user.set_preference(:notifications, nil)
+ @user.write_preference(:notifications, nil)
@user.save!
@user.reload
@@ -526,31 +526,31 @@ def setup
end
def test_should_have_same_value_if_not_changed
- @user.set_preference(:notifications, true, :chat)
+ @user.write_preference(:notifications, true, :chat)
assert_equal true, @user.preferred(:notifications, :chat)
end
def test_should_use_new_value_if_changed
- @user.set_preference(:notifications, false, :chat)
+ @user.write_preference(:notifications, false, :chat)
assert_equal false, @user.preferred(:notifications, :chat)
end
def test_should_not_create_stored_preference_if_value_not_changed
- @user.set_preference(:notifications, true, :chat)
+ @user.write_preference(:notifications, true, :chat)
@user.save!
assert_equal 0, @user.stored_preferences.count
end
def test_should_create_stored_preference_if_value_changed
- @user.set_preference(:notifications, false, :chat)
+ @user.write_preference(:notifications, false, :chat)
@user.save!
assert_equal 1, @user.stored_preferences.count
end
def test_should_set_group_attributes_on_stored_preferences
- @user.set_preference(:notifications, false, :chat)
+ @user.write_preference(:notifications, false, :chat)
@user.save!
preference = @user.stored_preferences.first
@@ -561,7 +561,7 @@ def test_should_set_group_attributes_on_stored_preferences
def test_should_overwrite_existing_stored_preference_if_value_changed
preference = create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => true)
- @user.set_preference(:notifications, false, :chat)
+ @user.write_preference(:notifications, false, :chat)
@user.save!
preference.reload
@@ -579,31 +579,31 @@ def setup
end
def test_should_have_same_value_if_not_changed
- @user.set_preference(:notifications, true, @car)
+ @user.write_preference(:notifications, true, @car)
assert_equal true, @user.preferred(:notifications, @car)
end
def test_should_use_new_value_if_changed
- @user.set_preference(:notifications, false, @car)
+ @user.write_preference(:notifications, false, @car)
assert_equal false, @user.preferred(:notifications, @car)
end
def test_should_not_create_stored_preference_if_value_not_changed
- @user.set_preference(:notifications, true, @car)
+ @user.write_preference(:notifications, true, @car)
@user.save!
assert_equal 0, @user.stored_preferences.count
end
def test_should_create_stored_preference_if_value_changed
- @user.set_preference(:notifications, false, @car)
+ @user.write_preference(:notifications, false, @car)
@user.save!
assert_equal 1, @user.stored_preferences.count
end
def test_should_set_group_attributes_on_stored_preferences
- @user.set_preference(:notifications, false, @car)
+ @user.write_preference(:notifications, false, @car)
@user.save!
preference = @user.stored_preferences.first
@@ -625,7 +625,7 @@ def test_should_only_have_defaults_if_nothing_customized
end
def test_should_merge_defaults_with_unsaved_changes
- @user.set_preference(:notifications, false)
+ @user.write_preference(:notifications, false)
assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences
end
@@ -636,7 +636,7 @@ def test_should_merge_defaults_with_saved_changes
def test_should_merge_stored_preferences_with_unsaved_changes
create_preference(:owner => @user, :name => 'notifications', :value => false)
- @user.set_preference(:language, 'Latin')
+ @user.write_preference(:language, 'Latin')
assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences
end
@@ -676,7 +676,7 @@ def test_should_only_have_defaults_if_nothing_customized
end
def test_should_merge_defaults_with_unsaved_changes
- @user.set_preference(:notifications, false, :chat)
+ @user.write_preference(:notifications, false, :chat)
assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences(:chat)
end
@@ -687,7 +687,7 @@ def test_should_merge_defaults_with_saved_changes
def test_should_merge_stored_preferences_with_unsaved_changes
create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => false)
- @user.set_preference(:language, 'Latin', :chat)
+ @user.write_preference(:language, 'Latin', :chat)
assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences(:chat)
end
@@ -732,7 +732,7 @@ def test_should_only_have_defaults_if_nothing_customized
end
def test_should_merge_defaults_with_unsaved_changes
- @user.set_preference(:notifications, false, @car)
+ @user.write_preference(:notifications, false, @car)
assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences(@car)
end
@@ -743,7 +743,7 @@ def test_should_merge_defaults_with_saved_changes
def test_should_merge_stored_preferences_with_unsaved_changes
create_preference(:owner => @user, :group_type => 'Car', :group_id => @car.id, :name => 'notifications', :value => false)
- @user.set_preference(:language, 'Latin', @car)
+ @user.write_preference(:language, 'Latin', @car)
assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences(@car)
end
end
@@ -763,7 +763,7 @@ def test_should_only_have_defaults_if_nothing_customized
end
def test_should_merge_defaults_with_unsaved_changes
- @user.set_preference(:notifications, false)
+ @user.write_preference(:notifications, false)
assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences(nil)
end
@@ -774,7 +774,7 @@ def test_should_merge_defaults_with_saved_changes
def test_should_merge_stored_preferences_with_unsaved_changes
create_preference(:owner => @user, :name => 'notifications', :value => false)
- @user.set_preference(:language, 'Latin')
+ @user.write_preference(:language, 'Latin')
assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences(nil)
end
end

0 comments on commit fa57073

Please sign in to comment.