Skip to content
Browse files

Initial commit

  • Loading branch information...
1 parent d822bae commit d6b2daa70494f3a67b9967c4a7f30dad57de09bb @binarylogic committed Aug 12, 2009
Showing with 43 additions and 21 deletions.
  1. +3 −15 README.rdoc
  2. +4 −3 Rakefile
  3. +13 −0 lib/coercionlogic.rb
  4. +6 −2 spec/coercionlogic_spec.rb
  5. +17 −1 spec/spec_helper.rb
View
18 README.rdoc
@@ -1,18 +1,6 @@
-= coercionlogic
+= Coercionlogic
-Description goes here.
+Extremely simple module to coerce blank strings into nil objects for ActiveRecord.
-== Note on Patches/Pull Requests
-
-* Fork the project.
-* Make your feature addition or bug fix.
-* Add tests for it. This is important so I don't break it in a
- future version unintentionally.
-* Commit, do not mess with rakefile, version, or history.
- (if you want to have your own version, that is fine but
- bump version in a commit by itself I can ignore when I pull)
-* Send me a pull request. Bonus points for topic branches.
-== Copyright
-
-Copyright (c) 2009 binarylogic. See LICENSE for details.
+Copyright (c) 2008 {Ben Johnson of Binary Logic}[http://www.binarylogic.com], released under the MIT license
View
7 Rakefile
@@ -5,14 +5,15 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "coercionlogic"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.summary = "Coerce blank strings into nil objects for ActiveRecord"
+ gem.description = "Coerce blank strings into nil objects for ActiveRecord"
gem.email = "bjohnson@binarylogic.com"
gem.homepage = "http://github.com/binarylogic/coercionlogic"
gem.authors = ["binarylogic"]
+ gem.add_dependency "activerecord"
gem.add_development_dependency "rspec"
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
+ Jeweler::RubyforgeTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end
View
13 lib/coercionlogic.rb
@@ -0,0 +1,13 @@
+require 'activerecord'
+
+module Coercionlogic
+ def write_attribute_with_coercion(attr_name, value)
+ value = nil if value.is_a?(String) && value.blank?
+ write_attribute_without_coercion(attr_name, value)
+ end
+end
+
+ActiveRecord::Base.class_eval do
+ include Coercionlogic
+ alias_method_chain :write_attribute, :coercion
+end
View
8 spec/coercionlogic_spec.rb
@@ -1,7 +1,11 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Coercionlogic" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
+ it "should convert blank strings to nils" do
+ user = User.new
+ user.name = ""
+ user.save
+ user.reload
+ user.name.should be_nil
end
end
View
18 spec/spec_helper.rb
@@ -1,8 +1,24 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
-require 'coercionlogic'
require 'spec'
require 'spec/autorun'
+require 'rubygems'
+require 'coercionlogic'
+
+ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
+ActiveRecord::Base.configurations = true
+
+ActiveRecord::Schema.verbose = false
+ActiveRecord::Schema.define(:version => 1) do
+ create_table :users do |t|
+ t.datetime :created_at
+ t.datetime :updated_at
+ t.string :name
+ end
+end
+
+class User < ActiveRecord::Base
+end
Spec::Runner.configure do |config|

0 comments on commit d6b2daa

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