nullify 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -6,27 +6,20 @@ module LaserLemon
6
6
 
7
7
  module ClassMethods
8
8
  def nullify(*columns)
9
- write_inheritable_array :nullified_columns, columns.map(&:to_sym)
10
- class_inheritable_reader :nullified_columns
11
-
12
- columns.each do |column|
13
- define_method "#{column}=" do |value|
14
- write_attribute(column, value.blank? ? nil : value)
15
- end
16
- end
9
+ class_inheritable_array :nullified_columns
10
+ self.nullified_columns = columns.map(&:to_s)
17
11
 
18
12
  include InstanceMethods
19
-
20
- before_validation :nullify_columns
21
- before_save :nullify_columns
13
+ alias_method_chain :write_attribute, :nullify
22
14
  end
23
15
  end
24
16
 
25
17
  module InstanceMethods
26
- def nullify_columns
27
- self.class.nullified_columns.each do |column|
28
- value = read_attribute(column)
29
- write_attribute(column, nil) if !value.nil? && value.blank?
18
+ def write_attribute_with_nullify(attr_name, value)
19
+ if nullified_columns.include?(attr_name.to_s)
20
+ write_attribute_without_nullify(attr_name, value.blank? ? nil : value)
21
+ else
22
+ write_attribute_without_nullify(attr_name, value)
30
23
  end
31
24
  end
32
25
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{nullify}
8
- s.version = "0.2.1"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["laserlemon"]
12
- s.date = %q{2009-10-07}
12
+ s.date = %q{2009-10-08}
13
13
  s.description = %q{Nuke blank values in ActiveRecord models}
14
14
  s.email = %q{steve@laserlemon.com}
15
15
  s.extra_rdoc_files = [
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
27
27
  "nullify.gemspec",
28
28
  "tasks/nullify_tasks.rake",
29
29
  "test/nullify_test.rb",
30
+ "test/schema.rb",
30
31
  "test/test_helper.rb"
31
32
  ]
32
33
  s.homepage = %q{http://github.com/laserlemon/nullify}
@@ -37,6 +38,7 @@ Gem::Specification.new do |s|
37
38
  s.summary = %q{Nuke blank values in ActiveRecord models}
38
39
  s.test_files = [
39
40
  "test/nullify_test.rb",
41
+ "test/schema.rb",
40
42
  "test/test_helper.rb"
41
43
  ]
42
44
 
@@ -1,21 +1,8 @@
1
- def get_class
2
- klass = ENV['CLASS'] || ENV['class']
3
- raise 'Must specify CLASS' unless klass
4
- @class = Object.const_get(klass)
5
- end
6
-
7
- def get_columns
8
- raise "Class #{@class.name} has no nullified columns" unless @class.respond_to?(:nullified_columns)
9
- @columns = if column = ENV['COLUMN'] || ENV['column']
10
- raise "Class #{@class.name} has no nullified column #{column}" unless @class.nullified_columns.include?(column.to_sym)
11
- [column.to_sym]
12
- else @class.nullified_columns
13
- end
14
- end
15
-
16
1
  desc 'Sets blank columns to nil for a given CLASS (and optional COLUMN)'
17
2
  task :nullify => :environment do
18
- get_class and get_columns
3
+ get_class
4
+ get_columns
5
+
19
6
  @class.all.each do |instance|
20
7
  @columns.each do |column|
21
8
  value = instance[column]
@@ -24,3 +11,21 @@ task :nullify => :environment do
24
11
  instance.save
25
12
  end
26
13
  end
14
+
15
+ def get_class
16
+ raise 'Must specify CLASS' unless ENV['CLASS']
17
+ begin
18
+ @class = ENV['CLASS'].classify.constantize
19
+ rescue NameError
20
+ raise "Class #{ENV['CLASS'].classify} does not exist"
21
+ end
22
+ end
23
+
24
+ def get_columns
25
+ raise "Class #{@class.name} has no nullified columns" unless @class.respond_to?(:nullified_columns) && !@class.nullified_columns.empty?
26
+ @columns = if ENV['COLUMN']
27
+ raise "Class #{@class.name} has no nullified column #{ENV['COLUMN']}" unless @class.nullified_columns.include?(column.to_s)
28
+ [column.to_s]
29
+ else @class.nullified_columns
30
+ end
31
+ end
@@ -1,86 +1,70 @@
1
- require 'test/unit'
2
- require 'rubygems'
3
- require 'active_record'
1
+ require 'test_helper'
4
2
 
5
- require File.join(File.dirname(__FILE__), '..', 'init')
6
-
7
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :dbfile => ':memory:')
8
-
9
- def setup_db
10
- ActiveRecord::Schema.define(:version => 1) do
11
- create_table :addresses do |t|
12
- t.string :street_1, :null => false
13
- t.string :street_2, :null => true
14
- t.string :city, :null => false
15
- t.string :state, :null => false
16
- t.string :zip, :null => false
17
- t.string :zip_4, :null => true
3
+ class ChangesTest < Test::Unit::TestCase
4
+ context 'A nullified model' do
5
+ setup do
6
+ @irs = Address.create(
7
+ :street_1 => 'Department of the Teasury',
8
+ :street_2 => 'Internal Revenue Service',
9
+ :city => 'Kansas City',
10
+ :state => 'Missouri',
11
+ :zip => '64999',
12
+ :zip_4 => '0102'
13
+ )
14
+ @apple = Address.create(
15
+ :street_1 => '1 Infinite Loop',
16
+ :city => 'Cupertino',
17
+ :state => 'California',
18
+ :zip => '95014'
19
+ )
18
20
  end
19
- end
20
- end
21
21
 
22
- def teardown_db
23
- ActiveRecord::Base.connection.tables.each do |table|
24
- ActiveRecord::Base.connection.drop_table(table)
25
- end
26
- end
27
-
28
- class Address < ActiveRecord::Base
29
- nullify :street_2, :zip_4
30
-
31
- validates_presence_of :street_1, :city, :zip
32
- validates_format_of :zip, :with => /^\d{5}$/, :allow_nil => false
33
- validates_format_of :zip_4, :with => /^\d{4}/, :allow_nil => true
34
- end
35
-
36
- class AddressTest < Test::Unit::TestCase
37
- def setup
38
- setup_db
39
- Address.create!(
40
- :street_1 => 'Department of the Teasury',
41
- :street_2 => 'Internal Revenue Service',
42
- :city => 'Kansas City',
43
- :state => 'Missouri',
44
- :zip => '64999',
45
- :zip_4 => '0102'
46
- )
47
- Address.create!(
48
- :street_1 => '1 Infinite Loop',
49
- :city => 'Cupertino',
50
- :state => 'California',
51
- :zip => '95014'
52
- )
53
- end
54
-
55
- def irs
56
- Address.first
57
- end
22
+ should 'return nullified_columns as an array of strings' do
23
+ [Address, Address.new].each do |model|
24
+ assert_respond_to model, :nullified_columns
25
+ assert_kind_of Array, model.nullified_columns
26
+ assert model.nullified_columns.all?{|c| c.is_a?(String) }
27
+ end
28
+ end
58
29
 
59
- def apple
60
- Address.last
61
- end
30
+ should 'should nuke blank values in nullified columns' do
31
+ assert_not_nil @irs.street_2
32
+ [nil, '', ' ', "\n", false].each do |value|
33
+ @irs.street_2 = value
34
+ assert_nil @irs.street_2
35
+ assert @irs.save
36
+ assert_nil @irs.street_2
37
+ end
38
+ end
62
39
 
63
- def teardown
64
- teardown_db
65
- end
40
+ should 'not nuke blank values in non-nullified columns' do
41
+ assert_not_nil @irs.state
42
+ [nil, '', ' ', "\n", false].each do |value|
43
+ @irs.state = value
44
+ assert_equal value, @irs.state
45
+ assert @irs.save
46
+ assert_equal value, @irs.state
47
+ end
48
+ end
66
49
 
67
- def test_no_nullification_needed
68
- before = irs.attributes
69
- irs.save
70
- after = irs.attributes
71
- assert_equal(before, after)
72
- before = apple.attributes
73
- apple.save
74
- after = apple.attributes
75
- assert_equal(before, after)
76
- end
50
+ should 'not nuke non-blank values in nullified columns' do
51
+ assert_not_nil @irs.zip_4
52
+ %w(1234 2345 3456 4567).each do |value|
53
+ @irs.zip_4 = value
54
+ assert_equal value, @irs.zip_4
55
+ assert @irs.save
56
+ assert_equal value, @irs.zip_4
57
+ end
58
+ end
77
59
 
78
- def test_nullification_needed
79
- assert(irs.update_attributes(:zip_4 => ''))
80
- assert_nil(irs.zip_4)
81
- assert(irs.update_attributes(:zip_4 => ' '))
82
- assert_nil(irs.zip_4)
83
- assert(irs.update_attributes(:zip_4 => '1939'))
84
- assert_equal('1939', irs.zip_4)
60
+ should 'not nuke non-blank values in non-nullified columns' do
61
+ assert_not_nil @irs.street_1
62
+ %w(T E S T).each do |value|
63
+ @irs.street_1 = value
64
+ assert_equal value, @irs.street_1
65
+ assert @irs.save
66
+ assert_equal value, @irs.street_1
67
+ end
68
+ end
85
69
  end
86
70
  end
@@ -0,0 +1,29 @@
1
+ ActiveRecord::Base.establish_connection(
2
+ :adapter => defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby' ? 'jdbcsqlite3' : 'sqlite3',
3
+ :database => File.join(File.dirname(__FILE__), 'test.db')
4
+ )
5
+
6
+ class CreateSchema < ActiveRecord::Migration
7
+ def self.up
8
+ create_table :addresses, :force => true do |t|
9
+ t.string :street_1
10
+ t.string :street_2
11
+ t.string :city
12
+ t.string :state
13
+ t.string :zip
14
+ t.string :zip_4
15
+ end
16
+ end
17
+ end
18
+
19
+ CreateSchema.suppress_messages do
20
+ CreateSchema.migrate(:up)
21
+ end
22
+
23
+ class Address < ActiveRecord::Base
24
+ nullify :street_2, :zip_4
25
+
26
+ validates_presence_of :street_1, :city, :zip
27
+ validates_format_of :zip, :with => /^\d{5}$/, :allow_nil => false
28
+ validates_format_of :zip_4, :with => /^\d{4}/, :allow_nil => true
29
+ end
@@ -1,3 +1,10 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $: << File.dirname(__FILE__)
3
+
1
4
  require 'rubygems'
2
- require 'active_support'
3
- require 'active_support/test_case'
5
+ require 'test/unit'
6
+ require 'activerecord'
7
+ require 'shoulda'
8
+ require 'nullify'
9
+ require 'schema'
10
+ begin; require 'redgreen'; rescue LoadError; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nullify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - laserlemon
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-07 00:00:00 -04:00
12
+ date: 2009-10-08 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -33,6 +33,7 @@ files:
33
33
  - nullify.gemspec
34
34
  - tasks/nullify_tasks.rake
35
35
  - test/nullify_test.rb
36
+ - test/schema.rb
36
37
  - test/test_helper.rb
37
38
  has_rdoc: true
38
39
  homepage: http://github.com/laserlemon/nullify
@@ -64,4 +65,5 @@ specification_version: 3
64
65
  summary: Nuke blank values in ActiveRecord models
65
66
  test_files:
66
67
  - test/nullify_test.rb
68
+ - test/schema.rb
67
69
  - test/test_helper.rb