lib/est/estimates.rb in est-0.2.1 vs lib/est/estimates.rb in est-0.3
- old
+ new
@@ -40,27 +40,36 @@
end
# Get total estimate.
def total
estimates = iterate
- estimates.reduce(0) do |a, e|
- Est.log.info "#{e.date}/#{e.author}: #{e.total}"
- a + e.total
- end / estimates.size
+ if estimates.empty?
+ total = 0
+ else
+ total = estimates.reduce(0) do |a, e|
+ Est.log.info "#{e.date}/#{e.author}: #{e.total}"
+ a + e.total
+ end / estimates.size
+ end
+ total
end
# Iterate them all
def iterate
unless @iterate
- @iterate = Dir.entries(@dir)
- .reject { |f| f.index('.') == 0 }
- .select { |f| f =~ /^.*\.est$/ }
- .map { |f| File.join(@dir, f) }
- .each { |f| Est.log.info "#{f} found" }
- .map { |f| Estimate.new(f) }
- .map { |f| Estimate::Const.new(f) }
+ if File.exist?(@dir) && File.directory?(@dir)
+ @iterate = Dir.entries(@dir)
+ .reject { |f| f.index('.') == 0 }
+ .select { |f| f =~ /^.*\.est$/ }
+ .map { |f| File.join(@dir, f) }
+ .each { |f| Est.log.info "#{f} found" }
+ .map { |f| Estimate.new(f) }
+ .map { |f| Estimate::Const.new(f) }
+ else
+ Est.log.info "#{@dir} is absent or is not a directory"
+ @iterate = []
+ end
end
- fail "no .est files found in #{@dir}" if @iterate.empty?
@iterate
end
# Const estimates.
class Const