Sha256: 884eab1e6e5393b661a41a576626af65c0788c475e00f22a3d54a39484fd32ad

Contents?: true

Size: 790 Bytes

Versions: 14

Compression:

Stored size: 790 Bytes

Contents

require "chronic_duration"

module Eye::Patch

  class ValueParser

    TIME_MATCHER = /\s(?<duration>(?:week|day|hour|minute|second)s?)(?:\s|\Z)/.freeze
    SIZE_MATCHER = /\s(?<size>(?:tera|giga|mega|kilo)?bytes?)(?:\s|\Z)/.freeze
    MATCHERS     = {
      time: TIME_MATCHER,
      size: SIZE_MATCHER }.freeze

    def self.parse(value)
      return value unless value.is_a?(String)

      result = MATCHERS.detect do |match_type, matcher|
        break send(:"parse_#{match_type}", value) if value.match(matcher)
      end

      result || value
    end

    private

    def self.parse_time(value)
      ChronicDuration.parse(value)
    end

    def self.parse_size(value)
      unit = value.match(SIZE_MATCHER)[:size]
      value.gsub(/[^\d.]/, "").to_f.send(unit)
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
eye-patch-0.5.1 lib/eye/patch/value_parser.rb
eye-patch-0.5.0 lib/eye/patch/value_parser.rb
eye-patch-0.4.2 lib/eye/patch/value_parser.rb
eye-patch-0.4.1 lib/eye/patch/value_parser.rb
eye-patch-0.4.0 lib/eye/patch/value_parser.rb
eye-patch-0.3.1 lib/eye/patch/value_parser.rb
eye-patch-0.3.0 lib/eye/patch/value_parser.rb
eye-patch-0.2.2 lib/eye/patch/value_parser.rb
eye-patch-0.2.1 lib/eye/patch/value_parser.rb
eye-patch-0.2.0 lib/eye/patch/value_parser.rb
eye-patch-0.1.8 lib/eye/patch/value_parser.rb
eye-patch-0.1.7 lib/eye/patch/value_parser.rb
eye-patch-0.1.6 lib/eye/patch/value_parser.rb
eye-patch-0.1.5 lib/eye/patch/value_parser.rb