Module: EasyTag::Utilities

Defined in:
lib/easytag/util.rb

Class Method Summary collapse

Class Method Details

.get_datetime(date_str) ⇒ Object

get_datetime

return a DateTime object for a given string



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/easytag/util.rb', line 9

def self.get_datetime(date_str)
  return nil if date_str.nil?

  # check for known possible formats
  case date_str
  when /^\d{4}$/ # YYYY
    datetime = DateTime.strptime(date_str, '%Y')
  when /^\d{4}\-\d{2}$/ # YYYY-MM
    datetime = DateTime.strptime(date_str, '%Y-%m')
  when /^\d{4}[0-3]\d[0-1]\d$/ # YYYYDDMM (TYER+TDAT)
    datetime = DateTime.strptime(date_str, '%Y%d%m')
  else
    datetime = nil
  end

  # let DateTime try to parse the stored date as a last resort
  if datetime.nil?
    begin
      datetime = DateTime.parse(date_str)
    rescue ArgumentError
      warn "DateTime couldn't parse '#{date_str}'"
    end
  end

  datetime
end

.get_int_pair(str) ⇒ Object

get_int_pair

Parses a pos/total string and returns a pair of ints



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/easytag/util.rb', line 39

def self.get_int_pair(str)
  pair = [0, 0]

  unless str.nil? || str.empty?
    if str.include?('/')
      pair = str.split('/').map { |it| it.to_i }
    else
      pair[0] = str.to_i
    end
  end

  pair
end

.normalize_string(str) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/easytag/util.rb', line 53

def self.normalize_string(str)
  # downcase string
  str.downcase!
  # we want snakecase
  str.gsub!(/\s/, '_')
  # we only want alphanumeric characters
  str.gsub(/\W/, '')
end