File: day.rb

package info (click to toggle)
ruby-ice-cube 0.16.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 800 kB
  • sloc: ruby: 7,823; makefile: 6
file content (77 lines) | stat: -rw-r--r-- 1,895 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module IceCube

  module Validations::Day

    def day(*days)
      days = days.flatten
      return self if days.empty?
      days.flatten.each do |day|
        unless day.is_a?(Integer) || day.is_a?(Symbol)
          raise ArgumentError, "expecting Integer or Symbol value for day, got #{day.inspect}"
        end
        day = TimeUtil.sym_to_wday(day)
        verify_alignment(day, :wday, :day) { |error| raise error }

        validations_for(:day) << Validation.new(day)
      end
      clobber_base_validations(:wday, :day)
      self
    end

    class Validation < Validations::FixedValue

      attr_reader :day
      alias :value :day

      def initialize(day)
        @day = day
      end

      def key
        :day
      end

      def type
        :wday
      end

      def dst_adjust?
        true
      end

      def build_s(builder)
        builder.piece(:day) << day
      end

      def build_hash(builder)
        builder.validations_array(:day) << day
      end

      def build_ical(builder)
        ical_day = IcalBuilder.fixnum_to_ical_day(day)
        # Only add if there aren't others from day_of_week that override
        if builder['BYDAY'].none? { |b| b.end_with?(ical_day) }
          builder['BYDAY'] << ical_day
        end
      end

      StringBuilder.register_formatter(:day) do |validation_days|
        # sort the days
        validation_days.sort!
        # pick the right shortening, if applicable
        if validation_days == [0, 6]
          IceCube::I18n.t('ice_cube.on_weekends')
        elsif validation_days == (1..5).to_a
          IceCube::I18n.t('ice_cube.on_weekdays')
        else
          day_names = ->(d){ "#{IceCube::I18n.t("ice_cube.days_on")[d]}" }
          segments = validation_days.map(&day_names)
          IceCube::I18n.t('ice_cube.on_days', days: StringBuilder.sentence(segments))
        end
      end

    end

  end

end