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