Module: Vpim::View::Week

Defined in:
lib/vpim/view.rb

Overview

View only events occuring in the next week.

Instance Method Summary collapse

Instance Method Details

#each(klass = nil) ⇒ Object

:nodoc:



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
# File 'lib/vpim/view.rb', line 18

def each(klass = nil) #:nodoc:
  unless block_given?
    return Enumerable::Enumerator.new(self, :each, klass)
  end

  t0 = Time.new.to_a
  t0[0] = t0[1] = t0[2] = 0 # sec,min,hour = 0
  t0 = Time.local(*t0)
  t1 = t0 + 7 * SECSPERDAY

  # Need to filter occurrences, too. Create modules for this on the fly.
  occurrences = Module.new
  # I'm passing state into the module's instance methods by doing string
  # evaluation... which sucks, but I don't think I can get this closure in
  # there.
  occurrences.module_eval(<<"__", __FILE__, __LINE__+1)
    def occurrences(dountil=nil)
      unless block_given?
        return Enumerable::Enumerator.new(self, :occurrences, dountil)
      end
      super(dountil) do |t|
        t0 = Time.at(#{t0.to_i})
        t1 = Time.at(#{t1.to_i})
        break if t >= t1
        tend = t
        if respond_to? :duration
          tend += duration || 0
        end
        if tend >= t0
          yield t
        end
      end
    end
__
=begin
  block = lambda do |dountil| 
      unless block_given?
        return Enumerable::Enumerator.new(self, :occurrences, dountil)
      end
      super(dountil) do |t|
        break if t >= t1
        yield t
      end
  end
  occurrences.send(:define_method, :occurrences, block)
=end
  super do |ve|
    if ve.occurs_in?(t0, t1)
      if ve.respond_to? :occurrences
        ve.extend occurrences
      end
      yield ve
    end
  end
end