Permalink
Browse files

documentation updates, bugfixes

  • Loading branch information...
1 parent b8a7882 commit 651e166874f2ea0dc2f43cf7cedff06ed70b3df2 @marten committed Jun 23, 2008
Showing with 48 additions and 26 deletions.
  1. +13 −16 README
  2. +35 −10 lib/has_details.rb
View
@@ -1,22 +1,19 @@
-HasDetails
-==========
+= HasDetails
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
-=======
+= Example
-class User << ActiveRecord::Base
- has_details :firstname => String,
- :lastname => String,
- :birthday => Date,
- :gender => [:male, :female]
-end
+ class User << ActiveRecord::Base
+ has_details :firstname => String,
+ :lastname => String,
+ :birthday => Date,
+ :gender => [:male, :female]
+ end
+
+ john = User.find(1)
+ john.birthday = 5.years.ago
+ john.gender
+ => :male
-john = User.find(1)
-john.birthday = 5.years.ago
-john.gender
-=> :male
-
-=======
Copyright (c) 2008 Marten Veldthuis, released under the MIT license
View
@@ -4,15 +4,41 @@ def self.included(base)
base.send(:include, InstanceMethods)
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
+ # 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 = { }
+ configuration = { :column => :details}
configuration.update(options) if options.is_a?(Hash)
raise(ArgumentError, "You must be supply at least one field in the configuration hash") unless configuration.keys.size > 0
- raise(Exception, "A details column must be present in the database for this plugin.") unless columns.include?(:details)
+ raise(Exception, "A #{configuration[:column]} column must be present in the database for this plugin.") unless columns.include?(:details)
- serialize :details, Hash
+ serialize configuration[:column], Hash
configuration.each do |f,t|
@@ -21,21 +47,20 @@ def has_details(options = {})
puts <<-EOV
def #{f}
- @details[:#{f}]
+ details ||= {}
+ details[:#{f}]
end
def #{f}=(val)
#{exception_code}
- @details[:#{f}] = val
+
+ details ||= {}
+ details[:#{f}] = val
end
EOV
end
end
end
-
- module InstanceMethods
-
- end
-
+
end

0 comments on commit 651e166

Please sign in to comment.