birthday 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Birthday
1
+ # Birthday [![Build Status](https://secure.travis-ci.org/railslove/birthday.png)](http://travis-ci.org/railslove/birthday)
2
2
 
3
3
  This is a small gem that hooks into ActiveRecord and allows to tag a database field (date or datetime) as birthday, allowing to find birthdays with ease.
4
4
 
@@ -12,11 +12,23 @@ After the gem has been properly tested, it will be released on RubyGems, and wil
12
12
 
13
13
  Read [a blog post about the gem](http://blog.railslove.com/2011/10/17/birthday-gem-easy-anniversaries-handling-ruby/) at Railslove blog to get a comprehensive guide to usage of this gem.
14
14
 
15
+ You can create your own adapters for the ORM adapters we're not supporting yet by writing a class with a class method `scope_hash`, which will return a hash normally used in `active_record` scopes.
16
+
17
+ class SqliteAdapter
18
+ def self.scope_hash(field, date_start, date_end)
19
+ # do some magic and return scope hash you can use
20
+ # field is the field used in the database
21
+ # date_start and date_end can be anything that responds to .to_date method
22
+ end
23
+ end
24
+
25
+ and then create an initializer file with this content, referencing the class you wrote:
26
+
27
+ ::Railslove::Acts::Birthday::BaseAdapter.birthday_adapter = SqliteAdapter
28
+
15
29
  ### To do
16
30
 
17
- * Test PostgreSQL
18
31
  * kick class_eval?
19
- * make tests more "aware" of environment
20
32
 
21
33
  ## Note on Patches/Pull Requests
22
34
 
@@ -55,4 +67,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
55
67
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
56
68
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
57
69
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
58
- THE SOFTWARE.
70
+ THE SOFTWARE.
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency "rake"
23
23
  s.add_development_dependency "rspec"
24
24
  s.add_development_dependency "mysql2", '0.3.6'
25
+ s.add_development_dependency "mysql"
25
26
  s.add_development_dependency "delorean"
26
27
  s.add_development_dependency "pg"
27
28
  s.add_runtime_dependency "activerecord"
@@ -4,4 +4,5 @@ require "railslove/acts/birthday/adapters/mysql2_adapter"
4
4
  require "railslove/acts/birthday/adapters/postgresql_adapter"
5
5
  require "railslove/acts/birthday/version"
6
6
 
7
- ActiveRecord::Base.send :include, ::Railslove::Acts::Birthday
7
+ ActiveRecord::Base.send :include, ::Railslove::Acts::Birthday
8
+ ::Railslove::Acts::Birthday::BaseAdapter.birthday_adapter = "Railslove::Acts::Birthday::#{ActiveRecord::Base.connection.class.name.split("::").last}".constantize
@@ -2,10 +2,8 @@
2
2
  module Railslove
3
3
  module Acts #:nodoc:
4
4
  module Birthday #:nodoc:
5
- module Adapters
6
- class Mysql2Adapter < MysqlAdapter
7
- end
5
+ class Mysql2Adapter < MysqlAdapter
8
6
  end
9
7
  end
10
8
  end
11
- end
9
+ end
@@ -2,27 +2,25 @@
2
2
  module Railslove
3
3
  module Acts #:nodoc:
4
4
  module Birthday #:nodoc:
5
- module Adapters
6
- class MysqlAdapter
7
- def self.scope_hash(field, date_start, date_end)
8
- date_start = date_start.to_date
9
- if ((date_end.respond_to?(:empty?) && date_end.empty?) || !date_end)
10
- where_sql = "DATE_FORMAT(`#{field}`, '%m%d') = \"#{date_start.strftime('%m%d')}\""
5
+ class MysqlAdapter < BaseAdapter
6
+ def self.scope_hash(field, date_start, date_end)
7
+ date_start = date_start.to_date
8
+ if ((date_end.respond_to?(:empty?) && date_end.empty?) || !date_end)
9
+ where_sql = "DATE_FORMAT(`#{field}`, '%m%d') = \"#{date_start.strftime('%m%d')}\""
10
+ else
11
+ date_end = date_end.to_date
12
+ if date_end.strftime('%m%d') < date_start.strftime('%m%d')
13
+ where_sql = "(DATE_FORMAT(`#{field}`, '%m%d') >= \"0101\""
14
+ where_sql << " AND DATE_FORMAT(`#{field}`, '%m%d') <= \"#{date_start.strftime('%m%d')}\")"
15
+ where_sql << " OR (DATE_FORMAT(`#{field}`, '%m%d') >= \"#{date_end.strftime('%m%d')}\""
16
+ where_sql << " AND DATE_FORMAT(`#{field}`, '%m%d') <= \"1231\")"
11
17
  else
12
- date_end = date_end.to_date
13
- if date_end.strftime('%m%d') < date_start.strftime('%m%d')
14
- where_sql = "(DATE_FORMAT(`#{field}`, '%m%d') >= \"0101\""
15
- where_sql << " AND DATE_FORMAT(`#{field}`, '%m%d') <= \"#{date_start.strftime('%m%d')}\")"
16
- where_sql << " OR (DATE_FORMAT(`#{field}`, '%m%d') >= \"#{date_end.strftime('%m%d')}\""
17
- where_sql << " AND DATE_FORMAT(`#{field}`, '%m%d') <= \"1231\")"
18
- else
19
- where_sql = "DATE_FORMAT(`#{field}`, '%m%d') >= \"#{date_start.strftime('%m%d')}\" AND DATE_FORMAT(`#{field}`, '%m%d') <= \"#{date_end.strftime('%m%d')}\""
20
- end
18
+ where_sql = "DATE_FORMAT(`#{field}`, '%m%d') >= \"#{date_start.strftime('%m%d')}\" AND DATE_FORMAT(`#{field}`, '%m%d') <= \"#{date_end.strftime('%m%d')}\""
21
19
  end
22
- { :conditions => where_sql }
23
20
  end
21
+ { :conditions => where_sql }
24
22
  end
25
23
  end
26
24
  end
27
25
  end
28
- end
26
+ end
@@ -2,25 +2,23 @@
2
2
  module Railslove
3
3
  module Acts #:nodoc:
4
4
  module Birthday #:nodoc:
5
- module Adapters
6
- class PostgreSQLAdapter
7
- def self.scope_hash(field, date_start, date_end)
8
- date_start = date_start.to_date
9
- if ((date_end.respond_to?(:empty?) && date_end.empty?) || !date_end)
10
- where_sql = "to_char(\"#{field}\", 'MMDD') = '#{date_start.strftime('%m%d')}'"
5
+ class PostgreSQLAdapter < BaseAdapter
6
+ def self.scope_hash(field, date_start, date_end)
7
+ date_start = date_start.to_date
8
+ if ((date_end.respond_to?(:empty?) && date_end.empty?) || !date_end)
9
+ where_sql = "to_char(\"#{field}\", 'MMDD') = '#{date_start.strftime('%m%d')}'"
10
+ else
11
+ date_end = date_end.to_date
12
+ if date_end.strftime('%m%d') < date_start.strftime('%m%d')
13
+ where_sql = "to_char(\"#{field}\", 'MMDD') BETWEEN '0101' AND '#{date_end.strftime('%m%d')}'"
14
+ where_sql << "OR to_char(\"#{field}\", 'MMDD') BETWEEN '#{date_start.strftime('%m%d')}' AND '1231'"
11
15
  else
12
- date_end = date_end.to_date
13
- if date_end.strftime('%m%d') < date_start.strftime('%m%d')
14
- where_sql = "to_char(\"#{field}\", 'MMDD') BETWEEN '0101' AND '#{date_end.strftime('%m%d')}'"
15
- where_sql << "OR to_char(\"#{field}\", 'MMDD') BETWEEN '#{date_start.strftime('%m%d')}' AND '1231'"
16
- else
17
- where_sql = "to_char(\"#{field}\", 'MMDD') BETWEEN '#{date_start.strftime('%m%d')}' AND '#{date_end.strftime('%m%d')}'"
18
- end
16
+ where_sql = "to_char(\"#{field}\", 'MMDD') BETWEEN '#{date_start.strftime('%m%d')}' AND '#{date_end.strftime('%m%d')}'"
19
17
  end
20
- { :conditions => where_sql }
21
18
  end
19
+ { :conditions => where_sql }
22
20
  end
23
21
  end
24
22
  end
25
23
  end
26
- end
24
+ end
@@ -2,22 +2,24 @@
2
2
  module Railslove
3
3
  module Acts #:nodoc:
4
4
  module Birthday #:nodoc:
5
+
6
+ class BaseAdapter
7
+ def self.birthday_adapter
8
+ @@birthday_adapter
9
+ end
10
+
11
+ def self.birthday_adapter=(value)
12
+ @@birthday_adapter = value
13
+ end
14
+ end
15
+
5
16
  def self.included(base)
6
17
  base.extend ClassMethods
7
18
  end
8
19
 
9
20
  module ClassMethods
10
21
 
11
- # Expects an array of date or datetime fields. If the first object
12
- # in passed array is a class with a class method `scope_hash`
13
- # class SqliteAdapter
14
- # def self.scope_hash(field, date_start, date_end)
15
- # # do some magic and return scope hash you can use
16
- # end
17
- # end
18
- #
19
- # then this class is used as an adapter to create its scope for birthday finders.
20
- #
22
+ # Expects an array of date or datetime fields.
21
23
  # An example code in basic model would be:
22
24
  #
23
25
  # class Person < ActiveRecord::Base
@@ -34,15 +36,7 @@ module Railslove
34
36
  # person.created_at_age => 2
35
37
  # person.created_at_today? => true/false
36
38
  def acts_as_birthday(*args)
37
- args = args.to_a.flatten.compact
38
- klass = args.shift if args.first.class == Class
39
- if klass && klass.class == Class
40
- @@_birthday_backend = klass
41
- else
42
- @@_birthday_backend ||= "Railslove::Acts::Birthday::Adapters::#{ActiveRecord::Base.connection.class.name.split("::").last}".constantize
43
- end
44
-
45
- birthday_fields = args.map(&:to_sym)
39
+ birthday_fields = args.to_a.flatten.compact.map(&:to_sym)
46
40
 
47
41
  scope_method = ActiveRecord::VERSION::MAJOR == 3 ? 'scope' : 'named_scope'
48
42
 
@@ -50,7 +44,7 @@ module Railslove
50
44
  self.send(scope_method, :"find_#{field.to_s.pluralize}_for", lambda{ |*scope_args|
51
45
  raise ArgumentError if scope_args.empty? or scope_args.size > 2
52
46
  date_start, date_end = *scope_args
53
- @@_birthday_backend.scope_hash(field, date_start, date_end)
47
+ ::Railslove::Acts::Birthday::BaseAdapter.birthday_adapter.scope_hash(field, date_start, date_end)
54
48
  })
55
49
 
56
50
  class_eval %{
@@ -73,4 +67,4 @@ module Railslove
73
67
 
74
68
  end
75
69
  end
76
- end
70
+ end
@@ -1,7 +1,7 @@
1
1
  module Railslove
2
2
  module Acts
3
3
  module Birthday
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
6
6
  end
7
7
  end
@@ -1,5 +1,5 @@
1
1
  mysql:
2
- adapter: mysql2
2
+ adapter: mysql
3
3
  database: birthday_plugin_test
4
4
  username:
5
5
  encoding: utf8
@@ -1,16 +1,20 @@
1
1
  $:.unshift(File.dirname(__FILE__))
2
2
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
 
4
- require 'test/unit'
5
4
  require 'rubygems'
6
5
  require 'rspec'
6
+ require 'delorean'
7
+ require 'yaml'
7
8
 
8
- def fixture(file_name)
9
- File.read(fixture_path(file_name))
10
- end
11
-
12
- def fixture_path(file_name)
13
- File.expand_path(File.dirname(__FILE__)) + "/fixtures/#{file_name}"
9
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
10
+ adapter = ENV['DB'] || 'mysql'
11
+ case adapter
12
+ when 'mysql'
13
+ require 'mysql2'
14
+ when 'postgres'
15
+ require 'pg'
16
+ else
17
+ require adapter
14
18
  end
15
19
 
16
20
  if ENV['RAILS'].nil?
@@ -20,31 +24,18 @@ else
20
24
  # load activerecord and activesupport and plugin manually
21
25
  require 'active_record'
22
26
  require 'active_support'
27
+ ActiveRecord::Base.configurations.update config
28
+ ActiveRecord::Base.establish_connection(adapter)
29
+ ActiveRecord::Base.default_timezone = :utc
23
30
  $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
24
- Dir["#{$LOAD_PATH.last}/**/*.rb"].each do |path|
31
+ Dir["#{$LOAD_PATH.last}/**/*.rb"].each do |path|
25
32
  require path[$LOAD_PATH.last.size + 1..-1]
26
33
  end
27
34
  require "railslove/acts/birthday/birthday"
28
35
  end
29
- require 'delorean'
30
- require 'yaml'
31
- config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
32
- adapter = ENV['DB'] || 'mysql'
33
- case adapter
34
- when 'mysql'
35
- require 'mysql2'
36
- when 'postgres'
37
- require 'pg'
38
- else
39
- require adapter
40
- end
41
-
42
- ActiveRecord::Base.configurations.update config
43
- ActiveRecord::Base.establish_connection(adapter)
44
- ActiveRecord::Base.default_timezone = :utc
45
36
 
46
37
  load(File.dirname(__FILE__) + "/schema.rb")
47
38
 
48
39
  RSpec.configure do |config|
49
40
  config.include Delorean
50
- end
41
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: birthday
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Mike Po\xC5\x82tyn"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-28 00:00:00 Z
18
+ date: 2011-11-06 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rake
@@ -62,7 +62,7 @@ dependencies:
62
62
  type: :development
63
63
  version_requirements: *id003
64
64
  - !ruby/object:Gem::Dependency
65
- name: delorean
65
+ name: mysql
66
66
  prerelease: false
67
67
  requirement: &id004 !ruby/object:Gem::Requirement
68
68
  none: false
@@ -76,7 +76,7 @@ dependencies:
76
76
  type: :development
77
77
  version_requirements: *id004
78
78
  - !ruby/object:Gem::Dependency
79
- name: pg
79
+ name: delorean
80
80
  prerelease: false
81
81
  requirement: &id005 !ruby/object:Gem::Requirement
82
82
  none: false
@@ -90,7 +90,7 @@ dependencies:
90
90
  type: :development
91
91
  version_requirements: *id005
92
92
  - !ruby/object:Gem::Dependency
93
- name: activerecord
93
+ name: pg
94
94
  prerelease: false
95
95
  requirement: &id006 !ruby/object:Gem::Requirement
96
96
  none: false
@@ -101,10 +101,10 @@ dependencies:
101
101
  segments:
102
102
  - 0
103
103
  version: "0"
104
- type: :runtime
104
+ type: :development
105
105
  version_requirements: *id006
106
106
  - !ruby/object:Gem::Dependency
107
- name: activesupport
107
+ name: activerecord
108
108
  prerelease: false
109
109
  requirement: &id007 !ruby/object:Gem::Requirement
110
110
  none: false
@@ -117,6 +117,20 @@ dependencies:
117
117
  version: "0"
118
118
  type: :runtime
119
119
  version_requirements: *id007
120
+ - !ruby/object:Gem::Dependency
121
+ name: activesupport
122
+ prerelease: false
123
+ requirement: &id008 !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ hash: 3
129
+ segments:
130
+ - 0
131
+ version: "0"
132
+ type: :runtime
133
+ version_requirements: *id008
120
134
  description: Birthday gem creates convienent methods for date and datetime fields in ActiveRecord, making it possible to look for birthdays without a hassle.
121
135
  email:
122
136
  - mike@railslove.com