Sha256: c5b2684d99705b94e0237bb8ebc09b07ab76b799b0caa996ae6ad1300badf0a5

Contents?: true

Size: 1.37 KB

Versions: 6

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true
module TimeBoss
  class Calendar
    module Support
      module Shiftable
        PERIODS = %w[day week month quarter half year]

        PERIODS.each do |period|
          periods = period.pluralize

          define_method periods do
            calendar.send("#{periods}_for", self)
          end

          define_method period do
            entries = send(periods)
            return nil unless entries.length == 1
            entries.first
          end

          define_method "in_#{period}" do
            base = send(periods)
            return unless base.length == 1
            base.first.send(self.class.type.to_s.pluralize).find_index { |p| p == self } + 1
          end

          define_method "#{periods}_ago" do |offset|
            base_offset = send("in_#{period}") or return
            (calendar.send("this_#{period}") - offset).send(self.class.type.to_s.pluralize)[base_offset - 1]
          end

          define_method "last_#{period}" do
            send("#{periods}_ago", 1)
          end

          define_method "this_#{period}" do
            send("#{periods}_ago", 0)
          end

          define_method "#{periods}_hence" do |offset|
            send("#{periods}_ago", offset * -1)
          end

          define_method "next_#{period}" do
            send("#{periods}_hence", 1)
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
timeboss-0.0.10 lib/timeboss/calendar/support/shiftable.rb
timeboss-0.0.9 lib/timeboss/calendar/support/shiftable.rb
timeboss-0.0.8 lib/timeboss/calendar/support/shiftable.rb
timeboss-0.0.7 lib/timeboss/calendar/support/shiftable.rb
timeboss-0.0.6 lib/timeboss/calendar/support/shiftable.rb
timeboss-0.0.5 lib/timeboss/calendar/support/shiftable.rb