Browse files

Goodbye, old by_star

  • Loading branch information...
1 parent 1010788 commit 9cd3274c26aa9b9640aad676478fe1233cb29a82 @radar committed Feb 19, 2012
View
23 lib/by_star.bak/calculations.rb
@@ -1,23 +0,0 @@
-module ByStar
- module Calculations
- include Count
- include Sum
-
- private
- def work_out_month(time, year=Time.zone.now.year)
- year ||= Time.zone.now.year
- # Work out what actual month is.
- month = if time.is_a?(Numeric) && (1..12).include?(time)
- time
- elsif valid_time_or_date?(time)
- year = time.year
- time.month
- elsif time.is_a?(String) && Date::MONTHNAMES.include?(time)
- Date::MONTHNAMES.index(time)
- else
- raise ParseError, "Value is not an integer (between 1 and 12), time object or string (make sure you typed the name right)."
- end
- [year, month]
- end
- end
-end
View
20 lib/by_star.bak/calculations/count.rb
@@ -1,20 +0,0 @@
-module ByStar
- module Calculations
- module Count
- def count_by_year(field=nil, year=Time.zone.now.year, options={}, &block)
- db_field = options.delete(:field)
- scoped_by(block) do
- count(field, { :conditions => conditions_for_range(start_of_year(year), end_of_year(year), db_field) }.reverse_merge!(options))
- end
- end
-
- def count_by_month(field=nil, month=Time.now.month, options={}, &block)
- db_field = options.delete(:field)
- year, month = work_out_month(month, options.delete(:year))
- scoped_by(block) do
- count(field, { :conditions => conditions_for_range(start_of_month(month, year), end_of_month(month, year), db_field) }.reverse_merge!(options))
- end
- end
- end
- end
-end
View
25 lib/by_star.bak/calculations/sum.rb
@@ -1,25 +0,0 @@
-module ByStar
- module Calculations
- module Sum
- def sum_by_year(field, year=Time.zone.now.year, options={}, &block)
- scoped_by(block) do
- sum(field, { :conditions => conditions_for_range(start_of_year(year), end_of_year(year), options.delete(:field)) }.reverse_merge!(options))
- end
- end
-
- def sum_by_month(field, month=Time.zone.now.month, options={}, &block)
- year, month = work_out_month(month, options.delete(:year))
- scoped_by(block) do
- sum(field, { :conditions => conditions_for_range(start_of_month(month, year), end_of_month(month, year), options.delete(:field)) }.reverse_merge!(options))
- end
- end
-
- def sum_by_day(field, day=Time.zone.now, options={}, &block)
- scoped_by(block) do
- day = parse(day)
- sum(field, { :conditions => conditions_for_range(day.beginning_of_day, day.end_of_day, options.delete(:field)) }.reverse_merge!(options))
- end
- end
- end
- end
-end
View
15 lib/by_star.bak/neighbours.rb
@@ -1,15 +0,0 @@
-module ByStar
- module Neighbours
- # Find the previous record to this.
- def previous(field=nil)
- field = field || self.class.by_star_field
- self.class.past(self.send(field.to_s.split(".").last)) { { :order => "#{field} DESC" }}.first
- end
-
- # Find the next record to this.
- def next(field=nil)
- field = field || self.class.by_star_field
- self.class.future(self.send(field.to_s.split(".").last)) { { :order => "#{field} ASC" }}.first
- end
- end
-end
View
23 lib/by_star.bak/range_calculations.rb
@@ -1,23 +0,0 @@
-module ByStar
- module RangeCalculations
-
- private
-
- def start_of_year(year=Time.zone.now.year)
- Time.utc(year, 1, 1)
- end
-
- def end_of_year(year=Time.zone.now.year)
- start_of_year(year).end_of_year
- end
-
- def start_of_month(month, year=Timeow.year)
- Time.utc(year, month, 1)
- end
-
- def end_of_month(month, year=Time.now.year)
- start_of_month(month, year).end_of_month
- end
- end
-
-end
View
18 lib/by_star.bak/shared.rb
@@ -1,18 +0,0 @@
-module Shared
- def conditions_for_range(start_time, end_time, field=nil)
- field = "#{table_name}.#{field}" if field
- field ||= by_star_field
- ["#{field} >= ? AND #{field} <= ?", start_time.utc, end_time.utc]
- end
-
- private
- def scoped_by(options=nil, &block)
- if options && scope = options.call
- with_scope(:find => scope) do
- block.call
- end
- else
- block.call
- end
- end
-end
View
20 lib/by_star.bak/time_ext.rb
@@ -1,20 +0,0 @@
-class Time
- def beginning_of_weekend
- friday = case self.wday
- when 0
- self.end_of_week.beginning_of_day.advance(:days => -2)
- when 5
- self.beginning_of_day
- else
- self.beginning_of_week.advance(:days => 4)
- end
- # 3pm, Friday.
- (friday + 15.hours)
- end
-
- def end_of_weekend
- # 3am, Monday.
- # LOL I CHEATED.
- beginning_of_weekend + 3.days - 12.hours
- end
-end
View
276 lib/by_star.bak/vanilla.rb
@@ -1,276 +0,0 @@
-module ByStar
- module Vanilla
- # Examples:
- # by_year(2010)
- # # 2-digit year:
- # by_year(10)
- # # Time or Date object:
- # by_year(time)
- # # String:
- # by_year("2010")
- def by_year(time=Time.zone.now.year, options={}, &block)
- year = work_out_year(time)
- start_time = start_of_year(year)
- end_time = start_time.end_of_year
- by_star(start_time, end_time, options, &block)
- rescue ArgumentError
- raise ParseError, "Invalid arguments detected, year may possibly be outside of valid range (1902-2039). This is no longer a problem on Ruby versions >= 1.8.7, so we recommend you upgrade to at least 1.8.7."
- end
-
- # Examples:
- # by_month(1)
- # by_month("January")
- # 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
-
- # 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
- Time.utc(year, 1, 1) + ((time.to_i) * 2).weeks
- else
- raise ParseError, "by_fortnight takes only a Time or Date object, a Fixnum (less than or equal to 26) or a Chronicable string."
- end
- start_time = start_time.beginning_of_week
- end_time = start_time + 2.weeks
- by_star(start_time, end_time, options, &block)
- end
-
- # Examples:
- # # 36th week
- # Post.by_week(36)
- # Post.by_week(36.54)
- # Post.by_week(36, :year => 2004)
- # 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
- Time.zone.now.beginning_of_year
- elsif time.is_a?(Numeric) && time < 53
- weeks = time.to_i
- Time.utc(year, 1, 1)
- else
- raise ParseError, "by_week takes only a Time or Date object, a Fixnum (less than or equal to 53) or a Chronicable string."
- end
- # Make the start of the week predictably Sunday.
- start_time.strftime("%w").to_i != 0 ? start_time - (7 - start_time.strftime("%w").to_i).days : start_time
- start_time += weeks.weeks
- end_time = start_time + 1.week
- by_star(start_time, end_time, options, &block)
- end
-
-
- # Examples:
- # Post.by_weekend
- # Post.by_weekend(Time.now + 5.days)
- # Post.by_weekend(Date.today + 5)
- # 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
- by_star(start_time, end_time, options, &block)
- end
-
-
- # Examples:
- # Post.by_current_weekend
- def by_current_weekend(options = {}, &block)
- time = Time.zone.now
- # Friday, 3pm
- start_time = time.beginning_of_weekend
- # Monday, 3am
- end_time = time.end_of_weekend
- by_star(start_time, end_time, options, &block)
- end
-
- # Examples:
- # Post.by_current_work_week
- def by_current_work_week(options = {}, &block)
- time = Time.zone.now
- # Monday, 3am
- time = time + 1.week if time.wday == 6 || time.wday == 0
- start_time = time.beginning_of_week + 3.hours
- # Friday, 3pm
- end_time = time.beginning_of_weekend
- by_star(start_time, end_time, options, &block)
- end
-
-
- # Examples:
- # Post.by_day
- # Post.by_day(Time.yesterday)
- # Post.by_day("next tuesday")
- def by_day(time = Time.zone.now, options = {}, &block)
- time = parse(time)
- by_star(time.beginning_of_day, time.end_of_day, options, &block)
- end
- alias_method :today, :by_day
-
- # Examples:
- # Post.yesterday
- # # 2 days ago:
- # Post.yesterday(Time.yesterday)
- # # day before next tuesday
- # Post.yesterday("next tuesday")
- def yesterday(time = Time.zone.now, options = {}, &block)
- time = parse(time)
- by_day(time.advance(:days => -1), options, &block)
- end
-
- # Examples:
- # Post.tomorrow
- # # 2 days from now:
- # Post.tomorrow(Time.tomorrow)
- # # day after next tuesday
- # Post.tomorrow("next tuesday")
- def tomorrow(time = Time.zone.now, options = {}, &block)
- time = parse(time)
- by_day(time.advance(:days => 1), options, &block)
- end
-
- # Scopes to records older than current or given time
- # Post.past
- # Post.past()
- def past(time = Time.zone.now, options = {}, &block)
- time = parse(time)
- by_direction("<", time, options, &block)
- end
-
- # Scopes to records newer than current or given time
- def future(time = Time.zone.now, options = {}, &block)
- time = parse(time)
- by_direction(">", time, options, &block)
- end
-
- private
-
- def by_direction(condition, time, options = {}, &block)
- field = options.delete(:field) || by_star_field
- ensure_valid_options(options)
- 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)
- 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
- when 40..99
- 1900 + value
- when nil
- Time.zone.now.year
- else
- # 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"
- o = object
- Chronic.parse(object, :now => Time.zone.now)
- when "Date"
- object.to_time(:utc)
- else
- 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
- else
- super
- end
- end
- end
-end
View
3 lib/by_star.bak/version.rb
@@ -1,3 +0,0 @@
-module ByStar
- VERSION = "1.0.1"
-end

0 comments on commit 9cd3274

Please sign in to comment.