find_near_date 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 72ad5021366a0f0adfca387a555c57bd6c4fc69d4d7434ef6459bf4066226009
4
+ data.tar.gz: b05fc85ea92fb4f556f2f046a1db6e2add1f5f0db3641d07df18966726f59147
5
+ SHA512:
6
+ metadata.gz: 6f61b4f9e5951fc01fe661ac9b6e28f39e07eb72933c9efc55b5c05f0cbc38338b9372f39aba95e5212495c74ed8d6e6783f4706b881184e7322a70462abdf6a
7
+ data.tar.gz: a2bb352d708123be5eccae2eaf8e300fad94bf302eb3bda6ea9a863adda630b0f92ec41ce003185e336dd36f562be2cfc97dad95715bdcc1b60e2fe16d330194
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,10 @@
1
+ inherit_gem:
2
+ onkcop:
3
+ - "config/rubocop.yml"
4
+ # uncomment if use rails cops
5
+ # - "config/rails.yml"
6
+ # uncomment if use rspec cops
7
+ # - "config/rspec.yml"
8
+
9
+ AllCops:
10
+ TargetRubyVersion: 2.5
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in find_near_date.gemspec
6
+ gemspec
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ find_near_date (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ onkcop (0.53.0.0)
11
+ rubocop (~> 0.53.0)
12
+ rubocop-rspec (>= 1.24.0)
13
+ parallel (1.12.1)
14
+ parser (2.5.1.0)
15
+ ast (~> 2.4.0)
16
+ powerpack (0.1.2)
17
+ rainbow (3.0.0)
18
+ rake (10.5.0)
19
+ rubocop (0.53.0)
20
+ parallel (~> 1.10)
21
+ parser (>= 2.5)
22
+ powerpack (~> 0.1)
23
+ rainbow (>= 2.2.2, < 4.0)
24
+ ruby-progressbar (~> 1.7)
25
+ unicode-display_width (~> 1.0, >= 1.0.1)
26
+ rubocop-rspec (1.26.0)
27
+ rubocop (>= 0.53.0)
28
+ ruby-progressbar (1.9.0)
29
+ unicode-display_width (1.4.0)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ bundler (~> 1.16)
36
+ find_near_date!
37
+ onkcop
38
+ rake (~> 10.0)
39
+
40
+ BUNDLED WITH
41
+ 1.16.1
@@ -0,0 +1,63 @@
1
+ # FindNearDate
2
+
3
+ find record by date from DB tables that is not indexed by date column (for ActiveRecord)
4
+
5
+ Caution: not for production app. for only human operation help.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'find_near_date'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install find_near_date
22
+
23
+ ## Usage
24
+
25
+ app/models/foo.rb
26
+
27
+ ```ruby
28
+ class Foo < ActiveRecord::Base
29
+ end
30
+ ```
31
+
32
+ lib/pry_helper.rb
33
+
34
+ ```ruby
35
+ require "find_near_date"
36
+
37
+ class Foo
38
+ extend FindNearDate
39
+ end
40
+ ```
41
+
42
+ in pry
43
+
44
+ ```
45
+ > require "lib/pry_helper"
46
+ > foo_10days_ago = Foo.find_near_date(10.days.ago, :updated_at, period: 1.day)
47
+ ```
48
+
49
+ etc.
50
+
51
+ ## Development
52
+
53
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
54
+
55
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Narazaka/find_near_date.
60
+
61
+ ## License
62
+
63
+ This is released under the [Zlib License](https://narazaka.net/license/Zlib?2018)
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "find_near_date"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("lib", __dir__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "find_near_date/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "find_near_date"
8
+ spec.version = FindNearDate::VERSION
9
+ spec.licenses = ["Zlib"]
10
+ spec.authors = ["Narazaka"]
11
+ spec.email = ["info@narazaka.net"]
12
+
13
+ spec.summary = "find record by date from DB tables that is not indexed by date column"
14
+ spec.homepage = "https://github.com/Narazaka/find_near_date"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "activerecord", ">= 4"
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "onkcop"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,40 @@
1
+ require "find_near_date/version"
2
+
3
+ module FindNearDate
4
+ # find record by date from DB tables that is not indexed by date column
5
+ # @param [Time] date date to find
6
+ # @param [Symbol] date_column date column name
7
+ # @param [Symbol] key_column primary_key column
8
+ # @param [Number] period acceptable defference period. if this is 0, find most near date
9
+ # @param [Boolean] log print log or not
10
+ def find_near_date(date, date_column = :created_at, key_column: primary_key, period: 0, log: false)
11
+ _first = select(key_column, date_column).order(key_column => :asc).first
12
+ _last = select(key_column, date_column).order(key_column => :desc).first
13
+ find_near_date_iteration(_first, _last, _last, date, date_column, key_column, period, log)
14
+ end
15
+
16
+ def find_near_date_iteration(_first, _last, _previous, date, date_column, key_column, period, log) # rubocop:disable Metrics/AbcSize, Metrics/ParameterLists, Metrics/LineLength
17
+ start_key = _first.public_send(key_column)
18
+ end_key = _last.public_send(key_column)
19
+ key_period = end_key - start_key
20
+ start_date = _first.public_send(date_column)
21
+ end_date = _last.public_send(date_column)
22
+ date_period = end_date - start_date
23
+ rate = 1.0 * (date - start_date) / date_period
24
+ target_key = (key_period * rate).round + start_key
25
+ _next = where("#{key_column} >= ?", target_key).order(key_column => :asc).first # id may not be exists
26
+ return _next if _next.public_send(key_column) == _previous.public_send(key_column)
27
+
28
+ next_date = _next.public_send(date_column)
29
+ date_diff = next_date - date
30
+ return _next if date_diff == 0 || date_diff.abs <= period
31
+
32
+ if date_diff > 0
33
+ _last = _next
34
+ else
35
+ _first = _next
36
+ end
37
+ puts "find_date_id> diff=#{date_diff} target=#{date} current #{key_column}=#{target_key} [#{start_key}=#{start_date} - #{end_key}=#{end_date}]" if log
38
+ find_near_date_iteration(_first, _last, _next, date, date_column, key_column, period, log)
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module FindNearDate
2
+ VERSION = "0.1.0".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: find_near_date
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Narazaka
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-13 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: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: onkcop
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
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description:
70
+ email:
71
+ - info@narazaka.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rubocop.yml"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - find_near_date.gemspec
85
+ - lib/find_near_date.rb
86
+ - lib/find_near_date/version.rb
87
+ homepage: https://github.com/Narazaka/find_near_date
88
+ licenses:
89
+ - Zlib
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.7.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: find record by date from DB tables that is not indexed by date column
111
+ test_files: []