Class: VCSRuby::TimeIndex

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/time_index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time_index = '') ⇒ TimeIndex

Returns a new instance of TimeIndex.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/time_index.rb', line 10

def initialize time_index = ''
  if time_index.instance_of? Float or time_index.instance_of? Fixnum
    @total_seconds = time_index
  else
    @total_seconds = 0.0
    @to_parse = time_index.strip

    unless @to_parse.empty?
      try_parse_ffmpeg_index
      try_parse_vcs_index
      try_parse_as_number
    end
  end
end

Instance Attribute Details

#total_secondsObject (readonly)

Returns the value of attribute total_seconds.



8
9
10
# File 'lib/time_index.rb', line 8

def total_seconds
  @total_seconds
end

Instance Method Details

#*(operand) ⇒ Object



84
85
86
# File 'lib/time_index.rb', line 84

def * operand
  TimeIndex.new total_seconds * operand
end

#+(operand) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/time_index.rb', line 68

def + operand
  if operand.instance_of? Fixnum
    TimeIndex.new @total_seconds + operand
  else
    TimeIndex.new @total_seconds + operand.total_seconds
  end
end

#-(operand) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/time_index.rb', line 76

def - operand
  if operand.instance_of? Fixnum
    TimeIndex.new @total_seconds - operand
  else
    TimeIndex.new @total_seconds - operand.total_seconds
  end
end

#/(operand) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/time_index.rb', line 88

def / operand
  if operand.instance_of? Fixnum
    TimeIndex.new @total_seconds / operand
  else
    @total_seconds / operand.total_seconds
  end
end

#<=>(operand) ⇒ Object



96
97
98
# File 'lib/time_index.rb', line 96

def <=> operand
  @total_seconds <=> operand.total_seconds
end

#add_vcs_part(part) ⇒ Object



43
44
45
46
47
# File 'lib/time_index.rb', line 43

def add_vcs_part part
  return @total_seconds += part.to_i * 60 * 60 if part.end_with? 'h'
  return @total_seconds += part.to_i * 60 if part.end_with? 'm'
  @total_seconds += part.to_i
end

#hoursObject



56
57
58
# File 'lib/time_index.rb', line 56

def hours
  (@total_seconds.abs / 3600).to_i
end

#minutesObject



60
61
62
# File 'lib/time_index.rb', line 60

def minutes
  ((@total_seconds.abs / 60) % 60).to_i
end

#secondsObject



64
65
66
# File 'lib/time_index.rb', line 64

def seconds
  @total_seconds.abs % 60
end

#signObject



100
101
102
103
# File 'lib/time_index.rb', line 100

def sign
  return '-' if @total_seconds < 0
  ''
end

#to_sObject



105
106
107
# File 'lib/time_index.rb', line 105

def to_s
  "#{sign}#{hours}h#{"%02d" % minutes}m#{"%02d" % seconds}s"
end

#to_timestampObject



109
110
111
112
113
114
115
# File 'lib/time_index.rb', line 109

def to_timestamp
  if hours == 0
    "#{sign}#{"%02d" % minutes}:#{"%02d" % seconds}"
  else
    "#{sign}#{hours}:#{"%02d" % minutes}:#{"%02d" % seconds}"
  end
end

#try_parse_as_numberObject



49
50
51
52
53
54
# File 'lib/time_index.rb', line 49

def try_parse_as_number
  temp = @to_parse.to_f
  if temp.to_s == @to_parse
    @total_seconds += temp
  end
end

#try_parse_ffmpeg_indexObject



25
26
27
28
29
30
31
32
# File 'lib/time_index.rb', line 25

def try_parse_ffmpeg_index
  parts = @to_parse.split(':')
  if parts.count == 3
    @total_seconds += parts[0].to_i * 60 * 60
    @total_seconds += parts[1].to_i * 60
    @total_seconds += parts[2].to_f
  end
end

#try_parse_vcs_indexObject



34
35
36
37
38
39
40
41
# File 'lib/time_index.rb', line 34

def try_parse_vcs_index
  if @to_parse =~ /\d*m|\d*h|\d*s/
    parts = @to_parse.split(/(\d*h)|(\d*m)|(\d*s)/).select{|e| !e.empty?}
    parts.each do |part|
      add_vcs_part part
    end
  end
end