DataMapper query - last year, last month etc
I use sinatra web framework and DataMapper.
I have code:
@wdata = Data.all(:date => ((DateTime.now - @interval.to_i)..DateTime.now))
When I want Data from last 24 hours I use @interval = 1, last 7 days @interval = 7.
How I should get data from e.g. last month or last year? Month can have 31, 30, 28 or 29 days I would like to distinguish this. I would e.g. get data from june last year, last two months, etc.
I searched in web for some tutorial, but I found only for ActiveRecord. :(
Perhaps there is more elegant solution, but you can do such ranges manually
d = DateTime.today p = d.prev_month py = d.prev_year last_month = Data.all(:date => (DateTime.new(p.year, p.month)..DateTime.new(d.year, d.month)) last_year = Data.all(:date => (DateTime.new(py.year)..DateTime.new(d.year))