Created
May 16, 2013 11:43
-
-
Save guipdutra/5591153 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MovementsFilter | |
attr_writer :accountancy_movement, :pending_accountancy | |
def initialize(params = {}) | |
@descriptor = params.fetch(:descriptor) { raise ArgumentError, "You must supply a :descriptor param" } | |
@month = params[:month] || Date.current.month | |
@year = params[:year] || Date.current.year | |
end | |
def filter() | |
period.map do |date| | |
{ | |
date: date, | |
movements: movements.fetch(date, 0), | |
pendings: pendings.fetch(date, 0) | |
} | |
end.reject { |item| item[:movements].zero? && item[:pendings].zero? } | |
end | |
private | |
attr_reader :descriptor, :month, :year | |
def date | |
@date ||= Date.new(year.to_i, month.to_i) | |
end | |
def period | |
@period ||= date.beginning_of_month..date.end_of_month | |
end | |
def movements | |
@accountancy_movements ||= accountancy_movement.with_descriptor(descriptor) | |
.by_date(period) | |
.count(:group => :date) | |
end | |
def pendings | |
@pending_accountancies ||= pending_accountancy.with_descriptor(descriptor) | |
.unprocessed(true) | |
.by_date(period) | |
.count(:group => :date) | |
require 'pry'; binding.pry | |
end | |
def accountancy_movement | |
@accountancy_movement ||= AccountancyMovement | |
end | |
def pending_accountancy | |
@pending_accountancy ||= PendingAccountancy | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment