activechronology 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 15a441e0563626217ce123e1c0d7cee083d229a3
4
- data.tar.gz: 75f8be7c5f250732a2a7ffb6b849508cd554f8e5
3
+ metadata.gz: ce0d3396fa9c9ac5ec7772499e21034e0ecf4cf8
4
+ data.tar.gz: ba6d46bdb6ec231ad65050ed9263bb357023489b
5
5
  SHA512:
6
- metadata.gz: dd738e5e827a9540c22ea01386427ff1b15f2ed760830626f9feca80520d97db20c0447932e51130ce83842a4619fec9d0d02f996efa481211f00a82b918de42
7
- data.tar.gz: d81e201d081c17e4d9cc5065b995e8e69e71c122f4d9c17204425e138cef38d620cba6bfb7a7dab1aba76f473131a1f33a245a9f4d6f14315cff4623b682b609
6
+ metadata.gz: 35b9f7859af8b3516b9ebc2d4e09f995134c186c1e20ece91f7b879a42f9db769a669088160c43c4c2b6a5d42b71593ddffe968d9df7c655ac4a8cc9158c60a1
7
+ data.tar.gz: 1ca413b3cad3bb5af90fbf07bd2e3774937e1e421aa1b1d75030dfaaab0a37637f8043dfc1075134711884be6ee8de8e351ac591e81ff71f1cadf6492a14b363
data/README.md CHANGED
@@ -1,15 +1,13 @@
1
- # Activechronology
1
+ # ActiveChronology
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/activechronology`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Easily scope your ActiveRecord models by timestamps or dates.
6
4
 
7
5
  ## Installation
8
6
 
9
7
  Add this line to your application's Gemfile:
10
8
 
11
9
  ```ruby
12
- gem 'activechronology'
10
+ gem 'activechronology', require: 'active_chronology'
13
11
  ```
14
12
 
15
13
  And then execute:
@@ -22,7 +20,31 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ### `set_chronology`
24
+
25
+ From your ActiveRecord models, you can add `chronological` and `reverse_chronological`
26
+ scopes by calling `set_chronology`. This accepts an attribute name as a parameter,
27
+ and it defaults to `created_at`.
28
+
29
+ ### `scope_by_timestamp`
30
+
31
+ Calling `scope_by_timestamp` with a list of attribute names, you get the following
32
+ methods defined, using `:created_at` as an example:
33
+
34
+ ```
35
+ created_after(time)
36
+ created_before(time)
37
+ created_between(start time, end time)
38
+ ```
39
+
40
+ Here's an example class:
41
+
42
+ ```ruby
43
+ class User
44
+ set_chronology :last_seen_at
45
+ scope_by_timestamp :last_seen_at
46
+ end
47
+ ```
26
48
 
27
49
  ## Development
28
50
 
@@ -38,4 +60,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
38
60
  ## License
39
61
 
40
62
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.11"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "sqlite3"
24
25
 
25
26
  spec.add_dependency "rails", "> 3"
26
27
  end
@@ -14,19 +14,32 @@ module ActiveChronology
14
14
  attributes.each do |attribute|
15
15
  name = attribute.to_s.sub(/_(at|on|time|date)$/, '')
16
16
 
17
- scope "#{name}_after", single_time_scope(attribute, '>=')
18
- scope "#{name}_before", single_time_scope(attribute, '<=')
19
- scope "#{name}_between", -> (start_time, end_time) { where(attribute => start_time..end_time) }
17
+ scope "#{name}_after", single_time_scope(attribute, '>')
18
+ scope "#{name}_before", single_time_scope(attribute, '<')
19
+ scope "#{name}_between", between_time_scope(attribute)
20
20
  end
21
21
  end
22
22
 
23
23
  private
24
24
 
25
- def single_time_scope(attribute, operator)
26
- -> (time) do
25
+ def single_time_scope(attribute, base_operator)
26
+ -> (time, options = {}) do
27
+ operator = options[:exclusive] ? base_operator : "#{base_operator}="
28
+
27
29
  time.present? ? where("#{table_name}.#{attribute} #{operator} ?", time) : all
28
30
  end
29
31
  end
32
+
33
+ def between_time_scope(attribute)
34
+ -> (start_time, end_time, options = {}) do
35
+ if options[:exclusive]
36
+ where(
37
+ "#{table_name}.#{attribute} > ? AND #{table_name}.#{attribute} < ?", start_time, end_time)
38
+ else
39
+ where(attribute => start_time..end_time)
40
+ end
41
+ end
42
+ end
30
43
  end
31
44
  end
32
45
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveChronology
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activechronology
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Edson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-03-10 00:00:00.000000000 Z
12
+ date: 2017-02-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -53,6 +53,20 @@ dependencies:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
55
  version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: sqlite3
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
56
70
  - !ruby/object:Gem::Dependency
57
71
  name: rails
58
72
  requirement: !ruby/object:Gem::Requirement