sequel-string_nilifier 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +1 -0
- data/lib/sequel/plugins/string_nilifier.rb +60 -0
- data/lib/sequel/plugins/string_nilifier/version.rb +7 -0
- data/lib/sequel/string_nilifier.rb +2 -0
- data/sequel-string_nilifier.gemspec +25 -0
- data/spec/sequel/plugins/string_nilifier_spec.rb +70 -0
- data/spec/spec_helper.rb +24 -0
- metadata +115 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jonathan Tron
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Sequel::StringNilifier
|
2
|
+
|
3
|
+
Sequel plugin to convert empty string to nil
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'sequel-string_nilifier'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install sequel-string_nilifier
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
# Make all model subclass instances nilify strings (called before loading subclasses)
|
23
|
+
Sequel::Model.plugin :string_nilifier
|
24
|
+
|
25
|
+
# Make the Album class nilify strings
|
26
|
+
Album.plugin :string_nilifier
|
27
|
+
```
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Plugins
|
3
|
+
module StringNilifier
|
4
|
+
# Set blob columns as skipping nilifying when plugin is loaded.
|
5
|
+
def self.configure(model)
|
6
|
+
model.instance_variable_set(:@skipped_string_nilifier_columns, [])
|
7
|
+
model.send(:set_skipped_string_nilifying_columns)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
# Copy skipped nilifying columns from superclass into subclass.
|
12
|
+
def inherited(subclass)
|
13
|
+
subclass.instance_variable_set(:@skipped_string_nilifier_columns, @skipped_string_nilifier_columns.dup)
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
# Set blob columns as skipping stripping when plugin is loaded.
|
18
|
+
def set_dataset(*)
|
19
|
+
super
|
20
|
+
set_skipped_string_nilifying_columns
|
21
|
+
end
|
22
|
+
|
23
|
+
# Skip nilifying for the given columns.
|
24
|
+
def skip_string_nilifying(*columns)
|
25
|
+
@skipped_string_nilifier_columns.concat(columns).uniq!
|
26
|
+
end
|
27
|
+
|
28
|
+
# Return true if the column should not have values stripped.
|
29
|
+
def skip_string_nilifying?(column)
|
30
|
+
@skipped_string_nilifier_columns.include?(column)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
# Automatically skip stripping of blob columns
|
36
|
+
def set_skipped_string_nilifying_columns
|
37
|
+
if @db_schema
|
38
|
+
blob_columns = @db_schema.map{|k,v| k if v[:type] == :blob}.compact
|
39
|
+
skip_string_nilifying(*blob_columns)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module InstanceMethods
|
45
|
+
# Strip value if it is a non-blob string and the model hasn't been set
|
46
|
+
# to skip stripping for the column, before attempting to assign
|
47
|
+
# it to the model's values.
|
48
|
+
def nil_string?(k,str)
|
49
|
+
str.is_a?(String) && !str.is_a?(SQL::Blob) && str.strip.empty? && !model.skip_string_nilifying?(k)
|
50
|
+
end
|
51
|
+
|
52
|
+
def []=(k, v)
|
53
|
+
v = nil if nil_string? k, v
|
54
|
+
super(k, v)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sequel/plugins/string_nilifier/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sequel-string_nilifier"
|
8
|
+
spec.version = Sequel::Plugins::StringNilifier::VERSION
|
9
|
+
spec.authors = ["Jonathan Tron", "Joseph Halter"]
|
10
|
+
spec.email = ["jonathan.tron@metrilio.com", "joseph.halter@metrilio.com"]
|
11
|
+
spec.description = %q{Sequel plugin to store empty string as nil}
|
12
|
+
spec.summary = spec.description
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "sequel"
|
22
|
+
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Sequel::Plugins::StringNilifier do
|
4
|
+
before do
|
5
|
+
@db = Sequel::Database.new
|
6
|
+
@c = Class.new(Sequel::Model(@db[:test]))
|
7
|
+
@c.columns :name, :b
|
8
|
+
@c.db_schema[:b][:type] = :blob
|
9
|
+
@c.plugin :string_nilifier
|
10
|
+
@o = @c.new
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should convert empty string to nil for all input strings" do
|
14
|
+
@o.name = " \n"
|
15
|
+
@o.name.should be_nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should not convert non-empty string" do
|
19
|
+
@o.name = "a \n"
|
20
|
+
@o.name.should == "a \n"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should not affect other types" do
|
24
|
+
@o.name = 1
|
25
|
+
@o.name.should == 1
|
26
|
+
@o.name = Date.today
|
27
|
+
@o.name.should == Date.today
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should not nilify strings for blob arguments" do
|
31
|
+
v = Sequel.blob(" \n")
|
32
|
+
@o.name = v
|
33
|
+
@o.name.should equal(v)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should not nilify strings for blob columns" do
|
37
|
+
@o.b = " \n"
|
38
|
+
@o.b.should be_a_kind_of(Sequel::SQL::Blob)
|
39
|
+
@o.b.should == Sequel.blob(" \n")
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should allow skipping of columns using Model.skip_string_nilifying" do
|
43
|
+
@c.skip_string_nilifying :name
|
44
|
+
v = " \n"
|
45
|
+
@o.name = v
|
46
|
+
@o.name.should equal(v)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should work correctly in subclasses" do
|
50
|
+
o = Class.new(@c).new
|
51
|
+
o.name = " \n"
|
52
|
+
o.name.should be_nil
|
53
|
+
o.b = " \n"
|
54
|
+
o.b.should be_a_kind_of(Sequel::SQL::Blob)
|
55
|
+
o.b.should == Sequel.blob(" \n")
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should work correctly for dataset changes" do
|
59
|
+
c = Class.new(Sequel::Model(@db[:test]))
|
60
|
+
c.plugin :string_nilifier
|
61
|
+
def @db.schema(*) [[:name, {}], [:b, {:type=>:blob}]] end
|
62
|
+
c.set_dataset(@db[:test2])
|
63
|
+
o = c.new
|
64
|
+
o.name = " \n"
|
65
|
+
o.name.should be_nil
|
66
|
+
o.b = " \n"
|
67
|
+
o.b.should be_a_kind_of(Sequel::SQL::Blob)
|
68
|
+
o.b.should == Sequel.blob(" \n")
|
69
|
+
end
|
70
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "sequel"
|
2
|
+
require "sequel/string_nilifier"
|
3
|
+
|
4
|
+
class << Sequel::Model
|
5
|
+
attr_writer :db_schema
|
6
|
+
alias orig_columns columns
|
7
|
+
def columns(*cols)
|
8
|
+
return super if cols.empty?
|
9
|
+
define_method(:columns){cols}
|
10
|
+
@dataset.instance_variable_set(:@columns, cols) if @dataset
|
11
|
+
def_column_accessor(*cols)
|
12
|
+
@columns = cols
|
13
|
+
@db_schema = {}
|
14
|
+
cols.each{|c| @db_schema[c] = {}}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
Sequel::Model.use_transactions = false
|
19
|
+
Sequel::Model.cache_anonymous_models = false
|
20
|
+
|
21
|
+
db = Sequel.mock(:fetch=>{:id => 1, :x => 1}, :numrows=>1, :autoid=>proc{|sql| 10})
|
22
|
+
def db.schema(*) [[:id, {:primary_key=>true}]] end
|
23
|
+
def db.reset() sqls end
|
24
|
+
Sequel::Model.db = MODEL_DB = db
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel-string_nilifier
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jonathan Tron
|
9
|
+
- Joseph Halter
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-03-07 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: sequel
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
none: false
|
23
|
+
type: :runtime
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ! '>='
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '0'
|
29
|
+
none: false
|
30
|
+
prerelease: false
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: rake
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
none: false
|
39
|
+
type: :development
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
none: false
|
46
|
+
prerelease: false
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
none: false
|
55
|
+
type: :development
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
none: false
|
62
|
+
prerelease: false
|
63
|
+
description: Sequel plugin to store empty string as nil
|
64
|
+
email:
|
65
|
+
- jonathan.tron@metrilio.com
|
66
|
+
- joseph.halter@metrilio.com
|
67
|
+
executables: []
|
68
|
+
extensions: []
|
69
|
+
extra_rdoc_files: []
|
70
|
+
files:
|
71
|
+
- .gitignore
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- lib/sequel/plugins/string_nilifier.rb
|
77
|
+
- lib/sequel/plugins/string_nilifier/version.rb
|
78
|
+
- lib/sequel/string_nilifier.rb
|
79
|
+
- sequel-string_nilifier.gemspec
|
80
|
+
- spec/sequel/plugins/string_nilifier_spec.rb
|
81
|
+
- spec/spec_helper.rb
|
82
|
+
homepage: ''
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
hash: 289012220966116596
|
96
|
+
version: '0'
|
97
|
+
none: false
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ! '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
hash: 289012220966116596
|
105
|
+
version: '0'
|
106
|
+
none: false
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 1.8.25
|
110
|
+
signing_key:
|
111
|
+
specification_version: 3
|
112
|
+
summary: Sequel plugin to store empty string as nil
|
113
|
+
test_files:
|
114
|
+
- spec/sequel/plugins/string_nilifier_spec.rb
|
115
|
+
- spec/spec_helper.rb
|