lib/vanilla.rb in by_star-0.6.3 vs lib/vanilla.rb in by_star-0.6.4
- old
+ new
@@ -23,11 +23,11 @@
# by_month("January", :year => 2008)
# by_month(time)
def by_month(time=Time.zone.now.month, options={}, &block)
time = Time.zone.now.month if time.nil?
year, month = work_out_month(time, options.delete(:year))
-
+
start_time = start_of_month(month, year)
end_time = start_time.end_of_month
by_star(start_time, end_time, options, &block)
end
@@ -35,18 +35,18 @@
# Examples:
# # 18th fortnight of 2004
# Post.by_fortnight(18, :year => 2004)
def by_fortnight(time=Time.zone.now, options = {}, &block)
time = parse(time)
-
+
# If options[:year] is passed in, use that year regardless.
year = work_out_year(options[:year]) if options[:year]
# If the first argument is a date or time, ask it for the year
year ||= time.year unless time.is_a?(Numeric)
# If the first argument is a fixnum, assume this year.
year ||= Time.zone.now.year
-
+
# Dodgy!
# Surely there's a method in Rails to do this.
start_time = if valid_time_or_date?(time)
time.beginning_of_year + (time.strftime("%U").to_i).weeks
elsif time.is_a?(Numeric) && time <= 26
@@ -67,23 +67,23 @@
# Post.by_week(<Time object>)
# Post.by_week(<Date object>)
# Post.by_week("next tuesday")
def by_week(time=Time.zone.now, options = {}, &block)
time = parse(time)
-
+
# If options[:year] is passed in, use that year regardless.
year = work_out_year(options.delete(:year)) if options[:year]
# If the first argument is a date or time, ask it for the year
year ||= time.year unless time.is_a?(Numeric)
# If the first argument is a fixnum, assume this year.
year ||= Time.zone.now.year
-
+
# Dodgy!
# Surely there's a method in Rails to do this.
start_time = if valid_time_or_date?(time)
weeks = time.strftime("%U").to_i
-
+
# Sunday defines the start of the week.
# Finds the sunday that defines the starting week of the year.
# This is because AS +*.weeks helper works off the given time, which could be any day.
# Principle of least surprise and all. Ideally.
start_time = Time.now.beginning_of_year
@@ -107,11 +107,10 @@
# Post.by_weekend("next tuesday")
def by_weekend(time=Time.now, options = {}, &block)
time = parse(time)
start_time = time.beginning_of_weekend
end_time = (start_time + 1.day).end_of_day
- p start_time..end_time
by_star(start_time, end_time, options, &block)
end
# Examples:
@@ -183,51 +182,46 @@
time = parse(time)
by_direction(">", time, options, &block)
end
private
-
+
def by_direction(condition, time, options = {}, &block)
- field = options.delete(:field) || "created_at"
+ field = options.delete(:field) || "#{self.table_name}.created_at"
ensure_valid_options(options)
- scoping = { :conditions => ["#{field} #{condition} ?", time.utc] }.merge(options)
- with_scope(:find => scoping) do
- scoped_by(block) do
- find(:all)
- end
- end
+ result = scoped({ :conditions => ["#{field} #{condition} ?", time.utc] }.merge(options))
+ result = result.scoped(block.call) if block_given?
+ result
end
-
+
# scopes results between start_time and end_time
def by_star(start_time, end_time, options = {}, &block)
start_time = parse(start_time)
end_time = parse(end_time)
-
-
+
+
raise ParseError, "End time is before start time, searching like this will return no results." if end_time < start_time
field = options.delete(:field)
ensure_valid_options(options)
-
+
scoping = { :conditions => conditions_for_range(start_time, end_time, field) }.merge(options)
- with_scope(:find => scoping) do
- scoped_by(block) do
- find(:all)
- end
- end
+ result = scoped(scoping)
+ result = result.scoped(block.call) if block_given?
+ result
end
-
+
def ensure_valid_options(options)
if respond_to?(:validate_find_options)
validate_find_options(options)
else
options.assert_valid_keys(ActiveRecord::SpawnMethods::VALID_FIND_OPTIONS)
end
end
-
+
alias :between :by_star
public :between
-
+
# This will work for the next 30 years (written in 2009)
def work_out_year(value)
case value
when 0..39
2000 + value
@@ -239,16 +233,16 @@
# We may be passed something that's not a straight out integer
# These things include: BigDecimals, Floats and Strings.
value.to_i
end
end
-
+
# Checks if the object is a Time, Date or TimeWithZone object.
def valid_time_or_date?(value)
value.is_a?(Time) || value.is_a?(Date) || value.is_a?(ActiveSupport::TimeWithZone)
end
-
+
def parse(object)
object = case object.class.to_s
when "NilClass"
o = Time.zone.now
when "String"
@@ -260,20 +254,20 @@
object
end
raise ParseError, "Chronic couldn't work out #{o.inspect}; please be more precise." if object.nil?
object
end
-
+
def method_missing(method, *args)
if method.to_s =~ /^(as_of|up_to)_(.+)$/
method = $1
expr = $2.humanize
unless time = parse(expr)
raise ParseError, "Chronic couldn't work out #{expr.inspect}; please be more precise."
end
-
+
reference = args.first || Time.now
-
+
if "as_of" == method
between(time, reference)
else
between(reference, time)
end
\ No newline at end of file