date_supercharger 0.1.0 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9cbde5991d41f650cdea7c806ad19374ab489464
4
- data.tar.gz: 7feaee3da4b2d798efb969ddec64a168f05128cb
3
+ metadata.gz: 2896b5b0efeff824bef43c94880d0194db738c5e
4
+ data.tar.gz: 4dca365454e6c76764e68c7c2e4eaeda6919e2e2
5
5
  SHA512:
6
- metadata.gz: cabbb1d6e0f9e892ed56c15fd03424b3717a5c746c815e1c620d15f45692b06007ccc1cb296d730b28a7f6b5fa9d86afd8eb5956047ebd6255d82986c99455b0
7
- data.tar.gz: bd635957f1bcf9a25c5eeaf32501781837c3256701704d0eee68d8ab7754e1d09cb282925f7d2658f4f38ba6e8ec2237952baa4f452b70fa6bbaf1e62d4f5f3c
6
+ metadata.gz: 438d4293b37490c687b82a770590f02250f93173021378461b0699cc2a95e7f886b6c5e66d15110b96108c9380e43dc4d8e6b1c5fc52736bdcbaa24a08573014
7
+ data.tar.gz: ca21fc7f5a0529201a384dd2bda0bb59c2d4ebb7636346050b647e6857429c183d637053aaa6cc35b5ad6aca7ac5cc29952e23c5fcb50b46df1d3a735e320845
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A nice shortcut for date queries.
4
4
 
5
- `date_supercharger` adds `_after`, `_after_or_at`, `_before` and `_before_or_at` methods to every date/datetime field of Active Record models.
5
+ `date_supercharger` adds `between`,`between_inclusive`,`_after`, `_after_or_at`, `_before` and `_before_or_at` methods to every date/datetime field of Active Record models.
6
6
 
7
7
  [![Build Status](https://travis-ci.org/simon0191/date_supercharger.svg)](https://travis-ci.org/simon0191/date_supercharger)
8
8
  [![Code Climate](https://codeclimate.com/github/simon0191/date_supercharger/badges/gpa.svg)](https://codeclimate.com/github/simon0191/date_supercharger)
@@ -10,13 +10,33 @@ A nice shortcut for date queries.
10
10
  ## Usage
11
11
 
12
12
  ```ruby
13
- Visit.created_at_after(DateTime.now)
13
+ Visit.created_at_between(from,to)
14
14
  ```
15
15
 
16
16
  instead of
17
17
 
18
18
  ```ruby
19
- Visit.where("created_at > ?",DateTime.now)
19
+ Visit.where("created_at >= ? AND created_at < ?",from,to)
20
+ ```
21
+
22
+ ```ruby
23
+ Visit.created_at_between_inclusive(from,to)
24
+ ```
25
+
26
+ instead of
27
+
28
+ ```ruby
29
+ Visit.where("created_at >= ? AND created_at <= ?",from,to)
30
+ ```
31
+
32
+ ```ruby
33
+ Visit.created_at_after(some_date)
34
+ ```
35
+
36
+ instead of
37
+
38
+ ```ruby
39
+ Visit.where("created_at > ?",some_date)
20
40
  ```
21
41
 
22
42
  ## Installation
@@ -1,4 +1,6 @@
1
1
  require "date_supercharger/version"
2
+ require "date_supercharger/matcher"
3
+ require "date_supercharger/method_definer"
2
4
  require "active_record"
3
5
 
4
6
  module DateSupercharger
@@ -7,30 +9,11 @@ module DateSupercharger
7
9
  included do
8
10
  def self.method_missing(method_sym, *arguments, &block)
9
11
  return super unless descends_from_active_record?
10
- match = ModelDateExtensionMatch.new(self,method_sym)
12
+ matcher = Matcher.new(self,method_sym)
11
13
 
12
- if match.match?
13
- case match.suffix
14
- when :after,:before,:before_or_at,:after_or_at
15
- operators = { after: ">", before: "<", before_or_at: "<=", after_or_at: ">=" }
16
- singleton_class.class_eval do
17
- define_method("#{match.attribute}_#{match.suffix}") do |date|
18
- where("#{match.attribute} #{operators[match.suffix]} ?", date)
19
- end
20
- end
21
- when :between
22
- singleton_class.class_eval do
23
- define_method("#{match.attribute}_#{match.suffix}") do |from,to|
24
- send("#{match.attribute}_after_or_at",from).send("#{match.attribute}_before",to)
25
- end
26
- end
27
- when :between_inclusive
28
- singleton_class.class_eval do
29
- define_method("#{match.attribute}_#{match.suffix}") do |from,to|
30
- send("#{match.attribute}_after_or_at",from).send("#{match.attribute}_before_or_at",to)
31
- end
32
- end
33
- end
14
+ if matcher.match?
15
+ method_definer = MethodDefiner.new(self)
16
+ method_definer.define(attribute: matcher.attribute, suffix: matcher.suffix)
34
17
  send(method_sym, *arguments)
35
18
  else
36
19
  super
@@ -39,34 +22,12 @@ module DateSupercharger
39
22
 
40
23
  def self.respond_to?(method_sym, include_private = false)
41
24
  return super unless descends_from_active_record?
42
- if ModelDateExtensionMatch.new(self,method_sym).match?
25
+ if Matcher.new(self,method_sym).match?
43
26
  true
44
27
  else
45
28
  super
46
29
  end
47
30
  end
48
31
  end
49
-
50
- class ModelDateExtensionMatch
51
-
52
- attr_accessor :attribute,:suffix
53
-
54
- def initialize(model,method_sym)
55
- #TODO: implement between and between_inclusive
56
- if method_sym.to_s =~ /^(.+)_(before|after|before_or_at|after_or_at|between|between_inclusive)$/
57
- date_columns = model.columns_hash.keys.select { |c| [:datetime,:date].include? model.columns_hash[c].type }.map(&:to_sym)
58
- if date_columns.include? $1.to_sym
59
- @attribute = $1.to_sym
60
- @suffix = $2.to_sym
61
- end
62
- end
63
-
64
- end
65
-
66
- def match?
67
- @attribute != nil
68
- end
69
- end
70
-
71
32
  end
72
33
  ActiveRecord::Base.send :include, DateSupercharger
@@ -0,0 +1,30 @@
1
+ module DateSupercharger
2
+ class Matcher
3
+
4
+ attr_accessor :attribute,:suffix
5
+
6
+ def initialize(model,method_sym)
7
+ if method_sym.to_s =~ /^(.+)_(before|after|before_or_at|after_or_at|between|between_inclusive)$/
8
+ date_columns = get_date_columns(model)
9
+
10
+ if date_columns.include? $1.to_sym
11
+ @attribute = $1.to_sym
12
+ @suffix = $2.to_sym
13
+ end
14
+ end
15
+
16
+ end
17
+
18
+ def match?
19
+ @attribute != nil
20
+ end
21
+
22
+ private
23
+
24
+ def get_date_columns(model)
25
+ model.columns_hash.keys.select do |c|
26
+ [:datetime,:date].include? model.columns_hash[c].type
27
+ end.map(&:to_sym)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,36 @@
1
+ module DateSupercharger
2
+ class MethodDefiner
3
+
4
+ attr_reader :klass
5
+
6
+ def initialize(klass)
7
+ @klass = klass
8
+ end
9
+
10
+ def define(opts)
11
+ attribute = opts[:attribute]
12
+ suffix = opts[:suffix]
13
+
14
+ new_method = "#{attribute}_#{suffix}"
15
+ case suffix
16
+ when :after,:before,:before_or_at,:after_or_at
17
+ operators = { after: ">", before: "<", before_or_at: "<=", after_or_at: ">=" }
18
+ klass.singleton_class.class_eval do
19
+ define_method(new_method) do |date|
20
+ where("#{attribute} #{operators[suffix]} ?", date)
21
+ end
22
+ end
23
+ when :between,:between_inclusive
24
+ methods = {between: ["after_or_at","before"],between_inclusive:["after_or_at","before_or_at"]}
25
+ klass.singleton_class.class_eval do
26
+ define_method(new_method) do |from,to|
27
+ from_method = methods[suffix].first
28
+ to_method = methods[suffix].second
29
+ send("#{attribute}_#{from_method}",from).send("#{attribute}_#{to_method}",to)
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ end
36
+ end
@@ -1,3 +1,3 @@
1
1
  module DateSupercharger
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: date_supercharger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Soriano
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-05 00:00:00.000000000 Z
11
+ date: 2015-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -138,6 +138,8 @@ files:
138
138
  - Rakefile
139
139
  - date_supercharger.gemspec
140
140
  - lib/date_supercharger.rb
141
+ - lib/date_supercharger/matcher.rb
142
+ - lib/date_supercharger/method_definer.rb
141
143
  - lib/date_supercharger/version.rb
142
144
  - test/date_supercharger_test.rb
143
145
  - test/test_helper.rb