lib/by_star.rb in by_star-1.0.1 vs lib/by_star.rb in by_star-2.0.0.beta1
- old
+ new
@@ -1,43 +1,63 @@
require 'chronic'
-require 'by_star/shared'
-require 'by_star/range_calculations'
require 'by_star/time_ext'
-require 'by_star/vanilla'
-require 'by_star/neighbours'
+require 'by_star/instance_methods'
-require 'by_star/calculations/count'
-require 'by_star/calculations/sum'
+require 'by_star/by_direction'
+require 'by_star/by_year'
+require 'by_star/by_month'
+require 'by_star/by_fortnight'
+require 'by_star/by_week'
+require 'by_star/by_weekend'
+require 'by_star/by_day'
-require 'by_star/calculations'
-
module ByStar
- def self.included(base)
- base.extend ClassMethods
- base.send(:include, InstanceMethods)
- base.class_eval do
- def self.by_star_field(value=nil)
- @by_star_field ||= value
- @by_star_field || "#{self.table_name}.created_at"
- end
- end
+ def by_star_field(field=nil)
+ @by_star_field ||= field
+ @by_star_field || "created_at"
end
- module ClassMethods
- include RangeCalculations
- include Shared
- include Vanilla
- include Calculations
+ include ByDirection
+ include ByYear
+ include ByMonth
+ include ByFortnight
+ include ByWeek
+ include ByWeekend
+ include ByDay
+
+ class ParseError < StandardError
+
end
- module InstanceMethods
- include Neighbours
+ # Returns all records between a given start and finish time.
+ #
+ # Currently only supports Time objects.
+ def between(start, finish, options={})
+ field = options[:field] || by_star_field
+ scope = where("#{field} >= ? AND #{field} <= ?",
+ start, finish)
+ scope = scope.order(options[:order]) if options[:order]
+ scope
end
- class ParseError < Exception; end
- class MonthNotFound < Exception; end
+ private
+
+ # Used inside the by_* methods to determine what kind of object "time" is.
+ # These methods take the result of the time_klass method, and call other methods
+ # using it, such as by_year_Time and by_year_String.
+ def time_klass(time)
+ case time
+ when ActiveSupport::TimeWithZone
+ Time
+ else
+ time.class
+ end
+ end
+
end
-ActiveRecord::Base.send :include, ByStar
-ActiveRecord::Relation.send :include, ByStar if ActiveRecord.const_defined?("Relation")
\ No newline at end of file
+ActiveRecord::Base.send :extend, ByStar
+ActiveRecord::Relation.send :extend, ByStar
+
+ActiveRecord::Base.send :include, ByStar::InstanceMethods