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.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/time_index.rb', line 25

def initialize time_index = ''
  if is_integer?(time_index) || time_index.instance_of?(Float)
    @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



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

def * operand
  TimeIndex.new total_seconds * operand
end

#+(operand) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/time_index.rb', line 83

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

#-(operand) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/time_index.rb', line 91

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

#/(operand) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/time_index.rb', line 103

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

#<=>(operand) ⇒ Object



111
112
113
# File 'lib/time_index.rb', line 111

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

#add_vcs_part(part) ⇒ Object



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

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

#has_unified_integer?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
# File 'lib/time_index.rb', line 10

def has_unified_integer?
  version = RUBY_VERSION.split('.').collect(&:to_i)

  return (version[0] == 2 && version[1] >= 4) || version[0] > 2
end

#hoursObject



71
72
73
# File 'lib/time_index.rb', line 71

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

#is_integer?(variable) ⇒ Boolean

compatibilty fix

Returns:

  • (Boolean)


17
18
19
20
21
22
23
# File 'lib/time_index.rb', line 17

def is_integer? variable
  if has_unified_integer?
    return variable.instance_of?(Integer)
  else
    return variable.instance_of?(Fixnum) || variable.instance_of?(Bignum)
  end
end

#minutesObject



75
76
77
# File 'lib/time_index.rb', line 75

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

#secondsObject



79
80
81
# File 'lib/time_index.rb', line 79

def seconds
  @total_seconds.abs % 60
end

#signObject



115
116
117
118
# File 'lib/time_index.rb', line 115

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

#to_sObject



120
121
122
# File 'lib/time_index.rb', line 120

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

#to_timestampObject



124
125
126
127
128
129
130
# File 'lib/time_index.rb', line 124

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

#try_parse_as_numberObject



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

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



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

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



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

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