Skip to content
Browse files

:as option for attribute alias

  • Loading branch information...
1 parent 943bc80 commit e30b51f79e96838ccbd1876eb40a0b53ef8982bd @gzigzigzeo committed Feb 28, 2012
Showing with 40 additions and 7 deletions.
  1. +24 −0 README.rdoc
  2. +9 −7 lib/magic_numbers/base.rb
  3. +6 −0 test/magic_numbers_test.rb
  4. +1 −0 test/test_helper.rb
View
24 README.rdoc
@@ -71,6 +71,30 @@ Magic-numbered columns will handle all incorrect (unspecified) values as +nil+:
@user.roles
# => [:user]
+== Using with Sphinx
+
+In Sphinx indexed model you should specify alias for magic_numbers accessor. Using magic_numbers without an alias
+causes search errors because Sphinx implies that magic_numbers attribute value type is integer, but it is an array in
+fact.
+
+ class IndexedUser < ActiveRecord::Base
+ bitfield_attribute :roles, :values => [:user, :moderator, :administrator], :as => :magic_roles
+
+ define_index do
+ # ...
+ end
+ end
+
+ @user = IndexedUser.new
+ @user.magic_roles = [:user, :administrator]
+ @user.save!
+
+ @user.roles
+ # => 3
+ @user.magic_roles
+ # [:user, :administrator]
+
+
== Copyrights
Copyright (c) 2009 Mikhail Lapshin (sotakone at sotakone dot com), released under the MIT license.
View
16 lib/magic_numbers/base.rb
@@ -47,23 +47,25 @@ def magic_number_attribute_options(name)
end
end
- protected
+ protected
- def magic_number_accessors(name)
+ def magic_number_accessors(name, accessor)
class_eval <<-EOE
- def #{name}; magic_number_read(:#{name}); end
- def #{name}=(new_value); magic_number_write(:#{name}, new_value); end
+ def #{accessor}; magic_number_read(:#{name}); end
+ def #{accessor}=(new_value); magic_number_write(:#{name}, new_value); end
EOE
end
def magic_number_attribute(name, options = {})
magic_number_attributes = self.magic_number_attributes || {}
- options.assert_valid_keys(:values, :type)
+ options.assert_valid_keys(:values, :type, :as)
- options[:stringified_values] = options[:values].map { |v| v.to_s }
+ options[:stringified_values] = options[:values].map(&:to_s)
+ options[:as] = options[:as] || name
+
magic_number_attributes[name] = options
self.magic_number_attributes = magic_number_attributes
- magic_number_accessors(name)
+ magic_number_accessors(name, options[:as])
end
end
View
6 test/magic_numbers_test.rb
@@ -6,6 +6,7 @@ class Foo < ActiveRecord::Base
enum_attribute :state, :values => [:active, :pending, :passive, :deleted]
bitfield_attribute :roles, :values => [:user, :administrator, :moderator]
+ bitfield_attribute :options, :values => [:wide, :tall], :as => :options_mask
end
@@ -58,4 +59,9 @@ def setup
assert_nil Foo.magic_number_for(:state, 'invalid state value')
end
+ test "should not hide base method if accessor assigned" do
+ @foo.options_mask = [:wide, :tall]
+ assert_equal [:wide, :tall], @foo.options_mask
+ assert_equal @foo.options, 3
+ end
end
View
1 test/test_helper.rb
@@ -17,6 +17,7 @@ def setup_db
create_table :foos do |t|
t.column :state, :integer
t.column :roles, :integer
+ t.column :options, :integer
end
end

0 comments on commit e30b51f

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