class DateSupercharger::MethodDefiner
Attributes
klass[R]
Public Class Methods
new(klass)
click to toggle source
# File lib/date_supercharger/method_definer.rb, line 6 def initialize(klass) @klass = klass end
Public Instance Methods
define(opts)
click to toggle source
# File lib/date_supercharger/method_definer.rb, line 10 def define(opts) attribute = opts[:attribute] suffix = opts[:suffix] new_method = "#{attribute}_#{suffix}" case suffix when :after,:before,:before_or_at,:after_or_at operators = { after: ">", before: "<", before_or_at: "<=", after_or_at: ">=" } klass.singleton_class.class_eval do define_method(new_method) do |date| where("#{attribute} #{operators[suffix]} ?", date) end end when :between,:between_inclusive methods = {between: ["after_or_at","before"],between_inclusive:["after_or_at","before_or_at"]} klass.singleton_class.class_eval do define_method(new_method) do |from,to| from_method = methods[suffix].first to_method = methods[suffix].second send("#{attribute}_#{from_method}",from).send("#{attribute}_#{to_method}",to) end end end end