Sha256: 30560037345927f8546fcde8ce0910f993fb5c3ca8951fcdde863bcf7ad9a948

Contents?: true

Size: 926 Bytes

Versions: 5

Compression:

Stored size: 926 Bytes

Contents

class Timespan
	class DurationParseError < StandardError; end

	module Span
		attr_reader :duration

		def duration= duration
			@duration = case duration
			when Timespan
				duration.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

5 entries across 5 versions & 1 rubygems

Version Path
timespan-0.2.5 lib/timespan/span.rb
timespan-0.2.4 lib/timespan/span.rb
timespan-0.2.3 lib/timespan/span.rb
timespan-0.2.2 lib/timespan/span.rb
timespan-0.2.1 lib/timespan/span.rb