module Mongoid::Timespanned::ClassMethods

Attributes

max_asap[W]
min_asap[W]

Public Instance Methods

asap_method(path) click to toggle source
# File lib/timespan/mongoid/timespanned.rb, line 38
def asap_method path
  define_singleton_method :asap do
    {:"#{path}.from".gte => min_asap, :"#{path}.from".lte => max_asap}
  end
end
duration_methods(path) click to toggle source
# File lib/timespan/mongoid/timespanned.rb, line 44
def duration_methods path
  define_singleton_method :exactly do |period|
    [{:"#{path}.from" => period.to_i}, {:"#{path}.to" => period.to_i}]
  end

  define_singleton_method :in_between do |range|
    [{:"#{path}.from" => range.min.to_i}, {:"#{path}.to" => range.max.to_i}]
  end

  define_singleton_method :at_least do |period|
    {:"#{path}.to".gte => period.to_i }
  end

  define_singleton_method :at_most do |period|
    {:"#{path}.to".lte => period.to_i }
  end
end
max_asap(time = nil) click to toggle source
# File lib/timespan/mongoid/timespanned.rb, line 30
def max_asap time = nil
  @max_asap ||= (time || 10.days.from_now).to_i
end
min_asap(time = nil) click to toggle source
# File lib/timespan/mongoid/timespanned.rb, line 34
def min_asap time = nil
  @min_asap ||= (time || 1.day.ago).to_i        
end
timespan_container_delegate(container, timespan, name, options = {}) click to toggle source
# File lib/timespan/mongoid/timespanned.rb, line 73
def timespan_container_delegate container, timespan, name, options = {}
  override = options[:override]
  mt = Mongoid::Timespanned
  case name.to_sym
  when :start
    meth = "start_date="
    raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && ! override

    mt.log "#{self} define container delegate: #{meth} to #{container}.#{timespan}_start="
    define_method meth do |date|            
      self.send(container).send("#{timespan}_start=", date)
    end
  when :end
    meth = "end_date="
    raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override

    mt.log "#{self} define container delegate: #{meth} to #{container}.#{timespan}_end="
    define_method meth do |date|
      self.send(container).send("#{timespan}_end=", date)
    end
  when :duration
    meth = "duration="
    raise ArgumentError, "method duration= already defined on #{self}" if self.respond_to?(meth) && !override

    mt.log "#{self} define container delegate: #{meth} to #{container}.#{timespan}_duration="
    define_method meth do |date|
      self.send(container).send("#{timespan}_duration=", date)
    end
  end
end
timespan_container_delegates(container, timespan, *names) click to toggle source

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

start_date= -> period.dates_start=
end_date= -> period.dates_end=
# File lib/timespan/mongoid/timespanned.rb, line 65
def timespan_container_delegates container, timespan, *names
  options = names.extract_options!
  names = [:start, :end, :duration] if names.first == :all || names.empty?
  names.flatten.each do |name|
    timespan_container_delegate container, timespan, name, options = {}
  end
end
timespan_delegate(meth, target = :period, options = {}) click to toggle source
# File lib/timespan/mongoid/timespanned.rb, line 128
def timespan_delegate meth, target = :period, options = {}
  override = options[:override]
  mt = Mongoid::Timespanned
  raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override

  mt.log "#{self} define delegate: #{meth} to #{target}"
  delegate meth, to: target
end
timespan_delegates(target = :period, *names) click to toggle source
# File lib/timespan/mongoid/timespanned.rb, line 111
def timespan_delegates target = :period, *names
  options = names.extract_options!
  names = names.flatten
  names = [:start_date, :end_date, :duration] if names.first == :all || names.empty?
  names.map! do |name|
    case name.to_sym
    when :start then :start_date
    when :end then :end_date
    else
      name.to_sym
    end
  end
  names.flatten.each do |name|
    timespan_delegate name, target, options
  end
end
timespan_methods(target = :period, *names) click to toggle source
# File lib/timespan/mongoid/timespanned.rb, line 104
def timespan_methods target = :period, *names
  options = names.extract_options!        
  names = [:start, :end, :duration] if names.first == :all || names.empty?
  timespan_delegates target, names, options
  timespan_setters target, names, options
end
timespan_setter(name, meth_name, options = {}) click to toggle source
# File lib/timespan/mongoid/timespanned.rb, line 146
def timespan_setter name, meth_name, options = {}
  override = options[:override]
  mt = Mongoid::Timespanned
  case meth_name.to_sym
  when :start
    meth = "#{name}_start="
    raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override

    mt.log "#{self} define setter: #{meth}"
    define_method meth do |date|
      options = self.send(name) ? {end_date: self.send(name).end_date} : {}
      timespan = ::Timespan.new(options.merge(start_date: date))
      self.send "#{name}=", timespan
    end
  when :end
    meth = "#{name}_end="
    raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override

    mt.log "#{self} define setter: #{meth}"
    define_method meth do |date|
      options = self.send(name) ? {start_date: self.send(name).start_date} : {}
      timespan = ::Timespan.new(options.merge(end_date: date))
      self.send "#{name}=", timespan
    end
  when :duration
    meth = "#{name}_duration="
    raise ArgumentError, "method #{meth} already defined on #{self}" if self.respond_to?(meth) && !override

    mt.log "#{self} define setter: #{meth}"

    define_method meth do |duration|
      options = self.send(name) ? {start_date: self.send(name).start_date} : {}
      timespan = ::Timespan.new(options.merge(duration: duration))
      self.send "#{name}=", timespan
    end
  end
end
timespan_setters(target = :period, *names) click to toggle source
# File lib/timespan/mongoid/timespanned.rb, line 137
def timespan_setters target = :period, *names
  options = names.extract_options!
  names = [:start, :end, :duration] if names.first == :all || names.empty?

  names.flatten.each do |name|
    timespan_setter target, name, options
  end
end