Skip to content

Commit cd3d0dc

Browse files
committed
Some moar docs
1 parent 650ac13 commit cd3d0dc

File tree

6 files changed

+138
-1
lines changed

6 files changed

+138
-1
lines changed

.yardopts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--markup=markdown
2+
--no-private

lib/time_calc.rb

+30
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,36 @@ def ==(other)
196196
# @return [Diff]
197197
# @return [Time or Diff]
198198

199+
# @!method to(date_or_time)
200+
# Produces {Sequence} from this value to `date_or_time`
201+
#
202+
# @param date_or_time [Date, Time, DateTime]
203+
# @return [Sequence]
204+
205+
# @!method step(span, unit = nil)
206+
# Produces endless {Sequence} from this value, with step specified.
207+
#
208+
# @overload step(unit)
209+
# Shortcut for `step(1, unit)`
210+
# @param unit [Symbol]
211+
# @overload step(span, unit)
212+
# @example
213+
# TimeCalc.(Time.parse('2019-06-01 14:50')).step(1, :day).take(3)
214+
# # => [2019-06-01 14:50:00 +0300, 2019-06-02 14:50:00 +0300, 2019-06-03 14:50:00 +0300]
215+
# @param span [Integer]
216+
# @param unit [Symbol]
217+
# @return [Sequence]
218+
219+
# @!method for(span, unit)
220+
# Produces {Sequence} from this value to `this + <span units>`
221+
#
222+
# @example
223+
# TimeCalc.(Time.parse('2019-06-01 14:50')).for(2, :weeks).step(1, :day).count
224+
# # => 15
225+
# @param span [Integer]
226+
# @param unit [Symbol]
227+
# @return [Sequence]
228+
199229
# @private
200230
MATH_OPERATIONS = %i[merge truncate floor ceil round + -].freeze
201231
# @private

lib/time_calc/diff.rb

+40
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ class TimeCalc
1212
# Allows to easily and correctly calculate number of years/monthes/days/etc between two points in
1313
# time.
1414
#
15+
# @example
16+
# t1 = Time.parse('2019-06-01 14:50')
17+
# t2 = Time.parse('2019-06-15 12:10')
18+
# (TimeCalc.(t2) - t1).div(:day)
19+
# # => 13
20+
# # the same:
21+
# (TimeCalc.(t2) - t1).days
22+
# # => 13
23+
# (TimeCalc.(t2) - t1).div(3, :hours)
24+
# # => 111
25+
#
26+
# (TimeCalc.(t2) - t1).factorize
27+
# # => {:year=>0, :month=>0, :week=>1, :day=>6, :hour=>21, :min=>20, :sec=>0}
28+
# (TimeCalc.(t2) - t1).factorize(weeks: false)
29+
# # => {:year=>0, :month=>0, :day=>13, :hour=>21, :min=>20, :sec=>0}
30+
# (TimeCalc.(t2) - t1).factorize(weeks: false, zeroes: false)
31+
# # => {:day=>13, :hour=>21, :min=>20, :sec=>0}
32+
#
1533
class Diff
1634
# @private
1735
attr_reader :from, :to
@@ -80,6 +98,28 @@ def div(span, unit = nil)
8098
singular_div(unit).div(span)
8199
end
82100

101+
# @!method years
102+
# Whole years in diff.
103+
# @return [Integer]
104+
# @!method months
105+
# Whole months in diff.
106+
# @return [Integer]
107+
# @!method weeks
108+
# Whole weeks in diff.
109+
# @return [Integer]
110+
# @!method days
111+
# Whole days in diff.
112+
# @return [Integer]
113+
# @!method hours
114+
# Whole hours in diff.
115+
# @return [Integer]
116+
# @!method minutes
117+
# Whole minutes in diff.
118+
# @return [Integer]
119+
# @!method seconds
120+
# Whole seconds in diff.
121+
# @return [Integer]
122+
83123
# Same as integer modulo: the "rest" of whole division of the distance between two time points by
84124
# `<span> <units>`. This rest will be also time point, equal to `first diff operand - span units`
85125
#

lib/time_calc/op.rb

+22-1
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,31 @@ def inspect
2525
'<%s %s>' % [self.class, @chain.map { |name, *args| "#{name}(#{args.join(' ')})" }.join('.')]
2626
end
2727

28-
TimeCalc::OPERATIONS.each do |name|
28+
TimeCalc::MATH_OPERATIONS.each do |name|
2929
define_method(name) { |*args| Op.new([*@chain, [name, *args]]) }
3030
end
3131

32+
# @!method +(span, unit)
33+
# Adds `+(span, unit)` to method chain
34+
# @see TimeCalc#+
35+
# @return [Op]
36+
# @!method -(span, unit)
37+
# Adds `-(span, unit)` to method chain
38+
# @see TimeCalc#-
39+
# @return [Op]
40+
# @!method floor(unit)
41+
# Adds `floor(span, unit)` to method chain
42+
# @see TimeCalc#floor
43+
# @return [Op]
44+
# @!method ceil(unit)
45+
# Adds `ceil(span, unit)` to method chain
46+
# @see TimeCalc#ceil
47+
# @return [Op]
48+
# @!method round(unit)
49+
# Adds `round(span, unit)` to method chain
50+
# @see TimeCalc#round
51+
# @return [Op]
52+
3253
# Performs the whole chain of operation on parameter, returning the result.
3354
#
3455
# @param date_or_time [Date, Time, DateTime]

lib/time_calc/version.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
class TimeCalc
4+
# @private
5+
VERSION = '0.1.1'
6+
end

time_calc.gemspec

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require './lib/time_calc/version'
2+
3+
Gem::Specification.new do |s|
4+
s.name = 'time_calc'
5+
s.version = TimeCalc::VERSION
6+
s.authors = ['Victor Shepelev']
7+
s.email = '[email protected]'
8+
s.homepage = 'https://github.com/zverok/time_calc'
9+
10+
s.summary = 'Easy time math'
11+
s.description = <<-EOF
12+
TimeCalc is a library for idiomatic time calculations, like "plus N days", "floor to month start",
13+
"how many hours between those dates", "sequence of months from this to that". It intends to
14+
be small and easy to remember without any patching of core classes.
15+
EOF
16+
s.licenses = ['MIT']
17+
18+
s.required_ruby_version = '>= 2.3.0'
19+
20+
s.files = `git ls-files lib LICENSE.txt *.md`.split($RS)
21+
s.require_paths = ["lib"]
22+
23+
s.add_runtime_dependency 'backports', '>= 3.15.0'
24+
25+
s.add_development_dependency 'rubocop', '~> 0.72.0'
26+
s.add_development_dependency 'rubocop-rspec', '>= 1.17.1'
27+
28+
s.add_development_dependency 'rspec', '>= 3.8'
29+
s.add_development_dependency 'rspec-its', '~> 1'
30+
s.add_development_dependency 'saharspec'
31+
s.add_development_dependency 'simplecov', '~> 0.9'
32+
s.add_development_dependency 'tzinfo'
33+
34+
s.add_development_dependency 'rake'
35+
s.add_development_dependency 'rubygems-tasks'
36+
37+
s.add_development_dependency 'yard'
38+
end

0 commit comments

Comments
 (0)