lib/timespan.rb in timespan-0.2.4 vs lib/timespan.rb in timespan-0.2.5

- old
+ new

@@ -32,16 +32,19 @@ ALL_KEYS = START_KEYS + END_KEYS + DURATION_KEYS def initialize options = {} @is_new = true + @init_options = options + validate! if options == {} + case options when Numeric, Duration, String options = {:duration => options} end - configure options + configure! options @is_new = false end def start_time= time @@ -87,15 +90,18 @@ end end protected + attr_reader :init_options + def first_from keys, options = {} keys.select {|key| options[key] }.first end - def configure options = {} + # uses init_options to configure + def configure! options = {} from = options[first_from START_KEYS, options] to = options[first_from END_KEYS, options] dur = options[first_from DURATION_KEYS, options] self.duration = dur if dur @@ -112,15 +118,15 @@ def default_from_now! self.start_time = Time.now end - def validate! - raise ArgumentError, "#{valid_requirement}, was: #{current_config}" unless valid? + def validate! + raise ArgumentError, "#{valid_requirement}, was: #{init_options.inspect} resulting in state: #{current_config}" unless valid? end def valid_requirement - "Timespan must take a :start and :end time or any of :start and :end time and a :duration" + "Timespan must take 1-2 of :start_time, :end_time or :duration or simply a duration as number of seconds or a string" end def current_config "end time: #{end_time}, start time: #{start_time}, duration: #{duration}" end \ No newline at end of file