Module: Mongoid::Timespanned::ClassMethods

Defined in:
lib/timespan/mongoid/timespanned.rb

Instance Method Summary collapse

Instance Method Details

#timespan_container_delegate(container, timespan, name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/timespan/mongoid/timespanned.rb', line 21

def timespan_container_delegate container, timespan, name      
  case name.to_sym
  when :start
    define_method "start_date=" do |date|
      send(container).send("#{timespan}_start=", date)
    end
  when :end
    define_method "end_date=" do |date|
      send(container).send("#{timespan}_end=", date)
    end
  when :duration
    define_method "duration=" do |date|
      send(container).send("#{timespan}_duration=", date)
    end
  end
end

#timespan_container_delegates(container, timespan, *names) ⇒ Object

fx Account.timespan_container_delegates :period, :dates, :start, :end

start_date= -> period.dates_start=
end_date= -> period.dates_end=


14
15
16
17
18
19
# File 'lib/timespan/mongoid/timespanned.rb', line 14

def timespan_container_delegates container, timespan, *names
  names = [:start, :end, :duration] if names.first == :all 
  names.flatten.each do |name|
    timespan_container_delegate container, timespan, name
  end
end

#timespan_delegates(name = :period) ⇒ Object



38
39
40
# File 'lib/timespan/mongoid/timespanned.rb', line 38

def timespan_delegates name = :period
  delegate :time_left, :duration, :start_date, :end_date, to: name
end

#timespan_methods(name) ⇒ Object



6
7
8
9
# File 'lib/timespan/mongoid/timespanned.rb', line 6

def timespan_methods name
  timespan_delegates name
  timespan_setters name
end

#timespan_setters(name = :period) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/timespan/mongoid/timespanned.rb', line 42

def timespan_setters name = :period  
  define_method :"#{name}_start=" do |date|
    options = self.send(name) ? {end_date: self.send(name).end_date} : {}
    self.send "#{name}=", ::Timespan.new(options.merge(start_date: date))
  end

  define_method :"#{name}_end=" do |date|
    options = self.send(name) ? {start_date: self.send(name).start_date} : {}
    self.send "#{name}=", ::Timespan.new(options.merge(end_date: date))
  end

  define_method :"#{name}_duration=" do |duration|
    options = self.send(name) ? {start_date: self.send(name).start_date} : {}
    self.send "#{name}=", ::Timespan.new(options.merge(duration: duration))
  end
end