Permalink
Browse files

Use column name option

  • Loading branch information...
esposito committed Nov 24, 2008
1 parent 4922107 commit 33d423a5867793fea90a68766133f5edb233deae
Showing with 30 additions and 29 deletions.
  1. +30 −29 lib/has_details.rb
View
@@ -2,63 +2,64 @@ module HasDetails
def self.included(base)
base.extend(ClassMethods)
end
-
- # HasDetails allows you to store a large amount of (optional) attributes for any model's instance in a
- # serialized column. It takes care of adding convenience methods to your model, and verifies that the
+
+ # HasDetails allows you to store a large amount of (optional) attributes for any model's instance in a
+ # serialized column. It takes care of adding convenience methods to your model, and verifies that the
# value being assigned is indeed (one of) the type(s) required for that attribute.
- #
+ #
# Example:
- #
+ #
# class User << ActiveRecord::Base
# has_details :column => :extended_attributes,
# :firstname => String,
# :lastname => String,
# :birthday => Date,
# :gender => [:male, :female]
# end
- #
+ #
# john = User.find(1)
# john.birthday = 5.years.ago
# john.gender
# => :male
module ClassMethods
-
+
# Configuration options are:
#
# * +column+ - Specifies the column name to use for storing the serialized attributes. This column will automatically be set to serialize in Rails. The default value is :details.
- #
+ #
# The rest of the configuration options is the set of attributes that will be saved in the +column+. Valid formats are:
# * +:field => ClassName+ (values assigned to +field+ must be of class +ClassName+)
# * +:field => [:symbol, :othersymbol]+ (values assigned to +field+ must be included in the array given)
def has_details(options = {})
configuration = { :column => :details}
configuration.update(options) if options.is_a?(Hash)
-
+ column = configuration[:column]
+
raise(ArgumentError, "You must be supply at least one field in the configuration hash") unless configuration.keys.size > 0
-# raise(Exception, "A #{configuration[:column]} column must be present in the database for this plugin.") unless columns.include?(:details)
-
- serialize configuration[:column], Hash
-
+ raise(Exception, "A #{column.inspect} column must be present in the database for this plugin.") unless column_names.include?(column.to_s)
+
+ serialize column, Hash
+
configuration.each do |f,t|
+ unless f == :column
+ exception_code = t.is_a?(Array) ? "raise \"Assigned value must be one of #{t.inspect}\" unless #{t.inspect}.include?(val)" : \
+ "raise \"Assigned value must be a #{t.inspect}\" unless val.is_a?(#{t.inspect})"
+
+ class_eval <<-EOV
+ def #{f}
+ self.#{column.to_s} ||= {}
+ self.#{column.to_s}[:#{f}]
+ end
- exception_code = t.is_a?(Array) ? "raise \"Assigned value must be one of #{t.inspect}\" unless #{t.inspect}.include?(val)" : \
- "raise \"Assigned value must be a #{t.inspect}\" unless val.is_a?(#{t.inspect})"
-
- class_eval <<-EOV
- def #{f}
- self.details ||= {}
- self.details[:#{f}]
- end
-
- def #{f}=(val)
- #{exception_code}
+ def #{f}=(val)
+ #{exception_code}
- self.details ||= {}
- self.details[:#{f}] = val
- end
- EOV
+ self.#{column.to_s} ||= {}
+ self.#{column.to_s}[:#{f}] = val
+ end
+ EOV
+ end
end
-
end
end

0 comments on commit 33d423a

Please sign in to comment.