Class: Duration

Inherits:
Object
  • Object
show all
Includes:
Comparable, Arithmetic
Defined in:
lib/duration.rb,
lib/duration/version.rb,
lib/duration/arithmetic.rb

Defined Under Namespace

Modules: Arithmetic

Constant Summary collapse

UNITS =
['s', 'm', 'h', 'd']
DURATIONS =
{
  'seconds' => 's',
  'minutes' => 'm',
  'hours'   => 'h',
  'days'    => 'd',
  'sec'     => 's',
  'hrs'     => 'h',
  'mins'    => 'm'
}
STANDARD_CONVERSION =
{
  's' => 1,
  'm' => 60,
  'h' => 3600,
  'd' => 86_400
}
VERSION =
"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Arithmetic

#+, #-

Constructor Details

#initialize(input) ⇒ Duration

Returns a new instance of Duration.



10
11
12
# File 'lib/duration.rb', line 10

def initialize input
  self.raw = input.to_s
end

Instance Attribute Details

#rawObject

Returns the value of attribute raw.



5
6
7
# File 'lib/duration.rb', line 5

def raw
  @raw
end

Instance Method Details

#<=>(other_thing) ⇒ Object

Comparable Protocol:



89
90
91
92
# File 'lib/duration.rb', line 89

def <=> other_thing
  other_thing = other_thing.to_duration if other_thing.respond_to?(:to_duration)
  self.to_i <=> other_thing.to_i
end

#condense_duration_tokens(input = raw.dup) ⇒ Object



26
27
28
# File 'lib/duration.rb', line 26

def condense_duration_tokens input = raw.dup
  input.gsub(/(#{DURATIONS.keys.join("|")})/, DURATIONS)
end

#convert_to_normal_form(input = raw.dup) ⇒ Object



38
39
40
41
42
# File 'lib/duration.rb', line 38

def convert_to_normal_form input = raw.dup
  return input unless input =~ /:/ || input !~ /[[:alpha:]]/
  split = input.split(":").reverse
  split.zip(UNITS).reverse.map { |unit| unit.join("") }.join(" ")
end

#count(input = normalize_input) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/duration.rb', line 44

def count input = normalize_input
  input.split(" ").inject(0) do |accum, token|
    _, amount, type = token.split(/([[:digit:]]+)(?=[[:alpha:]])/)
    accum += STANDARD_CONVERSION[type] * amount.to_i
    accum
  end
end

#intelligently_space(input = raw.dup) ⇒ Object



34
35
36
# File 'lib/duration.rb', line 34

def intelligently_space input = raw.dup
  input.gsub(/\s/, "").gsub(/([[:alpha:]])(?=[[:digit:]])/, '\1 ')
end

#normalize_input(input = raw.dup) ⇒ Object



19
20
21
22
23
24
# File 'lib/duration.rb', line 19

def normalize_input input = raw.dup
  input = condense_duration_tokens input # => '92 m and 7 s'
  input = strip_useless_words input # => '92 m 7 s'
  input = intelligently_space input # => '92m 7s'
  convert_to_normal_form input # '92:07' => '92m 7s'
end

#parseObject Also known as: to_i



14
15
16
# File 'lib/duration.rb', line 14

def parse
  @parsed ||= count normalize_input
end

#strip_useless_words(input = raw.dup) ⇒ Object



30
31
32
# File 'lib/duration.rb', line 30

def strip_useless_words input = raw.dup
  input.gsub(/[[:alpha:]]{2,}/, "").gsub(/[^\ddhms:]/, " ").squeeze(" ")
end

#to_clock_formatObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/duration.rb', line 52

def to_clock_format
  to_s proc { |s|
    a = STANDARD_CONVERSION.values.dup
    [(s / a[3]), (s % a[3] / a[2]), (s % a[2] / a[1]), (s % a[1])].
      each_with_index.
      reject { |value, index| index < 2 && value == 0 }.
      map { |value| "%02d" % value }.
      join(":")
  }
end

#to_s(formatter = nil) ⇒ Object



63
64
65
# File 'lib/duration.rb', line 63

def to_s formatter = nil
  formatter ? formatter.call(parse) : "#<Duration:#{self.object_id} @raw=#{raw.inspect} @clock=#{to_clock_format.inspect}>"
end