Sha256: 7773b0cddb520f44cdc031088af217aa5ce218df787d4dee3c6ae4e1cf4bfd4d

Contents?: true

Size: 887 Bytes

Versions: 1

Compression:

Stored size: 887 Bytes

Contents

class Timespan
	class DurationParseError < StandardError; end

	module Span
		attr_reader :duration

		def duration= duration
			@duration = case duration
			when Duration
				duration
			when Numeric, Hash
				Duration.new duration
			when String
				Duration.new parse_duration(duration)
			else
				raise ArgumentError, "Unsupported duration type: #{duration}"
			end	
			unless is_new?
				add_dirty :duration
				refresh!
				calculate!
			end
		end

		protected

		def parse_duration text
			spanner_parse text
		rescue Spanner::ParseError => e
			chronic_parse text
		rescue ChronicDuration::DurationParseError => e
			raise Timespan::DurationParseError, "Internal error: neither Spanner or ChronicDuration could parse '#{duration}'"
		end

		def spanner_parse text
			Spanner.parse(text.gsub /and/, '')
		end

		def chronic_parse text
			ChronicDuration.parse text
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
timespan-0.2.0 lib/timespan/span.rb