Sha256: 3216e528e4c1fd9eb05af5b311302593c5c003c15f784900bb66ebe512807d0f

Contents?: true

Size: 731 Bytes

Versions: 9

Compression:

Stored size: 731 Bytes

Contents

require "chronic_duration"

module Eye::Patch

  class ValueParser

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

    def self.parse(value)
      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

9 entries across 9 versions & 1 rubygems

Version Path
eye-patch-0.1.3 lib/eye/patch/value_parser.rb
eye-patch-0.1.2 lib/eye/patch/value_parser.rb
eye-patch-0.1.1 lib/eye/patch/value_parser.rb
eye-patch-0.1.0 lib/eye/patch/value_parser.rb
eye-patch-0.0.9 lib/eye/patch/value_parser.rb
eye-patch-0.0.8 lib/eye/patch/value_parser.rb
eye-patch-0.0.7 lib/eye/patch/value_parser.rb
eye-patch-0.0.6 lib/eye/patch/value_parser.rb
eye-patch-0.0.5 lib/eye/patch/value_parser.rb