sequel-enhancements 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/Rakefile +10 -0
- data/lib/sequel/enhancements.rb +17 -0
- data/lib/sequel/enhancements/version.rb +5 -0
- data/lib/sequel/extensions/sqlite_json.rb +93 -0
- data/lib/sequel/plugins/hash_cleaner.rb +34 -0
- data/lib/sequel/plugins/string_cleaner.rb +13 -0
- data/lib/sequel/plugins/string_downcaser.rb +38 -0
- data/lib/sequel/plugins/string_nilifier.rb +38 -0
- data/lib/sequel/plugins/string_upcaser.rb +38 -0
- data/sequel-enhancements.gemspec +27 -0
- data/spec/extensions/sqlite_json_spec.rb +42 -0
- data/spec/plugins/hash_cleaner_spec.rb +59 -0
- data/spec/plugins/string_cleaner_spec.rb +38 -0
- data/spec/plugins/string_downcaser_spec.rb +42 -0
- data/spec/plugins/string_nilifier_spec.rb +48 -0
- data/spec/plugins/string_upcaser_spec.rb +42 -0
- data/spec/spec_helper.rb +43 -0
- metadata +142 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2840f32e514ff77d17e198107a31d70b8d5e6759
|
4
|
+
data.tar.gz: 29237adc425e5bbc447c0156e77bab590f7b0d79
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0bd03fced0217c9f652c1d51313c974dd2bf63a9c79cf04cfa4330772503390944e520476dd4989bedf77f13f345d0500921712ec996e8c8ebe7f69a3c841fd1
|
7
|
+
data.tar.gz: f7e1af7cd2920d737dc2300312bbc91156a97a8f90c0195a4a37ffabec7d266585fde2e76c80d233e84b321f3e942f70e10708d261096b0e75ea3167cc09a437
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 peer60
|
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,48 @@
|
|
1
|
+
# Sequel::Enhancements
|
2
|
+
|
3
|
+
peer60 plugins and extensions for [Sequel](http://sequel.jeremyevans.net/)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'sequel-enhancements'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install sequel-enhancements
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Require plugins and extensions as normal
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
Sequel::Model.plugin :string_nilifier
|
27
|
+
```
|
28
|
+
|
29
|
+
## Included plugins
|
30
|
+
|
31
|
+
- `hash_cleaner` strips trailing and leading invisibles from
|
32
|
+
- `string_cleaner` a stronger version of `string_stripper`, removing all invisibles
|
33
|
+
- `string_downcaser` force downcasing of specified columns
|
34
|
+
- `string_nilifier` sets empty strings to nil
|
35
|
+
- `string_upcaser` force upcasing of specified columns
|
36
|
+
|
37
|
+
## Included extensions
|
38
|
+
|
39
|
+
- `sqlite_json` json support for SQLite
|
40
|
+
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it ( https://github.com/[my-github-username]/sequel-enhancements/fork )
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "sequel/enhancements/version"
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
module Enhancements
|
5
|
+
extend self
|
6
|
+
SURROUNDING_INVISIBLES = /\A[^[:graph:]]+|[^[:graph:]]+\z/.freeze
|
7
|
+
|
8
|
+
|
9
|
+
def trim_invisible(str)
|
10
|
+
if str.is_a?(String) && !str.is_a?(SQL::Blob)
|
11
|
+
str.gsub(SURROUNDING_INVISIBLES, '')
|
12
|
+
else
|
13
|
+
str
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module Sequel
|
2
|
+
module SQLite
|
3
|
+
class JSONArray < DelegateClass(Array)
|
4
|
+
include Sequel::SQL::AliasMethods
|
5
|
+
|
6
|
+
# Convert the array to a json string, append a
|
7
|
+
# literalized version of the string to the sql, and explicitly
|
8
|
+
# cast the string to json.
|
9
|
+
def sql_literal_append(ds, sql)
|
10
|
+
ds.literal_append(sql, Sequel.object_to_json(self))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class JSONHash < DelegateClass(Hash)
|
15
|
+
include Sequel::SQL::AliasMethods
|
16
|
+
|
17
|
+
# Convert the hash to a json string, append a
|
18
|
+
# literalized version of the string to the sql, and explicitly
|
19
|
+
# cast the string to json.
|
20
|
+
def sql_literal_append(ds, sql)
|
21
|
+
ds.literal_append(sql, Sequel.object_to_json(self))
|
22
|
+
end
|
23
|
+
|
24
|
+
# Return the object being delegated to.
|
25
|
+
alias to_hash __getobj__
|
26
|
+
end
|
27
|
+
|
28
|
+
module DatabaseMethods
|
29
|
+
JSON_TYPES = %w{
|
30
|
+
json varchar[] text[] integer[] int[] boolean[] money[]
|
31
|
+
}.map(&:freeze).freeze
|
32
|
+
|
33
|
+
def self.extended(db)
|
34
|
+
procs = db.conversion_procs
|
35
|
+
parse_method = method :db_parse_json
|
36
|
+
JSON_TYPES.each do |type|
|
37
|
+
procs[type] = parse_method
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.db_parse_json(s)
|
42
|
+
parse_json(s)
|
43
|
+
rescue Sequel.json_parser_error_class => e
|
44
|
+
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.parse_json(s)
|
48
|
+
begin
|
49
|
+
value = Sequel.parse_json(s)
|
50
|
+
rescue Sequel.json_parser_error_class => e
|
51
|
+
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
52
|
+
end
|
53
|
+
|
54
|
+
case value
|
55
|
+
when Array
|
56
|
+
JSONArray.new(value)
|
57
|
+
when Hash
|
58
|
+
JSONHash.new(value)
|
59
|
+
else
|
60
|
+
raise Sequel::InvalidValue, "unhandled json value: #{value.inspect} (from #{s.inspect})"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def schema_column_type(db_type)
|
65
|
+
case db_type
|
66
|
+
when 'json'
|
67
|
+
'json'
|
68
|
+
when /\[\]$/
|
69
|
+
'json'
|
70
|
+
else
|
71
|
+
super
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def typecast_value_json(value)
|
76
|
+
case value
|
77
|
+
when JSONArray, JSONHash
|
78
|
+
value
|
79
|
+
when Array
|
80
|
+
JSONArray.new(value)
|
81
|
+
when Hash
|
82
|
+
JSONHash.new(value)
|
83
|
+
when String
|
84
|
+
DatabaseMethods.parse_json(value)
|
85
|
+
else
|
86
|
+
raise Sequel::InvalidValue, "invalid value for json: #{value.inspect}"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
Database.register_extension :sqlite_json, SQLite::DatabaseMethods
|
93
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "sequel/enhancements"
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
module Plugins
|
5
|
+
module HashCleaner
|
6
|
+
SURROUNDING_INVISIBLES = /\A[^[:graph:]]+|[^[:graph:]]+\z/.freeze
|
7
|
+
|
8
|
+
def self.apply(model, *args)
|
9
|
+
model.plugin(:input_transformer, :hash_cleaner){ |v| v.is_a?(Hash) ? clean(v) : v }
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def self.clean(value)
|
15
|
+
if value.is_a?(Hash)
|
16
|
+
value.each_with_object({}) do |(k, v), memo|
|
17
|
+
cleaned = clean v
|
18
|
+
memo[k] = cleaned unless exclude?(cleaned)
|
19
|
+
end
|
20
|
+
else
|
21
|
+
Sequel::Enhancements.trim_invisible(value)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.clean_value(value)
|
26
|
+
value.is_a?(String) ? value.trim_invisible : value
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.exclude?(value)
|
30
|
+
value.nil? || (value.is_a?(String) && value.empty?)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "sequel/enhancements"
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
module Plugins
|
5
|
+
module StringDowncaser
|
6
|
+
def self.apply(model, *args)
|
7
|
+
model.instance_eval do
|
8
|
+
@downcase_columns = Set.new
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.configure(model, column)
|
13
|
+
model.add_downcase_column column
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
Plugins.inherited_instance_variables(self, :@downcase_columns => :dup)
|
18
|
+
|
19
|
+
def add_downcase_column(column)
|
20
|
+
@downcase_columns << column
|
21
|
+
end
|
22
|
+
|
23
|
+
def downcase_column?(column)
|
24
|
+
@downcase_columns.include? column
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module InstanceMethods
|
29
|
+
def []=(k, v)
|
30
|
+
if model.downcase_column?(k) && v.is_a?(String) && !v.is_a?(SQL::Blob)
|
31
|
+
v = v.downcase
|
32
|
+
end
|
33
|
+
super
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "sequel/enhancements"
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
module Plugins
|
5
|
+
module StringNilifier
|
6
|
+
def self.apply(model)
|
7
|
+
model.plugin(:input_transformer, :string_nilifier) do |v|
|
8
|
+
(v.is_a?(String) && !v.is_a?(SQL::Blob) && v.empty?) ? nil : v
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.configure(model)
|
13
|
+
model.instance_eval{set_skipped_string_nilifying_columns if @dataset}
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
Plugins.after_set_dataset(self, :set_skipped_string_nilifying_columns)
|
18
|
+
|
19
|
+
def skip_string_nilifying(*columns)
|
20
|
+
skip_input_transformer(:string_nilifier, *columns)
|
21
|
+
end
|
22
|
+
|
23
|
+
def skip_string_nilifying?(column)
|
24
|
+
skip_input_transformer?(:string_nilifier, column)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def set_skipped_string_nilifying_columns
|
30
|
+
if @db_schema
|
31
|
+
blob_columns = @db_schema.map{|k,v| k if v[:type] == :blob}.compact
|
32
|
+
skip_string_nilifying(*blob_columns)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "sequel/enhancements"
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
module Plugins
|
5
|
+
module StringUpcaser
|
6
|
+
def self.apply(model, *args)
|
7
|
+
model.instance_eval do
|
8
|
+
@upcase_columns = Set.new
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.configure(model, column)
|
13
|
+
model.add_upcase_column column
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
Plugins.inherited_instance_variables(self, :@upcase_columns => :dup)
|
18
|
+
|
19
|
+
def add_upcase_column(column)
|
20
|
+
@upcase_columns << column
|
21
|
+
end
|
22
|
+
|
23
|
+
def upcase_column?(column)
|
24
|
+
@upcase_columns.include? column
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module InstanceMethods
|
29
|
+
def []=(k, v)
|
30
|
+
if model.upcase_column?(k) && v.is_a?(String) && !v.is_a?(SQL::Blob)
|
31
|
+
v = v.upcase
|
32
|
+
end
|
33
|
+
super
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sequel/enhancements/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sequel-enhancements"
|
8
|
+
spec.version = Sequel::Enhancements::VERSION
|
9
|
+
spec.authors = ["peer60"]
|
10
|
+
spec.email = ["development@peer60.com"]
|
11
|
+
spec.summary = %q{Plugins and extensions for Sequel}
|
12
|
+
spec.description = %q{Plugins and extensions for Sequel}
|
13
|
+
spec.homepage = "https://bitbucket.org/peer60/sequel-enhancements"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.4"
|
25
|
+
spec.add_development_dependency "sequel"
|
26
|
+
spec.add_development_dependency "sqlite3"
|
27
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
describe 'sqlite_json extension' do
|
5
|
+
let(:db){ Sequel.connect("sqlite:///") }
|
6
|
+
let(:model_class){ Class.new(Sequel::Model(db[:people])){ @plugins.clear } }
|
7
|
+
|
8
|
+
subject { model_class.new }
|
9
|
+
|
10
|
+
before do
|
11
|
+
db.extension :sqlite_json
|
12
|
+
db.create_table(:people) do
|
13
|
+
primary_key :id
|
14
|
+
column :name, :text
|
15
|
+
column :address, :json
|
16
|
+
column :phones, 'text[]'
|
17
|
+
column :lucky_numbers, 'int[]'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'stores and retrieves json object' do
|
22
|
+
original = {'street' => '123 fake'}
|
23
|
+
subject.name = 'foo'
|
24
|
+
subject.address = original
|
25
|
+
subject.save
|
26
|
+
subject.address.must_equal original
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'stores and retrieves arrays of strings' do
|
30
|
+
original = ['18008675309', '18005555555']
|
31
|
+
subject.phones = original
|
32
|
+
subject.save
|
33
|
+
subject.phones.must_equal original
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'stores and retrieves arrays of ints' do
|
37
|
+
original = ['1','2','3']
|
38
|
+
subject.lucky_numbers = original
|
39
|
+
subject.save
|
40
|
+
subject.lucky_numbers.must_equal original
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Sequel::Plugins::HashCleaner' do
|
4
|
+
let(:db){ Sequel.mock }
|
5
|
+
let(:model_class){ Class.new(Sequel::Model(db[:test])){ @plugins.clear } }
|
6
|
+
|
7
|
+
subject { model_class.new }
|
8
|
+
|
9
|
+
before do
|
10
|
+
model_class.columns :json_field, :other
|
11
|
+
model_class.plugin :hash_cleaner
|
12
|
+
end
|
13
|
+
|
14
|
+
after do
|
15
|
+
model_class.instance_variable_set :'@plugins', @previous_plugins
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'clears nil' do
|
19
|
+
subject.json_field = {'value' => nil}
|
20
|
+
subject.json_field.must_equal({})
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'clears empty word' do
|
24
|
+
subject.json_field = {'value' => ' '}
|
25
|
+
subject.json_field.must_equal({})
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'cleans word' do
|
29
|
+
expected = 'something'
|
30
|
+
subject.json_field = {'value' => "\xC2\xA0 #{expected}\xC2\xA0 "}
|
31
|
+
subject.json_field.must_equal 'value' => expected
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'doesnt touch inner characters' do
|
35
|
+
expected = "some \t \xC2\xA0 \n thing"
|
36
|
+
subject.json_field = {'value' => "\xC2\xA0 #{expected}\xC2\xA0 "}
|
37
|
+
subject.json_field.must_equal 'value' => expected
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'doesnt affect falsy values' do
|
41
|
+
expected = 0
|
42
|
+
subject.json_field = {'value' => expected}
|
43
|
+
subject.json_field.must_equal 'value' => expected
|
44
|
+
|
45
|
+
expected = false
|
46
|
+
subject.json_field = {'value' => expected}
|
47
|
+
subject.json_field.must_equal 'value' => expected
|
48
|
+
|
49
|
+
expected = []
|
50
|
+
subject.json_field = {'value' => expected}
|
51
|
+
subject.json_field.must_equal 'value' => expected
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'doesnt affect non-hash value' do
|
55
|
+
expected = " can't touch this "
|
56
|
+
subject.json_field = expected
|
57
|
+
subject.json_field.must_equal expected
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Sequel::Plugins::StringCleaner' do
|
4
|
+
let(:db){ Sequel.mock }
|
5
|
+
let(:model_class){ Class.new(Sequel::Model(db[:test])){ @plugins.clear } }
|
6
|
+
|
7
|
+
subject { model_class.new }
|
8
|
+
|
9
|
+
before do
|
10
|
+
model_class.columns :name, :other
|
11
|
+
model_class.plugin :string_cleaner
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'cleans word' do
|
15
|
+
expected = 'something'
|
16
|
+
subject.name = "\xC2\xA0 #{expected}\xC2\xA0 "
|
17
|
+
subject.name.must_equal expected
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'doesnt touch inner characters' do
|
21
|
+
expected = "some \t \xC2\xA0 \n thing"
|
22
|
+
subject.name = "\xC2\xA0 #{expected}\xC2\xA0 "
|
23
|
+
subject.name.must_equal expected
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'doesnt affect non-string inputs' do
|
27
|
+
expected = rand_i
|
28
|
+
subject.name = expected
|
29
|
+
subject.name.must_equal expected
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'doesnt blob alone' do
|
33
|
+
expected = Sequel.blob " #{rand_s} "
|
34
|
+
subject.name = expected
|
35
|
+
subject.name.must_be_instance_of Sequel::SQL::Blob
|
36
|
+
subject.name.must_equal expected
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Sequel::Plugins::StringDowncaser' do
|
4
|
+
let(:db){ Sequel.mock }
|
5
|
+
let(:model_class){ Class.new(Sequel::Model(db[:test])){ @plugins.clear } }
|
6
|
+
|
7
|
+
subject { model_class.new }
|
8
|
+
|
9
|
+
before do
|
10
|
+
model_class.columns :name, :other
|
11
|
+
model_class.plugin :string_downcaser, :name
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'downcases specified column' do
|
15
|
+
expected = rand_s
|
16
|
+
subject.name = expected.upcase
|
17
|
+
subject.name.must_equal expected
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'doesnt affect unspecified columns' do
|
21
|
+
expected = rand_s.upcase
|
22
|
+
subject.other = expected
|
23
|
+
subject.other.must_equal expected
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'handles nil value' do
|
27
|
+
subject.name = nil
|
28
|
+
subject.name.must_be_nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'handles non-string value' do
|
32
|
+
subject.name = 7
|
33
|
+
subject.name.must_equal 7
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'leaves blob alone' do
|
37
|
+
expected = Sequel.blob rand_s.upcase
|
38
|
+
subject.name = expected
|
39
|
+
subject.name.must_be_instance_of Sequel::SQL::Blob
|
40
|
+
subject.name.must_equal expected
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Sequel::Plugins::StringNilifier' do
|
4
|
+
let(:db){ Sequel.mock }
|
5
|
+
let(:model_class){ Class.new(Sequel::Model(db[:test])){ @plugins.clear } }
|
6
|
+
|
7
|
+
subject { model_class.new }
|
8
|
+
|
9
|
+
before do
|
10
|
+
model_class.columns :name, :b
|
11
|
+
model_class.db_schema[:b][:type] = :blob
|
12
|
+
model_class.plugin :string_nilifier
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'nilifies empty strings' do
|
16
|
+
subject.name = ''
|
17
|
+
subject.name.must_be_nil
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'doesnt touch other fields' do
|
21
|
+
subject.name = 'hey'
|
22
|
+
subject.name.must_equal 'hey'
|
23
|
+
|
24
|
+
subject.name = 6
|
25
|
+
subject.name.must_equal 6
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'doesnt affect blob arguments' do
|
29
|
+
expected = Sequel.blob('')
|
30
|
+
subject.name = expected
|
31
|
+
subject.name.must_equal expected
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'doesnt affect blob columns' do
|
35
|
+
model_class.must_be :skip_string_nilifying?, :b
|
36
|
+
subject.b = ''
|
37
|
+
subject.b.must_be_instance_of Sequel::SQL::Blob
|
38
|
+
subject.b.must_equal Sequel.blob('')
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'allows skipping columns with Model.skip_string_nilifying' do
|
42
|
+
model_class.skip_string_nilifying :name
|
43
|
+
model_class.must_be :skip_string_nilifying?, :name
|
44
|
+
subject.name = ''
|
45
|
+
subject.name.must_equal ''
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Sequel::Plugins::StringUpcaser' do
|
4
|
+
let(:db){ Sequel.mock }
|
5
|
+
let(:model_class){ Class.new(Sequel::Model(db[:test])){ @plugins.clear } }
|
6
|
+
|
7
|
+
subject { model_class.new }
|
8
|
+
|
9
|
+
before do
|
10
|
+
model_class.columns :name, :other
|
11
|
+
model_class.plugin :string_upcaser, :name
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'upcases specified column' do
|
15
|
+
expected = rand_s.upcase
|
16
|
+
subject.name = expected.downcase
|
17
|
+
subject.name.must_equal expected
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'doesnt affect unspecified columns' do
|
21
|
+
expected = rand_s.downcase
|
22
|
+
subject.other = expected
|
23
|
+
subject.other.must_equal expected
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'handles nil value' do
|
27
|
+
subject.name = nil
|
28
|
+
subject.name.must_be_nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'handles non-string value' do
|
32
|
+
subject.name = 7
|
33
|
+
subject.name.must_equal 7
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'leaves blob alone' do
|
37
|
+
expected = Sequel.blob rand_s.downcase
|
38
|
+
subject.name = expected
|
39
|
+
subject.name.must_be_instance_of Sequel::SQL::Blob
|
40
|
+
subject.name.must_equal expected
|
41
|
+
end
|
42
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require "sequel"
|
3
|
+
require "minitest/autorun"
|
4
|
+
|
5
|
+
class << Sequel::Model
|
6
|
+
attr_writer :db_schema
|
7
|
+
alias orig_columns columns
|
8
|
+
def columns(*cols)
|
9
|
+
return super if cols.empty?
|
10
|
+
define_method(:columns){cols}
|
11
|
+
@dataset.instance_variable_set(:@columns, cols) if @dataset
|
12
|
+
def_column_accessor(*cols)
|
13
|
+
@columns = cols
|
14
|
+
@db_schema = {}
|
15
|
+
cols.each{|c| @db_schema[c] = {}}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Minitest::Spec
|
20
|
+
FIXNUM_MAX = 2**(0.size * 8 - 2)
|
21
|
+
|
22
|
+
def rand_s
|
23
|
+
SecureRandom.hex
|
24
|
+
end
|
25
|
+
|
26
|
+
def rand_i(max=FIXNUM_MAX)
|
27
|
+
SecureRandom.random_number(max)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class CustomFilter
|
32
|
+
def self.filter(bt)
|
33
|
+
return ['No backtrace'] unless bt
|
34
|
+
|
35
|
+
new_bt = bt.take_while { |line| line !~ %r{minitest} }
|
36
|
+
new_bt = bt.select { |line| line !~ %r{minitest} } if new_bt.empty?
|
37
|
+
new_bt = bt.dup if new_bt.empty?
|
38
|
+
|
39
|
+
new_bt
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
Minitest.backtrace_filter = CustomFilter
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel-enhancements
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- peer60
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sequel
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Plugins and extensions for Sequel
|
84
|
+
email:
|
85
|
+
- development@peer60.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/sequel/enhancements.rb
|
96
|
+
- lib/sequel/enhancements/version.rb
|
97
|
+
- lib/sequel/extensions/sqlite_json.rb
|
98
|
+
- lib/sequel/plugins/hash_cleaner.rb
|
99
|
+
- lib/sequel/plugins/string_cleaner.rb
|
100
|
+
- lib/sequel/plugins/string_downcaser.rb
|
101
|
+
- lib/sequel/plugins/string_nilifier.rb
|
102
|
+
- lib/sequel/plugins/string_upcaser.rb
|
103
|
+
- sequel-enhancements.gemspec
|
104
|
+
- spec/extensions/sqlite_json_spec.rb
|
105
|
+
- spec/plugins/hash_cleaner_spec.rb
|
106
|
+
- spec/plugins/string_cleaner_spec.rb
|
107
|
+
- spec/plugins/string_downcaser_spec.rb
|
108
|
+
- spec/plugins/string_nilifier_spec.rb
|
109
|
+
- spec/plugins/string_upcaser_spec.rb
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
homepage: https://bitbucket.org/peer60/sequel-enhancements
|
112
|
+
licenses:
|
113
|
+
- MIT
|
114
|
+
metadata: {}
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 2.2.2
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: Plugins and extensions for Sequel
|
135
|
+
test_files:
|
136
|
+
- spec/extensions/sqlite_json_spec.rb
|
137
|
+
- spec/plugins/hash_cleaner_spec.rb
|
138
|
+
- spec/plugins/string_cleaner_spec.rb
|
139
|
+
- spec/plugins/string_downcaser_spec.rb
|
140
|
+
- spec/plugins/string_nilifier_spec.rb
|
141
|
+
- spec/plugins/string_upcaser_spec.rb
|
142
|
+
- spec/spec_helper.rb
|