Sha256: 6f5e7fc51500fa187bced4518bd8e0e5c99f2c012c22b6f51632069830bbc5e8

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

module ESA
  module ContextProviders
    class DateContextProvider < ESA::ContextProvider
      def self.provided_types
        ["ESA::Contexts::DateContext"]
      end

      def self.context_id(context, options = {})
        [context.start_date, context.end_date]
      end

      def self.contained_ids(context, options = {})
        dates = context.transactions.pluck("date(esa_transactions.time)").uniq.sort

        if options[:period].present? and options[:period] == :month
          dates.group_by{|d| [d.year, d.month]}.keys.
          map do |year,month|
            start_date = Date.new(year, month, 1)
            end_date = start_date.end_of_month
            [start_date, end_date]
          end
        else
          dates.zip dates
        end
      end

      def self.instantiate(parent, namespace, id, options = {})
        start_date, end_date = id
        ESA::Contexts::DateContext.new(chart_id: parent.chart_id, parent_id: parent.id, namespace: namespace, start_date: start_date, end_date: end_date)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
event_sourced_accounting-0.1.4 lib/esa/context_providers/date_context_provider.rb
event_sourced_accounting-0.1.3 lib/esa/context_providers/date_context_provider.rb
event_sourced_accounting-0.1.1 lib/esa/context_providers/date_context_provider.rb
event_sourced_accounting-0.1.0 app/models/esa/context_providers/date_context_provider.rb