date_supercharger 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +30 -0
- data/Rakefile +8 -0
- data/date_supercharger.gemspec +29 -0
- data/lib/date_supercharger/version.rb +3 -0
- data/lib/date_supercharger.rb +57 -0
- data/test/date_supercharger_test.rb +68 -0
- data/test/test_helper.rb +22 -0
- metadata +156 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c01f08a024573fe98de61355523127f0d9ec6998
|
4
|
+
data.tar.gz: cfbc18aad068070ba1c16b53d74e7d089cb7a891
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 66bcd7f89b67144f649a5af868f0e99de1a1a32adf5bd0aef4fa7571d7e133f262126de97fc808a99cd3035889dfb351d5880de2f7a9b37a402be1d44d92f355
|
7
|
+
data.tar.gz: b2b81c228967ebaaf21c5dc52ec1dfeded27dd02a76fff8a63022c524f33845d9710f088c253ba22c2e48beda5dc17b176fbb4970a0ee4149ea70514c7c93620
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Andrew Kane
|
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,30 @@
|
|
1
|
+
# Date Supercharger
|
2
|
+
|
3
|
+
"A nice shortcut for date queries"
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application’s Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'date_supercharger'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```sh
|
16
|
+
bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
## History
|
20
|
+
|
21
|
+
View the [changelog](https://github.com/simon0191/date_supercharger/blob/master/CHANGELOG.md)
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
26
|
+
|
27
|
+
- [Report bugs](https://github.com/simon0191/date_supercharger/issues)
|
28
|
+
- Fix bugs and [submit pull requests](https://github.com/simon0191/date_supercharger/pulls)
|
29
|
+
- Write, clarify, or fix documentation
|
30
|
+
- Suggest or add new features
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "date_supercharger/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "date_supercharger"
|
8
|
+
spec.version = DateSupercharger::VERSION
|
9
|
+
spec.authors = ["Simon Soriano"]
|
10
|
+
spec.email = ["simon0191@gmail.com"]
|
11
|
+
spec.summary = "A nice shortcut for date queries"
|
12
|
+
spec.description = "A nice shortcut for date queries"
|
13
|
+
spec.homepage = "https://github.com/simon0191/date_supercharger"
|
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
|
+
spec.add_dependency "activerecord"
|
22
|
+
spec.add_dependency "activesupport"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "minitest"
|
27
|
+
spec.add_development_dependency "sqlite3"
|
28
|
+
spec.add_development_dependency "byebug"
|
29
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "date_supercharger/version"
|
2
|
+
require "active_record"
|
3
|
+
|
4
|
+
module DateSupercharger
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
def self.method_missing(method_sym, *arguments, &block)
|
9
|
+
return super unless descends_from_active_record?
|
10
|
+
match = ModelDateExtensionMatch.new(self,method_sym)
|
11
|
+
|
12
|
+
if match.match?
|
13
|
+
operators = { after: ">", before: "<", before_or_at: "<=", after_or_at: ">=" }
|
14
|
+
singleton_class.class_eval do
|
15
|
+
define_method("#{match.attribute}_#{match.suffix}") do |date|
|
16
|
+
where("#{match.attribute} #{operators[match.suffix]} ?", date)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
send(method_sym, arguments.first)
|
20
|
+
else
|
21
|
+
super
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.respond_to?(method_sym, include_private = false)
|
26
|
+
return super unless descends_from_active_record?
|
27
|
+
if ModelDateExtensionMatch.new(self,method_sym).match?
|
28
|
+
true
|
29
|
+
else
|
30
|
+
super
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class ModelDateExtensionMatch
|
36
|
+
|
37
|
+
attr_accessor :attribute,:suffix
|
38
|
+
|
39
|
+
def initialize(model,method_sym)
|
40
|
+
#TODO: implement between and between_inclusive
|
41
|
+
if method_sym.to_s =~ /^(.+)_(before|after|before_or_at|after_or_at)$/
|
42
|
+
date_columns = model.columns_hash.keys.select { |c| [:datetime,:date].include? model.columns_hash[c].type }.map(&:to_sym)
|
43
|
+
if date_columns.include? $1.to_sym
|
44
|
+
@attribute = $1.to_sym
|
45
|
+
@suffix = $2.to_sym
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
def match?
|
52
|
+
@attribute != nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
ActiveRecord::Base.send :include, DateSupercharger
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class TestDateSupercharger < Minitest::Test
|
4
|
+
extend Minitest::Spec::DSL
|
5
|
+
|
6
|
+
let(:now) { DateTime.now }
|
7
|
+
let(:monday) { DateTime.now.beginning_of_week }
|
8
|
+
let(:tuesday) { monday + 1.day }
|
9
|
+
let(:wednesday) { monday + 2.day }
|
10
|
+
let(:thursday) { monday + 3.day }
|
11
|
+
let(:friday) { monday + 4.day }
|
12
|
+
let(:saturday) { monday + 5.day }
|
13
|
+
let(:sunday) { monday + 6.day }
|
14
|
+
|
15
|
+
before do
|
16
|
+
unless ActiveRecord::Base.connected?
|
17
|
+
establish_connection
|
18
|
+
days = [monday,tuesday,wednesday,thursday,friday,saturday,sunday]
|
19
|
+
days.each do |day|
|
20
|
+
create({city: "Bogota",visit_date: day})
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "ActiveRecord has established a connection to the database" do
|
26
|
+
|
27
|
+
describe "#_after" do
|
28
|
+
it "should not include be inclusive" do
|
29
|
+
assert_equal 6, Visit.visit_date_after(monday).count
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#_after_or_at" do
|
34
|
+
it "should be inclusive" do
|
35
|
+
assert_equal 7, Visit.visit_date_after_or_at(monday).count
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#_before" do
|
40
|
+
it "should not include be inclusive" do
|
41
|
+
assert_equal 6, Visit.visit_date_before(sunday).count
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#_before_or_at" do
|
46
|
+
it "should be inclusive" do
|
47
|
+
assert_equal 7, Visit.visit_date_before_or_at(sunday).count
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "ActiveRecord has not established a connection to the database" do
|
54
|
+
before do
|
55
|
+
ActiveRecord::Base.remove_connection
|
56
|
+
end
|
57
|
+
it "should not raise exception" do
|
58
|
+
assert_equal false, Visit.respond_to?(:unexisting_method)
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def create(attributes, count = 1)
|
66
|
+
count.times { Visit.create!(attributes) }
|
67
|
+
end
|
68
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
Bundler.require(:default)
|
3
|
+
require "minitest/autorun"
|
4
|
+
require "minitest/pride"
|
5
|
+
require "logger"
|
6
|
+
require "byebug"
|
7
|
+
|
8
|
+
# for debugging
|
9
|
+
# ActiveRecord::Base.logger = Logger.new(STDOUT)
|
10
|
+
|
11
|
+
# migrations
|
12
|
+
def establish_connection
|
13
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
14
|
+
|
15
|
+
ActiveRecord::Migration.create_table :visits do |t|
|
16
|
+
t.string :city
|
17
|
+
t.datetime :visit_date
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Visit < ActiveRecord::Base
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date_supercharger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Simon Soriano
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-05 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: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.6'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.6'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
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: minitest
|
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
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: byebug
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: A nice shortcut for date queries
|
112
|
+
email:
|
113
|
+
- simon0191@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- .travis.yml
|
120
|
+
- CHANGELOG.md
|
121
|
+
- Gemfile
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- date_supercharger.gemspec
|
126
|
+
- lib/date_supercharger.rb
|
127
|
+
- lib/date_supercharger/version.rb
|
128
|
+
- test/date_supercharger_test.rb
|
129
|
+
- test/test_helper.rb
|
130
|
+
homepage: https://github.com/simon0191/date_supercharger
|
131
|
+
licenses:
|
132
|
+
- MIT
|
133
|
+
metadata: {}
|
134
|
+
post_install_message:
|
135
|
+
rdoc_options: []
|
136
|
+
require_paths:
|
137
|
+
- lib
|
138
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - '>='
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - '>='
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
requirements: []
|
149
|
+
rubyforge_project:
|
150
|
+
rubygems_version: 2.4.8
|
151
|
+
signing_key:
|
152
|
+
specification_version: 4
|
153
|
+
summary: A nice shortcut for date queries
|
154
|
+
test_files:
|
155
|
+
- test/date_supercharger_test.rb
|
156
|
+
- test/test_helper.rb
|