blind_date 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ari Epstein
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,20 @@
1
+ = blind_date
2
+
3
+ Blind Date is a utility which provides a DBMS-independent API to exploit basic
4
+ date manipulation functionality present in each database.
5
+
6
+ == Note on Patches/Pull Requests
7
+
8
+ * Fork the project.
9
+ * Make your feature addition or bug fix.
10
+ * Add tests for it. This is important so I don't break it in a
11
+ future version unintentionally.
12
+ * Commit, do not mess with rakefile, version, or history.
13
+ (if you want to have your own version, that is fine but
14
+ bump version in a commit by itself I can ignore when I pull)
15
+ * Send me a pull request. Bonus points for topic branches.
16
+
17
+ == Copyright
18
+
19
+ Copyright (c) 2009 Ari Epstein. See LICENSE for details.
20
+
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "blind_date"
8
+ gem.summary = %Q{DBMS-indepedent methods to query dates in ActiveRecord}
9
+ gem.description = %Q{Allows you to utilize date calculation functionality of DBMS with a uniform API}
10
+ gem.email = "aepstein607@gmail.com"
11
+ gem.homepage = "http://github.com/aepstein/blind_date"
12
+ gem.authors = ["Ari Epstein"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_dependency "activerecord", ">= 2.3.4"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "blind_date #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
47
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{blind_date}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ari Epstein"]
12
+ s.date = %q{2009-11-25}
13
+ s.description = %q{Allows you to utilize date calculation functionality of DBMS with a uniform API}
14
+ s.email = %q{aepstein607@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "blind_date.gemspec",
27
+ "lib/blind_date.rb",
28
+ "lib/blind_date/active_record/base.rb",
29
+ "lib/blind_date/active_record/connection_adapters/abstract_adapter.rb",
30
+ "lib/blind_date/active_record/connection_adapters/mysql_adapter.rb",
31
+ "lib/blind_date/active_record/connection_adapters/postgresql_adapter.rb",
32
+ "lib/blind_date/active_record/connection_adapters/sqlite3_adapter.rb",
33
+ "lib/blind_date/active_record/connection_adapters/sqlite_adapter.rb",
34
+ "spec/blind_date_spec.rb",
35
+ "spec/spec.opts",
36
+ "spec/spec_helper.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/aepstein/blind_date}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.5}
42
+ s.summary = %q{DBMS-indepedent methods to query dates in ActiveRecord}
43
+ s.test_files = [
44
+ "spec/blind_date_spec.rb",
45
+ "spec/spec_helper.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
53
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
54
+ s.add_runtime_dependency(%q<activerecord>, [">= 2.3.4"])
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
57
+ s.add_dependency(%q<activerecord>, [">= 2.3.4"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
61
+ s.add_dependency(%q<activerecord>, [">= 2.3.4"])
62
+ end
63
+ end
64
+
@@ -0,0 +1,43 @@
1
+ module BlindDate
2
+ module ActiveRecord
3
+ module Base
4
+
5
+ # Generates an sql fragement representing the sum of a date and a duration
6
+ # == Usage
7
+ #
8
+ # Model.date_add DateTime.now, 10.days
9
+ # => sqlite: DATETIME( '2009-11-25 12:01:01', '+10 DAY' )
10
+ # => mysql: ('2009-11-25 12:01:01' + 10 DAY)
11
+ # => pgsql: (TIMESTAMP '2009-11-25 12:01:01' + '10 DAY')
12
+ # Model.date_add 'models.starts_at', 10.days
13
+ # => sqlite: DATETIME( models.starts_at, '+10 DAY' )
14
+ # Model.date_add :starts_at, :duration, :days
15
+ # => sqlite: DATETIME( "models"."starts_at", '+' || "models"."duration" || ' DAY' )
16
+ # Model.date_add :starts_at, :duration, :days, true
17
+ # => sqlite: DATETIME( "models"."starts_at", '-' || "models"."duration" || ' DAY' )
18
+ #
19
+ # == Options
20
+ # * +date+ - Date subject to calculation, can be date, time, datetime, symbolized column, or string sql fragment
21
+ # * +interval+ - Duration to be added/subtracted, can be duration, number, symbolized column, or string sql fragment
22
+ # * +unit+ - Unit of time for duration, defaults to :seconds, and is ignored if using duration for interval
23
+ # * +diff+ - If true, will subtract duration from date, otherwise will add (default is false)
24
+ def date_add( date, interval, unit = 'SECOND', diff = false)
25
+ if date.is_a? Symbol
26
+ date = "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(date.to_s)}"
27
+ elsif date.acts_like?(:time) || date.acts_like?(:date)
28
+ date = connection.quote date
29
+ end
30
+ interval = "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(interval.to_s)}" if interval.is_a? Symbol
31
+ connection.class.date_add date, interval, unit, diff
32
+ end
33
+
34
+ # Generates a SQL fragment representing subtraction of a duration from a date
35
+ # Similar to date_add( date, interval, unit, +true+ )
36
+ def date_sub( date, interval, unit = 'SECOND' )
37
+ date_add date, interval, unit, true
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+
@@ -0,0 +1,56 @@
1
+ module BlindDate
2
+ module ActiveRecord
3
+ module ConnectionAdapters
4
+
5
+ # Includes extended behaviors
6
+ # Each DBMS-specific module must implement the private methods or raise NotImplemented
7
+ # for each private method
8
+ module AbstractAdapter
9
+
10
+ # Performs arithmetic on string is an SQL-escaped date/time/datetime or returns a date/time/datetime
11
+ def date_add(date, interval, unit = 'SECOND', diff = false )
12
+ case interval
13
+ when ActiveSupport::Duration
14
+ interval.parts.inject( date ) do |memo, span|
15
+ date_add_sql_without_operator memo, span.last, span.first.to_s.singularize.upcase, diff
16
+ end
17
+ when String
18
+ date_add_sql date, interval, unit.to_s.singularize.upcase, ( diff ? '-' : '+' )
19
+ when Numeric
20
+ date_add_sql_without_operator date, interval, unit.to_s.singularize.upcase, diff
21
+ else
22
+ raise ArgumentError
23
+ end
24
+ end
25
+
26
+ # Returns name of adapter suitable for loading related classes
27
+ # Should have same output as instance method of same name
28
+ # TODO: Kludgy: maybe upstream could provide a class method?
29
+ def adapter_name
30
+ to_s.underscore[ /\/([^\/]+)_adapter$/, 1 ].gsub( '_', '' )
31
+ end
32
+
33
+ private
34
+
35
+ # Adds appropriate operator before calling adapter-specific method
36
+ def date_add_sql_without_operator( sql, interval, unit, diff )
37
+ if diff
38
+ date_add_sql sql, interval.abs, unit, ( interval < 0 ? '+' : '-' )
39
+ else
40
+ date_add_sql sql, interval.abs, unit, ( interval < 0 ? '-' : '+' )
41
+ end
42
+ end
43
+
44
+ # Will attempt to load the correct behavior if this is not already provided
45
+ def date_add_sql( sql, interval, unit, operator )
46
+ require "blind_date/active_record/connection_adapters/#{adapter_name}_adapter"
47
+ date_add_sql( sql, interval, unit, operator )
48
+ end
49
+
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.extend BlindDate::ActiveRecord::ConnectionAdapters::AbstractAdapter
56
+
@@ -0,0 +1,16 @@
1
+ module BlindDate
2
+ module ActiveRecord
3
+ module ConnectionAdapters
4
+ module MysqlAdapter
5
+
6
+ def date_add_sql( sql, interval, unit, operator )
7
+ "( #{sql} #{operator} INTERVAL #{interval} #{unit} )"
8
+ end
9
+
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.extend BlindDate::ActiveRecord::ConnectionAdapters::MysqlAdapter
16
+
@@ -0,0 +1,23 @@
1
+ module BlindDate
2
+ module ActiveRecord
3
+ module ConnectionAdapters
4
+ module PostgresqlAdapter
5
+
6
+ def date_add_sql( sql, interval, unit, operator )
7
+ case interval
8
+ when Numeric
9
+ "( #{sql} #{operator} '#{interval} #{unit}' )"
10
+ when String
11
+ "( #{sql} #{operator} #{interval} || ' #{unit}')"
12
+ else
13
+ raise ArgumentError
14
+ end
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.send :include, BlindDate::ActiveRecord::ConnectionAdapters::MysqlAdapter
23
+
@@ -0,0 +1,2 @@
1
+ require 'blind_date/active_record/connection_adapters/sqlite_adapter'
2
+
@@ -0,0 +1,23 @@
1
+ module BlindDate
2
+ module ActiveRecord
3
+ module ConnectionAdapters
4
+ module SQLiteAdapter
5
+
6
+ def date_add_sql( sql, interval, unit, operator )
7
+ case interval
8
+ when Numeric
9
+ "DATETIME( #{sql}, '#{operator}#{interval} #{unit}' )"
10
+ when String
11
+ "DATETIME( #{sql}, '#{operator}' || #{interval} || ' #{unit}' )"
12
+ else
13
+ raise ArgumentError
14
+ end
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ ActiveRecord::ConnectionAdapters::SQLiteAdapter.extend BlindDate::ActiveRecord::ConnectionAdapters::SQLiteAdapter
23
+
data/lib/blind_date.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'blind_date/active_record/base'
2
+ require 'blind_date/active_record/connection_adapters/abstract_adapter'
3
+
4
+ ActiveRecord::Base.extend BlindDate::ActiveRecord::Base
5
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.extend BlindDate::ActiveRecord::ConnectionAdapters::AbstractAdapter
6
+
@@ -0,0 +1,76 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "BlindDate" do
4
+ before(:each) do
5
+ @activity = Activity.create( :starts_at => DateTime.now, :duration => 60 )
6
+ @future = Activity.create( :starts_at => @activity.starts_at + 60.seconds, :duration => 60 )
7
+ end
8
+
9
+ it "should produce valid date_add output" do
10
+ matches = matches_for Activity.date_add( @activity.starts_at, 60.seconds )
11
+ matches.size.should eql 1
12
+ matches.should include @future
13
+ end
14
+
15
+ it "should produce valid date_add output with plain sql string" do
16
+ matches = Activity.find( :all, :conditions => [ "#{@activity.connection.quote(@future.starts_at)} = " +
17
+ Activity.date_add( 'activities.starts_at', 60.seconds ) ] )
18
+ matches.size.should eql 1
19
+ matches.should include @activity
20
+ end
21
+
22
+ it "should produce valid date_add output with multipart date" do
23
+ other_activity = Activity.create( :starts_at => @activity.starts_at + 2.months + 10.seconds )
24
+ matches = Activity.find( :all, :conditions => [ "#{@activity.connection.quote(other_activity.starts_at)} = " +
25
+ Activity.date_add( :starts_at, 2.months + 10.seconds ) ])
26
+ matches.size.should eql 1
27
+ matches.should include @activity
28
+ end
29
+
30
+ it "should produce valid date_add output with a symbol string" do
31
+ matches = Activity.find( :all, :conditions => [ "#{@activity.connection.quote(@future.starts_at)} = " +
32
+ Activity.date_add( :starts_at, 60.seconds ) ] )
33
+ matches.size.should eql 1
34
+ matches.should include @activity
35
+ end
36
+
37
+ it "should default to seconds with an integer interval" do
38
+ matches = Activity.find( :all, :conditions => [ "#{@activity.connection.quote(@future.starts_at)} = " +
39
+ Activity.date_add( :starts_at, 60 ) ] )
40
+ matches.size.should eql 1
41
+ matches.should include @activity
42
+ end
43
+
44
+ it "should accept sql fragment, unit, operator for interval" do
45
+ matches = Activity.find( :all, :conditions => [ "#{@activity.connection.quote(@activity.starts_at + @activity.duration.seconds)} = " +
46
+ Activity.date_add( :starts_at, 'activities.duration' ) ] )
47
+ matches.size.should eql 1
48
+ matches.should include @activity
49
+ end
50
+
51
+ it "should accept a symbol for the interval" do
52
+ matches = Activity.find( :all, :conditions => [ "#{@activity.connection.quote(@activity.starts_at + @activity.duration.seconds)} = " +
53
+ Activity.date_add( :starts_at, :duration ) ] )
54
+ matches.size.should eql 1
55
+ matches.should include @activity
56
+ end
57
+
58
+ it "should accept an alternative unit for a specified interval" do
59
+ matches = Activity.find( :all, :conditions => [ "#{@activity.connection.quote(@activity.starts_at + @activity.duration.seconds)} = " +
60
+ Activity.date_add( :starts_at, 1, :minutes ) ] )
61
+ matches.size.should eql 1
62
+ matches.should include @activity
63
+ end
64
+
65
+ it "should calculate a difference with the sub method" do
66
+ matches = Activity.find( :all, :conditions => [ "#{@activity.connection.quote @activity.starts_at} = " +
67
+ Activity.date_sub( 'activities.starts_at', 1, :minutes ) ] )
68
+ matches.size.should eql 1
69
+ matches.should include @future
70
+ end
71
+
72
+ def matches_for(condition)
73
+ Activity.find( :all, :conditions => "starts_at = #{condition}" )
74
+ end
75
+ end
76
+
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'activerecord'
3
+
4
+ ENV['TZ'] = 'UTC'
5
+ Time.zone = 'Eastern Time (US & Canada)'
6
+
7
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
8
+ ActiveRecord::Base.configurations = true
9
+
10
+ ActiveRecord::Schema.verbose = false
11
+ ActiveRecord::Schema.define(:version => 1) do
12
+ create_table :activities do |t|
13
+ t.datetime :starts_at
14
+ t.integer :duration
15
+ end
16
+ end
17
+
18
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
19
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
20
+ require 'blind_date'
21
+ require 'spec'
22
+ require 'spec/autorun'
23
+
24
+ Spec::Runner.configure do |config|
25
+ config.before(:each) do
26
+ class Activity < ActiveRecord::Base
27
+ end
28
+
29
+ Activity.destroy_all
30
+ end
31
+
32
+ config.after(:each) do
33
+ Object.send(:remove_const, :Activity)
34
+ end
35
+ end
36
+
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blind_date
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ari Epstein
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-25 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.4
34
+ version:
35
+ description: Allows you to utilize date calculation functionality of DBMS with a uniform API
36
+ email: aepstein607@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - blind_date.gemspec
52
+ - lib/blind_date.rb
53
+ - lib/blind_date/active_record/base.rb
54
+ - lib/blind_date/active_record/connection_adapters/abstract_adapter.rb
55
+ - lib/blind_date/active_record/connection_adapters/mysql_adapter.rb
56
+ - lib/blind_date/active_record/connection_adapters/postgresql_adapter.rb
57
+ - lib/blind_date/active_record/connection_adapters/sqlite3_adapter.rb
58
+ - lib/blind_date/active_record/connection_adapters/sqlite_adapter.rb
59
+ - spec/blind_date_spec.rb
60
+ - spec/spec.opts
61
+ - spec/spec_helper.rb
62
+ has_rdoc: true
63
+ homepage: http://github.com/aepstein/blind_date
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --charset=UTF-8
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.3.5
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: DBMS-indepedent methods to query dates in ActiveRecord
90
+ test_files:
91
+ - spec/blind_date_spec.rb
92
+ - spec/spec_helper.rb