Sha256: c5f53f2a456f7eaced8afaa5806e5eef28ea5d647cf3e791074cff2012fe057e

Contents?: true

Size: 686 Bytes

Versions: 1

Compression:

Stored size: 686 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

    if @value.is_a?(String)
      from_string.to_f
    elsif @value.is_a?(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

1 entries across 1 versions & 1 rubygems

Version Path
tempus-1.0.2 lib/tempus/parser.rb