checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9f1a8e83ab2ee29e39fe322af75225c0afb71fe45ce0c9ac0a9d93a4c694351d
4
+ data.tar.gz: 910ece0c01d8bc20dd23e2538fd878c4b74f2522a77b59f949df326ea3668afd
5
+ SHA512:
6
+ metadata.gz: 2456666472f46222eb6f042fe494a47d62cb55e79be77e3245069fae1f8f90a0762c3ea0b8e2e1b057610a344311b28b0722d8bff5a343d46f2a30c54ff066c2
7
+ data.tar.gz: ff90a662e004f3a9137f887d74ad972ab2a330e0aec5b8a621b400ec06dfac6fee5949784911611d0d63eb98ff972f430041af75757b7abe87b495d515c8c7d0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 shoma07
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # SQLite3ExtendFunction
2
+
3
+ Add some user-defined function to SQL when using SQLie3 with ActiveRecord.
4
+
5
+ ## Functions
6
+
7
+ - ceil
8
+ - floor
9
+
10
+ ```
11
+ ActiveRecord::Base.connection.select_one("select ceil(0.1)")
12
+ => {"ceil(0.1)"=>1}
13
+
14
+ ActiveRecord::Base.connection.select_one("select floor(1.1)")
15
+ => {"floor(1.1)"=>1}
16
+ ```
17
+
18
+ ## Installation
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'sqlite3_extend_function', '~> 0.1'
23
+ ```
24
+
25
+ And then execute:
26
+ ```bash
27
+ $ bundle
28
+ ```
29
+
30
+ ## License
31
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rdoc/task'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'SQLite3ExtendFunction'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ require 'rspec/core/rake_task'
20
+ RSpec::Core::RakeTask.new(:spec)
21
+ task default: :spec
22
+
23
+ require 'bundler/gem_tasks'
data/lib/sqlite3_extend_function.rb ADDED
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'sqlite3_extend_function/version'
5
+ require 'sqlite3_extend_function/function'
6
+ require 'sqlite3_extend_function/configuration'
7
+
8
+ # SQLite3ExtendFunction
9
+ module SQLite3ExtendFunction
10
+ extend Configuration
11
+
12
+ def self.included(klass)
13
+ klass.set_callback(:checkout, :before, lambda { |conn|
14
+ return if conn.adapter_name != 'SQLite'
15
+
16
+ SQLite3ExtendFunction.options[:functions].each do |func|
17
+ lmd = Function.send(func)
18
+ args = lmd.parameters.size - 1
19
+ conn.raw_connection.create_function(func.to_s, args, &lmd)
20
+ end
21
+ })
22
+ end
23
+ end
24
+
25
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.include(SQLite3ExtendFunction)
data/lib/sqlite3_extend_function/configuration.rb ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ # SQLite3ExtendFunction::Configuration
5
+ module Configuration
6
+ mattr_accessor :options
7
+
8
+ self.options = {
9
+ functions: Function.singleton_methods
10
+ }
11
+
12
+ def configure
13
+ yield self
14
+ end
15
+
16
+ # @param [Array] functions
17
+ def functions=(functions)
18
+ options[:functions] = Function.singleton_methods & functions
19
+ end
20
+ end
21
+ end
data/lib/sqlite3_extend_function/function.rb ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ # SQLite3ExtendFunction::Function
5
+ module Function
6
+ class << self
7
+ # @return [Proc]
8
+ def ceil
9
+ lambda do |func, value|
10
+ func.result = if value.nil?
11
+ nil
12
+ elsif value.is_a? Numeric
13
+ value.ceil
14
+ else
15
+ 0
16
+ end
17
+ end
18
+ end
19
+
20
+ # @return [Proc]
21
+ def floor
22
+ lambda do |func, value|
23
+ func.result = if value.nil?
24
+ nil
25
+ elsif value.is_a? Numeric
26
+ value.floor
27
+ else
28
+ 0
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
data/lib/sqlite3_extend_function/version.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLite3ExtendFunction
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sqlite3_extend_function
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - shoma07
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Add some user-defined functions to SQL when using SQLie3 with Rails.
56
+ email:
57
+ - 23730734+shoma07@users.noreply.github.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/sqlite3_extend_function.rb
66
+ - lib/sqlite3_extend_function/configuration.rb
67
+ - lib/sqlite3_extend_function/function.rb
68
+ - lib/sqlite3_extend_function/version.rb
69
+ homepage: https://github.com/shoma07/sqlite3_extend_function
70
+ licenses:
71
+ - MIT
72
+ metadata:
73
+ homepage_uri: https://github.com/shoma07/sqlite3_extend_function
74
+ source_code_uri: https://github.com/shoma07/sqlite3_extend_function
75
+ changelog_uri: https://github.com/shoma07/sqlite3_extend_function/blob/v0.1.0/CHANGELOG.md
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '2'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubygems_version: 3.0.3
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Add some user-defined functions to SQL when using SQLie3 with Rails.
95
+ test_files: []