birthday 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,23 +8,33 @@ After the gem has been properly tested, it will be released on RubyGems, and wil
8
8
 
9
9
  gem install birthday
10
10
 
11
+ or in your Gemfile:
12
+
13
+ gem 'birthday', '~> 0.1.0'
14
+
11
15
  ## Synopsis
12
16
 
13
17
  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
18
 
15
19
  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
20
 
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
21
+ module Railslove
22
+ module Acts
23
+ module Birthday
24
+ module Adapter
25
+ class SqliteAdapter
26
+ def self.scope_hash(field, date_start, date_end)
27
+ # do some magic and return scope hash you can use
28
+ # field is the field used in the database
29
+ # date_start and date_end can be anything that responds to .to_date method
30
+ end
31
+ end
32
+ end
33
+ end
22
34
  end
23
35
  end
24
36
 
25
- and then create an initializer file with this content, referencing the class you wrote:
26
-
27
- ::Railslove::Acts::Birthday::BaseAdapter.birthday_adapter = SqliteAdapter
37
+ With this namespacing (`Railslove::Acts::Birthday::Adapter`) and naming the class after `active_record`'s ORM adapter (like `SqliteAdapter` in the example above) you can automatically use your own adapters with this gem.
28
38
 
29
39
  ### To do
30
40
 
@@ -46,6 +56,7 @@ Copyright (c) 2011 Railslove
46
56
  ## Main contributors
47
57
 
48
58
  * [@Holek (Mike Połtyn)](http://github.com/Holek)
59
+ * [@Bumi (Michael Bumann)](http://github.com/bumi)
49
60
 
50
61
  ## License
51
62
 
@@ -1,8 +1,4 @@
1
1
  require "railslove/acts/birthday/birthday"
2
- require "railslove/acts/birthday/adapters/mysql_adapter"
3
- require "railslove/acts/birthday/adapters/mysql2_adapter"
4
- require "railslove/acts/birthday/adapters/postgresql_adapter"
5
2
  require "railslove/acts/birthday/version"
6
3
 
7
4
  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
@@ -0,0 +1,17 @@
1
+ # Birthday
2
+ module Railslove
3
+ module Acts #:nodoc:
4
+ module Birthday #:nodoc:
5
+ module Adapter
6
+ autoload :MysqlAdapter, 'railslove/acts/birthday/adapter/mysql_adapter'
7
+ autoload :Mysql2Adapter, 'railslove/acts/birthday/adapter/mysql2_adapter'
8
+ autoload :PostgreSQLAdapter, 'railslove/acts/birthday/adapter/postgresql_adapter'
9
+
10
+ def self.adapter_for(connection)
11
+ "Railslove::Acts::Birthday::Adapter::#{connection.class.name.demodulize}".constantize
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ # Birthday
2
+ require 'railslove/acts/birthday/adapter/mysql_adapter'
3
+ module Railslove
4
+ module Acts #:nodoc:
5
+ module Birthday #:nodoc:
6
+ module Adapter
7
+ class Mysql2Adapter < MysqlAdapter
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,28 @@
1
+ # Birthday
2
+ module Railslove
3
+ module Acts #:nodoc:
4
+ module Birthday #:nodoc:
5
+ module Adapter
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')}\""
11
+ 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
21
+ end
22
+ { :conditions => where_sql }
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ # Birthday
2
+ module Railslove
3
+ module Acts #:nodoc:
4
+ module Birthday #:nodoc:
5
+ module Adapter
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')}'"
11
+ 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
19
+ end
20
+ { :conditions => where_sql }
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -3,16 +3,6 @@ module Railslove
3
3
  module Acts #:nodoc:
4
4
  module Birthday #:nodoc:
5
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
-
16
6
  def self.included(base)
17
7
  base.extend ClassMethods
18
8
  end
@@ -35,8 +25,7 @@ module Railslove
35
25
  # person.birthday_today? => true/false
36
26
  # person.created_at_age => 2
37
27
  # person.created_at_today? => true/false
38
- def acts_as_birthday(*args)
39
- birthday_fields = args.to_a.flatten.compact.map(&:to_sym)
28
+ def acts_as_birthday(*birthday_fields)
40
29
 
41
30
  scope_method = ActiveRecord::VERSION::MAJOR == 3 ? 'scope' : 'named_scope'
42
31
 
@@ -44,7 +33,7 @@ module Railslove
44
33
  self.send(scope_method, :"find_#{field.to_s.pluralize}_for", lambda{ |*scope_args|
45
34
  raise ArgumentError if scope_args.empty? or scope_args.size > 2
46
35
  date_start, date_end = *scope_args
47
- ::Railslove::Acts::Birthday::BaseAdapter.birthday_adapter.scope_hash(field, date_start, date_end)
36
+ ::Railslove::Acts::Birthday::Adapter.adapter_for(self.connection).scope_hash(field, date_start, date_end)
48
37
  })
49
38
 
50
39
  class_eval %{
@@ -1,7 +1,7 @@
1
1
  module Railslove
2
2
  module Acts
3
3
  module Birthday
4
- VERSION = "0.0.2"
4
+ VERSION = "0.1.0"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 2
10
- version: 0.0.2
10
+ version: 0.1.0
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-11-06 00:00:00 Z
18
+ date: 2011-11-18 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rake
@@ -150,9 +150,10 @@ files:
150
150
  - Rakefile
151
151
  - birthday.gemspec
152
152
  - lib/birthday.rb
153
- - lib/railslove/acts/birthday/adapters/mysql2_adapter.rb
154
- - lib/railslove/acts/birthday/adapters/mysql_adapter.rb
155
- - lib/railslove/acts/birthday/adapters/postgresql_adapter.rb
153
+ - lib/railslove/acts/birthday/adapter.rb
154
+ - lib/railslove/acts/birthday/adapter/mysql2_adapter.rb
155
+ - lib/railslove/acts/birthday/adapter/mysql_adapter.rb
156
+ - lib/railslove/acts/birthday/adapter/postgresql_adapter.rb
156
157
  - lib/railslove/acts/birthday/birthday.rb
157
158
  - lib/railslove/acts/birthday/version.rb
158
159
  - spec/birthday_spec.rb
@@ -1,9 +0,0 @@
1
- # Birthday
2
- module Railslove
3
- module Acts #:nodoc:
4
- module Birthday #:nodoc:
5
- class Mysql2Adapter < MysqlAdapter
6
- end
7
- end
8
- end
9
- end
@@ -1,26 +0,0 @@
1
- # Birthday
2
- module Railslove
3
- module Acts #:nodoc:
4
- module Birthday #:nodoc:
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\")"
17
- else
18
- where_sql = "DATE_FORMAT(`#{field}`, '%m%d') >= \"#{date_start.strftime('%m%d')}\" AND DATE_FORMAT(`#{field}`, '%m%d') <= \"#{date_end.strftime('%m%d')}\""
19
- end
20
- end
21
- { :conditions => where_sql }
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,24 +0,0 @@
1
- # Birthday
2
- module Railslove
3
- module Acts #:nodoc:
4
- module Birthday #:nodoc:
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'"
15
- else
16
- where_sql = "to_char(\"#{field}\", 'MMDD') BETWEEN '#{date_start.strftime('%m%d')}' AND '#{date_end.strftime('%m%d')}'"
17
- end
18
- end
19
- { :conditions => where_sql }
20
- end
21
- end
22
- end
23
- end
24
- end