Sha256: f256789d3d2bef970a21e40fb096aea5672f7ae952afe0c141ddc1d4a2a7ff56

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 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.between_dates(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

2 entries across 2 versions & 1 rubygems

Version Path
event_sourced_accounting-0.1.1 app/models/esa/contexts/date_context.rb
event_sourced_accounting-0.1.0 app/models/esa/contexts/date_context.rb