lib/fugit/duration.rb in fugit-1.1.3 vs lib/fugit/duration.rb in fugit-1.1.4

- old
+ new

@@ -3,44 +3,62 @@ class Duration attr_reader :original, :h, :options - def self.new(s) + class << self - parse(s) - end + def new(s) - def self.parse(s, opts={}) + parse(s) + end - return s if s.is_a?(self) + def parse(s, opts={}) - original = s + return s if s.is_a?(self) - s = "#{s}s" if s.is_a?(Numeric) + original = s - return nil unless s.is_a?(String) + s = "#{s}s" if s.is_a?(Numeric) - s = s.strip + return nil unless s.is_a?(String) + + s = s.strip #p [ original, s ]; Raabro.pp(Parser.parse(s, debug: 3), colours: true) - h = - if opts[:iso] - IsoParser.parse(opts[:stricter] ? s : s.upcase) - elsif opts[:plain] - Parser.parse(s) - else - Parser.parse(s) || IsoParser.parse(opts[:stricter] ? s : s.upcase) - end -#p h + h = + if opts[:iso] + IsoParser.parse(opts[:stricter] ? s : s.upcase) + elsif opts[:plain] + Parser.parse(s) + else + Parser.parse(s) || IsoParser.parse(opts[:stricter] ? s : s.upcase) + end - h ? self.allocate.send(:init, original, opts, h) : nil - end + h ? self.allocate.send(:init, original, opts, h) : nil + end - def self.do_parse(s, opts={}) + def do_parse(s, opts={}) - parse(s, opts) || fail(ArgumentError.new("not a duration #{s.inspect}")) + parse(s, opts) || + fail(ArgumentError.new("not a duration #{s.inspect}")) + end + + def to_plain_s(o); do_parse(o).deflate.to_plain_s; end + def to_iso_s(o); do_parse(o).deflate.to_iso_s; end + def to_long_s(o, opts={}); do_parse(o).deflate.to_long_s(opts); end + + def common_rewrite_dur(t) + + t + .subgather(nil) + .inject({}) { |h, tt| + v = tt.string; v = v.index('.') ? v.to_f : v.to_i + # drops ending ("y", "m", ...) by itself + h[tt.name] = (h[tt.name] || 0) + v + h } + end end KEYS = { yea: { a: 'Y', r: 'y', i: 'Y', s: 365 * 24 * 3600, x: 0, l: 'year' }, mon: { a: 'M', r: 'M', i: 'M', s: 30 * 24 * 3600, x: 1, l: 'month' }, @@ -101,16 +119,10 @@ end s.string end - class << self - def to_plain_s(o); do_parse(o).deflate.to_plain_s; end - def to_iso_s(o); do_parse(o).deflate.to_iso_s; end - def to_long_s(o, opts={}); do_parse(o).deflate.to_long_s(opts); end - end - # For now, let's alias to #h # def to_h; h; end def to_rufus_h @@ -306,35 +318,23 @@ end self end - def self.common_rewrite_dur(t) - - t - .subgather(nil) - .inject({}) { |h, tt| - v = tt.string; v = v.index('.') ? v.to_f : v.to_i - # drops ending ("y", "m", ...) by itself - h[tt.name] = (h[tt.name] || 0) + v - h - } - end - module Parser include Raabro # piece parsers bottom to top def sep(i); rex(nil, i, /([ \t,]+|and)*/i); end def yea(i); rex(:yea, i, /(\d+\.\d*|(\d*\.)?\d+) *y(ears?)?/i); end def mon(i); rex(:mon, i, /(\d+\.\d*|(\d*\.)?\d+) *(M|months?)/); end - def wee(i); rex(:wee, i, /(\d+\.\d*|(\d*\.)?\d+) *(weeks?|w)/i); end - def day(i); rex(:day, i, /(\d+\.\d*|(\d*\.)?\d+) *(days?|d)/i); end - def hou(i); rex(:hou, i, /(\d+\.\d*|(\d*\.)?\d+) *(hours?|h)/i); end - def min(i); rex(:min, i, /(\d+\.\d*|(\d*\.)?\d+) *(mins?|minutes?|m)/); end + def wee(i); rex(:wee, i, /(\d+\.\d*|(\d*\.)?\d+) *w(eeks?)?/i); end + def day(i); rex(:day, i, /(\d+\.\d*|(\d*\.)?\d+) *d(ays?)?/i); end + def hou(i); rex(:hou, i, /(\d+\.\d*|(\d*\.)?\d+) *h(ours?)?/i); end + def min(i); rex(:min, i, /(\d+\.\d*|(\d*\.)?\d+) *m(in(ute)?s?)?/); end - def sec(i); rex(:sec, i, /(\d+\.\d*|(\d*\.)?\d+) *(secs?|seconds?|s)/i); end + def sec(i); rex(:sec, i, /(\d+\.\d*|(\d*\.)?\d+) *s(ec(ond)?)?s?/i); end def sek(i); rex(:sec, i, /(\d+\.\d*|\.\d+|\d+)$/); end def elt(i); alt(nil, i, :yea, :mon, :wee, :day, :hou, :min, :sec, :sek); end def sign(i); rex(:sign, i, /[-+]?/); end