Permalink
Browse files

HasDetails

  • Loading branch information...
0 parents commit 8da1dd01f84e1e3ae3b0546ec9d51f96d5a0aeaa @marten committed Jun 23, 2008
Showing with 117 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +19 −0 README
  3. +22 −0 Rakefile
  4. +1 −0 init.rb
  5. +1 −0 install.rb
  6. +41 −0 lib/has_details.rb
  7. +4 −0 tasks/has_details_tasks.rake
  8. +8 −0 test/has_details_test.rb
  9. +1 −0 uninstall.rb
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 README
@@ -0,0 +1,19 @@
+HasDetails
+==========
+
+HasDetails
+
+Example
+=======
+
+class User << ActiveRecord::Base
+ has_details :firstname => String,
+ :lastname => String,
+ :birthday => Date,
+ :gender => Symbol
+
+end
+
+
+=======
+Copyright (c) 2008 Marten Veldthuis, released under the MIT license
22 Rakefile
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the has_details plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the has_details plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'HasDetails'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
1 init.rb
@@ -0,0 +1 @@
+ActiveRecord::Base.send :include, HasDetails
1 install.rb
@@ -0,0 +1 @@
+# Install hook code here
41 lib/has_details.rb
@@ -0,0 +1,41 @@
+module HasDetails
+ def self.included(base)
+ base.extend(ClassMethods)
+ base.send(:include, InstanceMethods)
+ end
+
+ module ClassMethods
+ def has_details(options = {})
+ configuration = { }
+ 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)
+
+ serialize :details, Hash
+
+ configuration.each do |f,t|
+
+ 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})"
+
+ puts <<-EOV
+ def #{f}
+ @details[:#{f}]
+ end
+
+ def #{f}=(val)
+ #{exception_code}
+ @details[:#{f}] = val
+ end
+ EOV
+ end
+
+ end
+ end
+
+ module InstanceMethods
+
+ end
+
+end
4 tasks/has_details_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :has_details do
+# # Task goes here
+# end
8 test/has_details_test.rb
@@ -0,0 +1,8 @@
+require 'test/unit'
+
+class HasDetailsTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ def test_this_plugin
+ flunk
+ end
+end
1 uninstall.rb
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit 8da1dd0

Please sign in to comment.