Sha256: b8b973d7fe84006ea36c6214b25b97181d8fef73d8d366286c718dce4d9256cf

Contents?: true

Size: 2 KB

Versions: 3

Compression:

Stored size: 2 KB

Contents

module ESA
  module Contexts
    class DateContext < ESA::Context
      attr_accessible :start_date, :end_date
      attr_readonly   :start_date, :end_date

      validate :validate_dates

      def preceeding_context
        if self.start_date.present?
          DateContext.new chart: self.chart,
                          end_date: self.start_date - 1.day
        else
          nil
        end
      end

      def following_context
        if self.end_date.present?
          DateContext.new chart: self.chart,
                          start_date: self.end_date + 1.day
        else
          nil
        end
      end

      protected

      def validate_dates
        if self.start_date.nil? and self.end_date.nil?
          errors[:start_date] = "at least one of the two dates must be provided"
          errors[:end_date] = "at least one of the two dates must be provided"
        end
      end

      def create_name
        if self.start_date.present? and self.end_date.present?
          if self.start_date == self.end_date
            "#{self.start_date.to_s}"
          else
            "#{self.start_date.to_s} - #{self.end_date.to_s}"
          end
        elsif self.start_date.present?
          "#{self.start_date.to_s} - ..."
        elsif self.end_date.present?
          "... - #{self.end_date.to_s}"
        end
      end

      def create_position
        if self.start_date.present?
          self.start_date.to_time.to_i
        elsif self.end_date.present?
          self.end_date.to_time.to_i
        end
      end

      def initialize_filters
        @filters = []

        if self.start_date.present? and self.end_date.present?
          @filters << lambda { |relation| relation.with_date_range(self.start_date..self.end_date) }
        elsif self.start_date.present?
          @filters << lambda { |relation| relation.with_date_gte(self.start_date) }
        elsif self.end_date.present?
          @filters << lambda { |relation| relation.with_date_lte(self.end_date) }
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
event_sourced_accounting-0.1.6 app/models/esa/contexts/date_context.rb
event_sourced_accounting-0.1.4 app/models/esa/contexts/date_context.rb
event_sourced_accounting-0.1.3 app/models/esa/contexts/date_context.rb