Sha256: 62488ebc57bed5f6b320dfda8f9b882e01e2d7c30997114e649f10655e43ec76

Contents?: true

Size: 674 Bytes

Versions: 2

Compression:

Stored size: 674 Bytes

Contents

# frozen_string_literal: true

# Parser others types to value
class Parser
  def initialize(only_hours: true)
    @only_hours = only_hours
  end

  def parse(value)
    @value = value

    case @value
    when String
      from_string.to_f
    when Time
      from_time.to_f
    else
      @value.to_f
    end
  end

  private

  def from_time
    return @value.to_f unless @only_hours

    (@value - @value.beginning_of_day).to_f
  end

  def from_string
    str = @value.to_s.split(":")
    value = 0

    %i[hours minutes seconds].each_with_index do |m, i|
      value += str.at(i).to_i.abs.send(m.to_s)
    end

    str.to_s.include?("-") ? value * -1 : value
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tempus-1.1.1 lib/tempus/parser.rb
tempus-1.1.0 lib/tempus/parser.rb