availability 0.0.4 → 1.0.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: 3be1734eee2d550422776a8d399236e4e9fdd2ca
4
- data.tar.gz: 4c98f924c2a0a12daafb846886a8be06a4a85358
3
+ metadata.gz: 5694685226c41dca7879ede7f7465d55bef2e355
4
+ data.tar.gz: bcd53b5d00090dfc2460500343fc938a6bb951f6
5
5
  SHA512:
6
- metadata.gz: 0d334f811b0745a5774ca32a5e0357694abf6b72973d67c59668b93c06b44bd8b640ae5dedeae529d164265273bb4307dc94418a59d405cd6b2cd523a18fbf13
7
- data.tar.gz: 71ff6a5d558627ce59c4bfafdb6e81102ea60c7e8dda203ed30b9d52513559670b2f224dc7163202ffbc604b65cac33b75a9a3e1ecdcd58cf9dbd5bad5159629
6
+ metadata.gz: 61392ce35039f61bb39d9d71c0801586e10a572c68befaf3f8ea000cfb8e6d538205a3f18049e4b21db28fc3ca158d768870ee92906e35b1d5066e31bc38afaa
7
+ data.tar.gz: 440aeb3e3f13046b00855e5e3a917ea07d2e6478a941ad87a7e2201279d91610ea3e69e91aacc32374c84e0f7adaa8962b8211845831916abecb83fc0034487c
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![Gem Version](https://badge.fury.io/rb/availability.svg)](https://badge.fury.io/rb/availability)
6
6
  [read the docs][DOCS]
7
7
 
8
- This library uses modular arithmetic and residue classes to calculate schedule availability for dates. Time ranges within a date are handled differently. The goal is to create an easy-to-use API for schedule availability that is very fast and lightweight that is also easy and lightweight to persist in a database.
8
+ This library uses modular arithmetic and residue classes to calculate schedule availability for dates. The goal is to create an easy-to-use API for schedule availability that is very fast and lightweight that is also easy and lightweight to persist in a database.
9
9
 
10
10
  Shout out to @dpmccabe for his [original article](http://dmcca.be/2014/01/09/recurring-subscriptions-with-ruby-rspec-and-modular-arithmetic.html) and code.
11
11
 
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["jacaetevha@gmail.com"]
11
11
 
12
12
  spec.summary = %q{Calculating schedule availability}
13
- spec.description = %q{Use modular arithmetic and residue classes to calculate schedule availability for dates (times handled separately).}
13
+ spec.description = %q{Use modular arithmetic and residue classes to calculate schedule availability for dates and times.}
14
14
  spec.homepage = "https://github.com/upper-hand/availability"
15
15
  spec.license = "Unlicense"
16
16
  spec.required_ruby_version = '>= 2.0'
@@ -8,7 +8,7 @@ class Scheduler
8
8
  attr_reader :availabilities, :scheduled
9
9
 
10
10
  #
11
- # availabilities: a list of Availability instances defining when the resource is available
11
+ # availabilities: a list of Availability::AbstractAvailability instances defining when the resource is available
12
12
  #
13
13
  def initialize(availabilities)
14
14
  @availabilities = validate_availabilities availabilities
@@ -1,6 +1,7 @@
1
1
  require 'active_support'
2
2
  require 'active_support/core_ext'
3
- require_relative "availability/version"
3
+ require_relative 'availability/version'
4
+ require_relative 'availability/instance_variable_comparability'
4
5
  require_relative 'availability/createable'
5
6
  require_relative 'availability/exclusion'
6
7
  require_relative 'availability/abstract_availability'
@@ -1,6 +1,6 @@
1
1
  module Availability
2
2
  # @abstract see concrete classes: Once, Daily, Weekly, Monthly and Yearly
3
- class AbstractAvailability
3
+ class AbstractAvailability < InstanceVariableComparability
4
4
  private_class_method :new # :nodoc:
5
5
 
6
6
  attr_accessor :capacity, :duration, :stops_by
@@ -201,6 +201,12 @@ module Availability
201
201
 
202
202
  # @!endgroup
203
203
 
204
+ protected
205
+
206
+ def comparability_ivar_names
207
+ %w{@capacity @exclusions @stops_by @duration @interval @start_time}
208
+ end
209
+
204
210
  private
205
211
 
206
212
  def compute_residue
@@ -1,5 +1,5 @@
1
1
  module Availability
2
- class Exclusion
2
+ class Exclusion < InstanceVariableComparability
3
3
  attr_reader :rule
4
4
 
5
5
  def self.after_day(date)
@@ -48,13 +48,18 @@ module Availability
48
48
  @rule = rule
49
49
  end
50
50
 
51
+ def <=>(other)
52
+ return 1 if other.nil?
53
+ rule <=> other.rule
54
+ end
55
+
51
56
  def violated_by?(time)
52
57
  @rule.violated_by? time
53
58
  end
54
59
 
55
60
  private
56
61
  module Rule
57
- class AfterDate
62
+ class AfterDate < InstanceVariableComparability
58
63
  def initialize(date)
59
64
  @date = date
60
65
  end
@@ -64,7 +69,7 @@ module Availability
64
69
  end
65
70
  end
66
71
 
67
- class AfterDateAndTime
72
+ class AfterDateAndTime < InstanceVariableComparability
68
73
  def initialize(time)
69
74
  @after_date = AfterDate.new time.to_date
70
75
  @after_time = AfterTime.new time
@@ -75,7 +80,7 @@ module Availability
75
80
  end
76
81
  end
77
82
 
78
- class AfterTime
83
+ class AfterTime < InstanceVariableComparability
79
84
  def initialize(date_or_time)
80
85
  @compare_to = date_or_time.to_time
81
86
  end
@@ -85,7 +90,7 @@ module Availability
85
90
  end
86
91
  end
87
92
 
88
- class BeforeDate
93
+ class BeforeDate < InstanceVariableComparability
89
94
  def initialize(date)
90
95
  @date = date
91
96
  end
@@ -95,7 +100,7 @@ module Availability
95
100
  end
96
101
  end
97
102
 
98
- class BeforeDateAndTime
103
+ class BeforeDateAndTime < InstanceVariableComparability
99
104
  def initialize(time)
100
105
  @before_date = BeforeDate.new time.to_date
101
106
  @before_time = BeforeTime.new time
@@ -106,7 +111,7 @@ module Availability
106
111
  end
107
112
  end
108
113
 
109
- class BeforeTime
114
+ class BeforeTime < InstanceVariableComparability
110
115
  def initialize(date_or_time)
111
116
  @compare_to = date_or_time.to_time
112
117
  end
@@ -116,7 +121,7 @@ module Availability
116
121
  end
117
122
  end
118
123
 
119
- class OnDate
124
+ class OnDate < InstanceVariableComparability
120
125
  def initialize(date)
121
126
  @date = date
122
127
  end
@@ -126,7 +131,7 @@ module Availability
126
131
  end
127
132
  end
128
133
 
129
- class OnDayOfWeek
134
+ class OnDayOfWeek < InstanceVariableComparability
130
135
  def initialize(day_of_week)
131
136
  @day_of_week = day_of_week
132
137
  end
@@ -0,0 +1,16 @@
1
+ class InstanceVariableComparability
2
+ include Comparable
3
+ def <=>(other)
4
+ return 1 if other.nil?
5
+ comparisons = comparability_ivar_names.map do |ivar|
6
+ instance_variable_get(ivar) <=> other.instance_variable_get(ivar)
7
+ end.reject { |c| c == 0 }
8
+ return 0 if comparisons.empty?
9
+ comparisons.inject(0) { |acc, each| acc + each } >= 1 ? 1 : -1
10
+ end
11
+
12
+ protected
13
+ def comparability_ivar_names
14
+ instance_variables
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module Availability
2
- VERSION = "0.0.4"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: availability
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Rogers
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-09 00:00:00.000000000 Z
11
+ date: 2016-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -109,7 +109,7 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  description: Use modular arithmetic and residue classes to calculate schedule availability
112
- for dates (times handled separately).
112
+ for dates and times.
113
113
  email:
114
114
  - jacaetevha@gmail.com
115
115
  executables: []
@@ -142,6 +142,7 @@ files:
142
142
  - lib/availability/exclusion.rb
143
143
  - lib/availability/factory_methods.rb
144
144
  - lib/availability/hourly.rb
145
+ - lib/availability/instance_variable_comparability.rb
145
146
  - lib/availability/monthly.rb
146
147
  - lib/availability/once.rb
147
148
  - lib/availability/version.rb