Module: Mongoid::Timespanned::ClassMethods
- Defined in:
- lib/timespan/mongoid/timespanned.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #asap_method(path) ⇒ Object
- #duration_methods(path) ⇒ Object
- #timespan_container_delegate(container, timespan, name, options = {}) ⇒ Object
-
#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=.
- #timespan_delegate(meth, target = :period, options = {}) ⇒ Object
- #timespan_delegates(target = :period, *names) ⇒ Object
- #timespan_methods(target = :period, *names) ⇒ Object
- #timespan_setter(name, meth_name, options = {}) ⇒ Object
- #timespan_setters(target = :period, *names) ⇒ Object
Instance Attribute Details
#max_asap(time = nil) ⇒ Object
30 31 32 |
# 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) ⇒ Object
34 35 36 |
# File 'lib/timespan/mongoid/timespanned.rb', line 34 def min_asap time = nil @min_asap ||= (time || 1.day.ago).to_i end |
Instance Method Details
#asap_method(path) ⇒ Object
38 39 40 41 42 |
# 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) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 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 |
#timespan_container_delegate(container, timespan, name, options = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/timespan/mongoid/timespanned.rb', line 73 def timespan_container_delegate container, timespan, name, = {} override = [: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) ⇒ Object
fx Account.timespan_container_delegates :period, :dates, :start, :end
start_date= -> period.dates_start=
end_date= -> period.dates_end=
65 66 67 68 69 70 71 |
# File 'lib/timespan/mongoid/timespanned.rb', line 65 def timespan_container_delegates container, timespan, *names = names. names = [:start, :end, :duration] if names.first == :all || names.empty? names.flatten.each do |name| timespan_container_delegate container, timespan, name, = {} end end |
#timespan_delegate(meth, target = :period, options = {}) ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'lib/timespan/mongoid/timespanned.rb', line 128 def timespan_delegate meth, target = :period, = {} override = [: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) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/timespan/mongoid/timespanned.rb', line 111 def timespan_delegates target = :period, *names = names. 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, end end |
#timespan_methods(target = :period, *names) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/timespan/mongoid/timespanned.rb', line 104 def timespan_methods target = :period, *names = names. names = [:start, :end, :duration] if names.first == :all || names.empty? timespan_delegates target, names, timespan_setters target, names, end |
#timespan_setter(name, meth_name, options = {}) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/timespan/mongoid/timespanned.rb', line 146 def timespan_setter name, meth_name, = {} override = [: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| = self.send(name) ? {end_date: self.send(name).end_date} : {} timespan = ::Timespan.new(.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| = self.send(name) ? {start_date: self.send(name).start_date} : {} timespan = ::Timespan.new(.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| = self.send(name) ? {start_date: self.send(name).start_date} : {} timespan = ::Timespan.new(.merge(duration: duration)) self.send "#{name}=", timespan end end end |
#timespan_setters(target = :period, *names) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/timespan/mongoid/timespanned.rb', line 137 def timespan_setters target = :period, *names = names. names = [:start, :end, :duration] if names.first == :all || names.empty? names.flatten.each do |name| timespan_setter target, name, end end |