Skip to content

Commit

Permalink
simplifies +/- methods for durations and ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
markrebec committed Oct 13, 2015
1 parent 012c23a commit 6ffbdd2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 30 deletions.
20 changes: 4 additions & 16 deletions lib/duranged/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,13 @@ def <=>(other)
end

def +(other)
if other.is_a?(Duration) || other.is_a?(Interval)
Duranged.logger.warn "Warning: You are adding a #{other.class.name} to a #{self.class.name}, which will result in a #{self.class.name}. If you would like a #{other.class.name} object to be returned you must add your #{self.class.name} to your #{other.class.name} instead." if other.is_a?(Range)
self.class.new(value + other.value)
elsif other.is_a?(Integer)
self.class.new(value + other)
else
raise ArgumentError, "value must be an Integer, Duranged::Duration or Duranged::Interval"
end
Duranged.logger.warn "Warning: You are adding a #{other.class.name} to a #{self.class.name}, which will result in a #{self.class.name}. If you would like a #{other.class.name} object to be returned you must add your #{self.class.name} to your #{other.class.name} instead." if other.is_a?(Range)
self.class.new(to_i + other.to_i)
end

def -(other)
if other.is_a?(Duration) || other.is_a?(Interval)
Duranged.logger.warn "Warning: You are subtracting a #{other.class.name} from a #{self.class.name}, which will result in a #{self.class.name}. If you would like a #{other.class.name} object to be returned you must subtract your #{self.class.name} from your #{other.class.name} instead." if other.is_a?(Range)
self.class.new(value - other.value)
elsif other.is_a?(Integer)
self.class.new(value - other)
else
raise ArgumentError, "value must be an Integer, Duranged::Duration or Duranged::Interval"
end
Duranged.logger.warn "Warning: You are subtracting a #{other.class.name} from a #{self.class.name}, which will result in a #{self.class.name}. If you would like a #{other.class.name} object to be returned you must subtract your #{self.class.name} from your #{other.class.name} instead." if other.is_a?(Range)
self.class.new(to_i - other.to_i)
end

protected
Expand Down
16 changes: 2 additions & 14 deletions lib/duranged/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,11 @@ def end_at(format=nil)
alias_method :end_time, :end_at

def +(other)
if other.is_a?(Duration) || other.is_a?(Interval)
self.class.new(start_at, value + other.value)
elsif other.is_a?(Integer)
self.class.new(start_at, value + other)
else
raise ArgumentError, "value must be an Integer, Duranged::Duration or Duranged::Interval"
end
self.class.new(start_at, to_i + other.to_i)
end

def -(other)
if other.is_a?(Duration) || other.is_a?(Interval)
self.class.new(start_at, value - other.value)
elsif other.is_a?(Integer)
self.class.new(start_at, value - other)
else
raise ArgumentError, "value must be an Integer, Duranged::Duration or Duranged::Interval"
end
self.class.new(start_at, to_i - other.to_i)
end

def to_duration
Expand Down

0 comments on commit 6ffbdd2

Please sign in to comment.