Class: Eye::Patch::ValueParser

Inherits:
Object
  • Object
show all
Defined in:
lib/eye/patch/value_parser.rb

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.parse(value) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/eye/patch/value_parser.rb', line 13

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

.parse_size(value) ⇒ Object



27
28
29
30
# File 'lib/eye/patch/value_parser.rb', line 27

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

.parse_time(value) ⇒ Object



23
24
25
# File 'lib/eye/patch/value_parser.rb', line 23

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