clavius 0.0.1 → 0.1.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: 1d984a93f31c3341e8da6d9621844d64ed93489a
4
- data.tar.gz: 445473b481a49888889c4187d3233a537471f265
3
+ metadata.gz: 8144307e042b9d7f78554129aec5a0e669867c98
4
+ data.tar.gz: 6824499bee409cf9ff9ffdcaa7548ccc577b16c2
5
5
  SHA512:
6
- metadata.gz: 4a66eff0003d01f1a891fd5976ca205ce8565a20bccc5d4f8ceb95b4364830684e013a06e4c47abef94f9034fbfa403eb4f8ba728b6b439a8db238248cfa4610
7
- data.tar.gz: c8be97c17611b197fe1eec31f77dbe9a3b92a676cceb3429d30af4048458bdf2c139a374e045759473cd356103cb9c54b9702b84576751e30fda31cfe9715bd3
6
+ metadata.gz: 47831e8296e50923b5e6c9d13b48dbb04dc16feaf5c36fa85ea34ba16425fb7700e902f0ee26806ae2228d2a5b6042aa4e93c93fa2346b9eca63855661e79a26
7
+ data.tar.gz: 6fb5065a976b6b1e87077e58bdc869c46d1bb7f21d699ab52307b2b141f4f93fc44a4d198d0ceeb3e5831cc5d921eef2c8a23f75c61e2a52b205c4c1326b49d6
data/README.md CHANGED
@@ -1,7 +1,13 @@
1
1
  # Clavius
2
2
 
3
+ [![Build Status](https://travis-ci.org/craiglittle/clavius.svg?branch=master)](https://travis-ci.org/craiglittle/clavius)
4
+ [![Code Climate](https://codeclimate.com/github/craiglittle/clavius/badges/gpa.svg)](https://codeclimate.com/github/craiglittle/clavius)
5
+ [![Test Coverage](https://codeclimate.com/github/craiglittle/clavius/badges/coverage.svg)](https://codeclimate.com/github/craiglittle/clavius)
6
+ [![Dependency Status](https://gemnasium.com/craiglittle/clavius.svg)](https://gemnasium.com/craiglittle/clavius)
7
+
3
8
  Date calculations based on a schedule.
4
9
 
10
+
5
11
  ## Installation
6
12
 
7
13
  ```
@@ -23,8 +29,6 @@ end
23
29
  ```ruby
24
30
  Clavius.active?(Date.new(2014, 1, 5))
25
31
 
26
- Clavius.inactive?(Date.new(2014, 5, 2))
27
-
28
32
  Clavius.days(5).before(Date.new(2015, 1, 5))
29
33
 
30
34
  Clavius.days(2).after(Date.new(2015, 1, 5))
@@ -1,4 +1,13 @@
1
+ require 'set'
2
+ require 'forwardable'
3
+
4
+ require 'memoizable'
5
+
1
6
  module Clavius
2
7
  end
3
8
 
9
+ require 'clavius/calculation'
10
+ require 'clavius/configuration'
11
+ require 'clavius/schedule'
12
+ require 'clavius/time'
4
13
  require 'clavius/version'
@@ -0,0 +1,6 @@
1
+ module Clavius
2
+ module Calculation
3
+ end
4
+ end
5
+
6
+ require 'clavius/calculation/days_from'
@@ -0,0 +1,25 @@
1
+ module Clavius
2
+ module Calculation
3
+ class DaysFrom
4
+
5
+ def initialize(schedule, number_of_days)
6
+ @schedule = schedule
7
+ @number_of_days = Integer(number_of_days)
8
+ end
9
+
10
+ def before(date)
11
+ schedule.before(date).take(number_of_days).to_a.last
12
+ end
13
+
14
+ def after(date)
15
+ schedule.after(date).take(number_of_days).to_a.last
16
+ end
17
+
18
+ protected
19
+
20
+ attr_reader :schedule,
21
+ :number_of_days
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,58 @@
1
+ module Clavius
2
+ class Configuration
3
+
4
+ include Memoizable
5
+
6
+ def initialize
7
+ @raw = Raw.new.tap do |raw| yield raw end
8
+ end
9
+
10
+ def weekdays
11
+ raw.weekdays
12
+ .select { |weekday| Time::WEEKDAYS.include?(weekday) }
13
+ .map { |weekday| Time::WEEKDAYS.index(weekday) }
14
+ .to_set
15
+ end
16
+
17
+ def included
18
+ raw.included
19
+ .select { |date| date.respond_to?(:to_date) }
20
+ .map(&:to_date)
21
+ .to_set
22
+ end
23
+
24
+ def excluded
25
+ raw.excluded
26
+ .select { |date| date.respond_to?(:to_date) }
27
+ .map(&:to_date)
28
+ .to_set
29
+ end
30
+
31
+ protected
32
+
33
+ attr_reader :raw
34
+
35
+ memoize :weekdays,
36
+ :included,
37
+ :excluded
38
+
39
+ Raw = Struct.new(:weekdays, :included, :excluded) do
40
+ module Default
41
+
42
+ WEEKDAYS = Set.new(%i[mon tue wed thu fri])
43
+ INCLUDED = Set.new
44
+ EXCLUDED = Set.new
45
+
46
+ end
47
+
48
+ def initialize(*)
49
+ super
50
+
51
+ self.weekdays ||= Default::WEEKDAYS
52
+ self.included ||= Default::INCLUDED
53
+ self.excluded ||= Default::EXCLUDED
54
+ end
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,47 @@
1
+ module Clavius
2
+ class Schedule
3
+
4
+ extend Forwardable
5
+
6
+ def initialize(&config)
7
+ @configuration = Configuration.new(&config)
8
+ end
9
+
10
+ delegate %i[
11
+ weekdays
12
+ included
13
+ excluded
14
+ ] => :configuration
15
+
16
+ def before(date)
17
+ date.to_date.prev_day.downto(Time::BIG_BANG).lazy
18
+ .select(&method(:active?))
19
+ end
20
+
21
+ def after(date)
22
+ date.to_date.next_day.upto(Time::HEAT_DEATH).lazy
23
+ .select(&method(:active?))
24
+ end
25
+
26
+ def active?(date)
27
+ date = date.to_date
28
+
29
+ (weekdays.include?(date.wday) || included.include?(date)) &&
30
+ !excluded.include?(date)
31
+ end
32
+
33
+ def days(number)
34
+ Calculation::DaysFrom.new(self, number)
35
+ end
36
+
37
+ def between(start_date, end_date)
38
+ start_date.to_date.upto(end_date.to_date.prev_day)
39
+ .select(&method(:active?))
40
+ end
41
+
42
+ protected
43
+
44
+ attr_reader :configuration
45
+
46
+ end
47
+ end
@@ -0,0 +1,10 @@
1
+ module Clavius
2
+ module Time
3
+
4
+ WEEKDAYS = %i[sun mon tue wed thu fri sat]
5
+
6
+ BIG_BANG = Date.new(-10**100)
7
+ HEAT_DEATH = Date.new(10**100)
8
+
9
+ end
10
+ end
@@ -1,5 +1,5 @@
1
1
  module Clavius
2
2
 
3
- VERSION = '0.0.1'
3
+ VERSION = '0.1.0'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clavius
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig Little
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-01 00:00:00.000000000 Z
11
+ date: 2015-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: memoizable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.4.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.4.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rake
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -47,6 +61,11 @@ extra_rdoc_files: []
47
61
  files:
48
62
  - README.md
49
63
  - lib/clavius.rb
64
+ - lib/clavius/calculation.rb
65
+ - lib/clavius/calculation/days_from.rb
66
+ - lib/clavius/configuration.rb
67
+ - lib/clavius/schedule.rb
68
+ - lib/clavius/time.rb
50
69
  - lib/clavius/version.rb
51
70
  homepage: https://github.com/craiglittle/clavius
52
71
  licenses:
@@ -73,4 +92,3 @@ signing_key:
73
92
  specification_version: 4
74
93
  summary: Date calculations
75
94
  test_files: []
76
- has_rdoc: