time_frame 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16119ae2815aedbad4ad320c8e31c4bf7c1d9389
4
- data.tar.gz: 93f06cafdda61db5d3ab2b68ce126307c1f9bbf9
3
+ metadata.gz: bff74b0836d124c9e908e78228d6d2d6ce69a5c3
4
+ data.tar.gz: 2f508be6ca07c0d76e33c09da5491c69ebceed95
5
5
  SHA512:
6
- metadata.gz: 124139bd01a35c6e7e9ae9ca22aa4c042bcc0b3df0880ba2814bfe57eef074a55766515bd31ca2e4b5bc660a60400ac2e35af492bab8347e7b5efe589f7f4b1f
7
- data.tar.gz: 906d7abb6482845a0fd455e43520cbf79b6b8951ab12e7590f8b8bdb91cb960403fc90ec94181afa326245447170ec9dea5dad29e709ff55f963bd443b313bf4
6
+ metadata.gz: e46135147bb82bd9eead8ea48855359068dd1b8788547c57176c46b9791e17b6f7c76bc0ad193947cbfeca41ead41f4f785b598d95b0b4b059f9645084fd0ea5
7
+ data.tar.gz: fa004da36e99f6b3c0cba0e00e5f713d405013935a37e4b412f765dbeb7d9e1b630695efbffbacc8865486175f4f3b552ae069c17575159c9f06fdff9b5347a4
data/.rubocop.yml CHANGED
@@ -1,5 +1,7 @@
1
1
  AllCops:
2
- Exclude: []
2
+ Exclude:
3
+ - 'vendor/**/*'
4
+ - 'spec/models/vogon_poem.rb'
3
5
 
4
6
  # Setting the line length to a maximum of 80 chars.
5
7
  LineLength:
data/README.md CHANGED
@@ -51,18 +51,6 @@ my_time = Time.new(2014, 5, 7, 16)
51
51
  time_frame.cover?(my_time)
52
52
  # => true
53
53
 
54
- # Deviation to another time?
55
- earlier_time = time_frame.min - 1.day
56
- later_time = time_frame.max + 4.days
57
- time_frame.deviation_of(earlier_time)
58
- # => -86400.0
59
- time_frame.deviation_of(later_time)
60
- # => 345600.0
61
- # No deviation expected here:
62
- time_frame.deviation_of(time_frame.min + 20.minutes)
63
- # => 0
64
- # ... yay!
65
-
66
54
  # Shifting to another time... duration remains:
67
55
  time_frame.shift_to(Time.new(2016, 1, 1))
68
56
  # => 2016-01-01 00:00:00 +0100..2016-01-02 00:00:00 +0100
@@ -22,19 +22,21 @@ class TimeFrame
22
22
  end
23
23
 
24
24
  def all_covering(time)
25
- [].tap do |result|
26
- add_covering(time, @root, result) if any?
27
- end
25
+ all_matching { |element| element.cover? time }
28
26
  end
29
27
 
30
28
  def all_intersecting(time_frame)
31
- [].tap do |result|
32
- add_intersecting(time_frame, @root, result) if any?
33
- end
29
+ all_matching { |element| element.overlaps? time_frame }
34
30
  end
35
31
 
36
32
  private
37
33
 
34
+ def all_matching(&matcher)
35
+ [].tap do |result|
36
+ add_matching(@root, result, &matcher) if any?
37
+ end
38
+ end
39
+
38
40
  def sort_nodes
39
41
  @tree_nodes.sort_by! do |item|
40
42
  [item.time_frame.min, item.time_frame.max]
@@ -59,22 +61,12 @@ class TimeFrame
59
61
  node.update_child_frame(node.child_time_frame) if lower == upper
60
62
  end
61
63
 
62
- def add_covering(time, node, result)
63
- search_left = node.continue_left_side_search_for_time?(time)
64
- search_right = node.continue_right_side_search_for_time?(time)
65
-
66
- add_covering(time, node.left_child, result) if search_left
67
- result << node.item if node.time_frame.cover?(time)
68
- add_covering(time, node.right_child, result) if search_right
69
- end
70
-
71
- def add_intersecting(time_frame, node, result)
72
- search_left = node.continue_left_side_search_for_time_frame?(time_frame)
73
- search_right = node.continue_right_side_search_for_time_frame?(time_frame)
64
+ def add_matching(node, result, &matcher)
65
+ return unless node && matcher.call(node.child_time_frame)
74
66
 
75
- add_intersecting(time_frame, node.left_child, result) if search_left
76
- result << node.item if node.time_frame.overlaps? time_frame
77
- add_intersecting(time_frame, node.right_child, result) if search_right
67
+ add_matching(node.left_child, result, &matcher)
68
+ result << node.item if matcher.call(node.time_frame)
69
+ add_matching(node.right_child, result, &matcher)
78
70
  end
79
71
  end
80
72
  end
@@ -12,7 +12,7 @@ class TimeFrame
12
12
 
13
13
  def initialize(args)
14
14
  min = args.fetch(:min)
15
- max = args.fetch(:max, nil) || min + args.fetch(:duration)
15
+ max = args.fetch(:max) { min + args.fetch(:duration) }
16
16
  check_bounds(max, min)
17
17
  @max = max
18
18
  @min = min
@@ -0,0 +1,16 @@
1
+ class TimeFrame
2
+ # This class tells the active_record predicate builder how to handle
3
+ # time_frame classes when passed into a where-clause
4
+ class Handler
5
+ def call(column, time_frame)
6
+ Arel::Nodes::Between.new(
7
+ column,
8
+ Arel::Nodes::And.new([time_frame.min, time_frame.max])
9
+ )
10
+ end
11
+ end
12
+ end
13
+
14
+ ActiveRecord::PredicateBuilder.register_handler(
15
+ TimeFrame, TimeFrame::Handler.new
16
+ )
@@ -30,26 +30,6 @@ class TimeFrame
30
30
  @child_time_frame = TimeFrame.new(min: min, max: max)
31
31
  ancestor.update_child_frame(@child_time_frame) if ancestor
32
32
  end
33
-
34
- def continue_left_side_search_for_time?(time)
35
- left_child && left_child.child_time_frame.cover?(time)
36
- end
37
-
38
- def continue_left_side_search_for_time_frame?(interval)
39
- left_child &&
40
- left_child.child_time_frame.min <= interval.max &&
41
- left_child.child_time_frame.max >= interval.min
42
- end
43
-
44
- def continue_right_side_search_for_time?(time)
45
- right_child && right_child.child_time_frame.cover?(time)
46
- end
47
-
48
- def continue_right_side_search_for_time_frame?(interval)
49
- right_child &&
50
- right_child.child_time_frame.min <= interval.max &&
51
- right_child.child_time_frame.max >= interval.min
52
- end
53
33
  end
54
34
  end
55
35
  end
@@ -1,5 +1,5 @@
1
1
  # Encoding: utf-8
2
2
  # gem version
3
3
  class TimeFrame
4
- VERSION = '0.4.3'
4
+ VERSION = '0.5.0'
5
5
  end
data/lib/time_frame.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # Encoding: utf-8
2
- require 'active_support'
2
+ require 'active_record'
3
3
  require 'active_support/core_ext'
4
4
  require 'singleton'
5
5
 
@@ -10,6 +10,7 @@ require 'time_frame/time_frame_overlaps'
10
10
  require 'time_frame/time_frame_uniter'
11
11
 
12
12
  require 'time_frame/time_frame'
13
+ require 'time_frame/time_frame_handler'
13
14
 
14
15
  require 'time_frame/tree_node'
15
16
  require 'time_frame/collection'
@@ -0,0 +1,23 @@
1
+ # Vogon poetry is of course, the third worst in the universe.
2
+ class VogonPoem < ActiveRecord::Base
3
+ def text
4
+ <<-EOS
5
+ Oh freddled gruntbuggly,
6
+ Thy micturitions are to me,
7
+ As plurdled gabbleblotchits,
8
+ On a lurgid bee,
9
+ That mordiously hath blurted out,
10
+ Its earted jurtles,
11
+ Into a rancid festering confectious organ squealer. [drowned out by moaning and screaming]
12
+ Now the jurpling slayjid agrocrustles,
13
+ Are slurping hagrilly up the axlegrurts,
14
+ And living glupules frart and slipulate,
15
+ Like jowling meated liverslime,
16
+ Groop, I implore thee, my foonting turling dromes,
17
+ And hooptiously drangle me,
18
+ With crinkly bindlewurdles,
19
+ Or else I shall rend thee in the gobberwarts with my blurglecruncheon,
20
+ See if I don't.
21
+ EOS
22
+ end
23
+ end
data/spec/spec_helper.rb CHANGED
@@ -12,3 +12,16 @@ require 'rspec'
12
12
  RSpec.configure do |config|
13
13
  config.order = 'random'
14
14
  end
15
+
16
+ # active_record setup for active_record handler specs
17
+ ActiveRecord::Base.establish_connection(
18
+ adapter: 'sqlite3', database: ':memory:'
19
+ )
20
+
21
+ ActiveRecord::Schema.define do
22
+ self.verbose = false
23
+
24
+ create_table :vogon_poems, force: true do |t|
25
+ t.datetime :written_at
26
+ end
27
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ require 'models/vogon_poem'
3
+
4
+ describe TimeFrame::Handler do
5
+ let(:time_frame) { TimeFrame.new(min: 5.days.ago, duration: 20.days) }
6
+ let!(:poem_min) { VogonPoem.create(written_at: time_frame.min) }
7
+ let!(:poem_max) { VogonPoem.create(written_at: time_frame.max) }
8
+
9
+ it 'should return all records between min and max' do
10
+ expect(VogonPoem.where(written_at: time_frame).count).to eq 2
11
+ expect(VogonPoem.where(written_at: time_frame).first).to eq poem_min
12
+ expect(VogonPoem.where(written_at: time_frame).last).to eq poem_max
13
+ end
14
+ end
@@ -10,7 +10,7 @@ describe TimeFrame do
10
10
  I18n.enforce_available_locales = true
11
11
  end
12
12
 
13
- it "should be hashable" do
13
+ it 'should be hashable' do
14
14
  hash = {}
15
15
  time_frame1 = TimeFrame.new(min: time, duration: duration)
16
16
  time_frame2 = TimeFrame.new(min: time, duration: duration)
data/time_frame.gemspec CHANGED
@@ -30,5 +30,7 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency 'rspec', '~> 3.0.0'
31
31
  spec.add_development_dependency 'simplecov', '~> 0.8.2'
32
32
  spec.add_development_dependency 'rubocop', '~> 0.23.0'
33
+ spec.add_development_dependency 'sqlite3'
34
+ spec.add_dependency 'activerecord', '~> 4.1.1'
33
35
  spec.add_dependency 'activesupport', '~> 4.1.1'
34
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_frame
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Derichs
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-10-21 00:00:00.000000000 Z
13
+ date: 2014-10-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -68,6 +68,34 @@ dependencies:
68
68
  - - "~>"
69
69
  - !ruby/object:Gem::Version
70
70
  version: 0.23.0
71
+ - !ruby/object:Gem::Dependency
72
+ name: sqlite3
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ - !ruby/object:Gem::Dependency
86
+ name: activerecord
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: 4.1.1
92
+ type: :runtime
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - "~>"
97
+ - !ruby/object:Gem::Version
98
+ version: 4.1.1
71
99
  - !ruby/object:Gem::Dependency
72
100
  name: activesupport
73
101
  requirement: !ruby/object:Gem::Requirement
@@ -103,13 +131,16 @@ files:
103
131
  - lib/time_frame/empty.rb
104
132
  - lib/time_frame/time_frame.rb
105
133
  - lib/time_frame/time_frame_covered.rb
134
+ - lib/time_frame/time_frame_handler.rb
106
135
  - lib/time_frame/time_frame_overlaps.rb
107
136
  - lib/time_frame/time_frame_splitter.rb
108
137
  - lib/time_frame/time_frame_uniter.rb
109
138
  - lib/time_frame/tree_node.rb
110
139
  - lib/time_frame/version.rb
111
140
  - spec/collection_spec.rb
141
+ - spec/models/vogon_poem.rb
112
142
  - spec/spec_helper.rb
143
+ - spec/time_frame_handler_spec.rb
113
144
  - spec/time_frame_spec.rb
114
145
  - time_frame.gemspec
115
146
  homepage: https://github.com/injixo/time_frame
@@ -138,5 +169,7 @@ specification_version: 4
138
169
  summary: Ruby gem that offers support for time frames
139
170
  test_files:
140
171
  - spec/collection_spec.rb
172
+ - spec/models/vogon_poem.rb
141
173
  - spec/spec_helper.rb
174
+ - spec/time_frame_handler_spec.rb
142
175
  - spec/time_frame_spec.rb