timeframeable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +44 -0
- data/Rakefile +6 -0
- data/lib/timeframeable.rb +92 -0
- data/lib/timeframeable/version.rb +3 -0
- data/spec/lib/timeframeable_spec.rb +111 -0
- data/spec/spec_helper.rb +4 -0
- data/timeframeable.gemspec +24 -0
- metadata +122 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Alexander Pavlenko
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Timeframeable
|
2
|
+
|
3
|
+
Timeframeable module allows you to easily extract datetime range from request params keeping you controllers DRY.
|
4
|
+
Intended to use with Rails 3, but may be compatible or easily adaptable with/to other versions.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'timeframeable'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install timeframeable
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Inject dates from ```params[:start]``` and ```params[:end]``` or their default values into ```@timeframe.start``` and ```@timeframe.end```:
|
23
|
+
|
24
|
+
timeframeable :defaults => [:beginning_of_month, :now]
|
25
|
+
|
26
|
+
Inject dates from ```params[:s]``` and ```params[:e]``` or their default values into ```@timeframe.start``` and ```@timeframe.end```:
|
27
|
+
|
28
|
+
timeframeable :defaults => [:beginning_of_month, :now], :start_key => :s, :end_key => :e
|
29
|
+
|
30
|
+
Inject dates from ```params[:start]``` and ```params[:end]``` or their default values into ```@timeframe.start``` and ```@timeframe.end```, and inject dates from ```params[:s]``` and ```params[:e]``` or their default values into ```@tf.start``` and ```@tf.end```:
|
31
|
+
|
32
|
+
timeframeable :defaults => [:beginning_of_month, :now]
|
33
|
+
timeframeable :defaults => [:beginning_of_month, :now], :start_key => :s, :end_key => :e, :variable => :'@tf'
|
34
|
+
|
35
|
+
## TODO
|
36
|
+
If there will be such a need, it may be usefull to add ```:start_value``` and ```:end_value``` lambda options, which will extract appropriate values from ```params``` bypassing ```:start_key``` and ```:end_key```.
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
1. Fork it
|
41
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
42
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
43
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
44
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'timeframeable/version'
|
2
|
+
require 'active_support/concern'
|
3
|
+
require 'date'
|
4
|
+
require 'active_support/core_ext/date/calculations'
|
5
|
+
require 'active_support/core_ext/date/conversions'
|
6
|
+
require 'active_support/core_ext/time/calculations'
|
7
|
+
require 'active_support/core_ext/date_time/calculations'
|
8
|
+
require 'active_support/core_ext/date_time/conversions'
|
9
|
+
|
10
|
+
module Timeframeable
|
11
|
+
extend ActiveSupport::Concern
|
12
|
+
|
13
|
+
Timeframe = Struct.new(:start, :end)
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
def timeframeable(options={})
|
17
|
+
raise 'Timeframe defaults not defined' if options[:defaults].blank?
|
18
|
+
raise 'Invalid Timeframe defaults' unless options[:defaults].size == 2
|
19
|
+
|
20
|
+
options = options.dup
|
21
|
+
|
22
|
+
options[:start_key] ||= :start
|
23
|
+
options[:end_key] ||= :end
|
24
|
+
options[:variable] ||= :'@timeframe'
|
25
|
+
options[:defaults] = options[:defaults].map do |x|
|
26
|
+
if x.is_a? Symbol
|
27
|
+
if x == :now
|
28
|
+
DateTime.now
|
29
|
+
else
|
30
|
+
DateTime.now.send(x)
|
31
|
+
end
|
32
|
+
else
|
33
|
+
x
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
before_filter do
|
38
|
+
set_timeframe options
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.parse_date(param, extrapolate=nil)
|
44
|
+
return unless param
|
45
|
+
|
46
|
+
if param.kind_of? Hash
|
47
|
+
parts = [:year, :month, :day].map do |key|
|
48
|
+
value = param[key].to_i
|
49
|
+
if value == 0
|
50
|
+
value = case key
|
51
|
+
when :year
|
52
|
+
Date.today.year
|
53
|
+
else
|
54
|
+
1
|
55
|
+
end
|
56
|
+
end
|
57
|
+
value
|
58
|
+
end
|
59
|
+
|
60
|
+
begin
|
61
|
+
result = DateTime.new(*parts)
|
62
|
+
rescue
|
63
|
+
return
|
64
|
+
end
|
65
|
+
|
66
|
+
case extrapolate
|
67
|
+
when :end
|
68
|
+
if param[:day].to_i == 0
|
69
|
+
result = result.end_of_month
|
70
|
+
end
|
71
|
+
result = result.end_of_day
|
72
|
+
else
|
73
|
+
end
|
74
|
+
|
75
|
+
result
|
76
|
+
else
|
77
|
+
begin
|
78
|
+
DateTime.parse(param.to_s)
|
79
|
+
rescue
|
80
|
+
return
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def set_timeframe(options)
|
88
|
+
start_date = Timeframeable.parse_date(params[options[:start_key]]) || options[:defaults][0].to_datetime
|
89
|
+
end_date = Timeframeable.parse_date(params[options[:end_key]], :end) || options[:defaults][1].to_datetime
|
90
|
+
instance_variable_set options[:variable], Timeframe.new(start_date.utc, end_date.utc)
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'timeframeable'
|
3
|
+
require 'timecop'
|
4
|
+
|
5
|
+
describe Timeframeable do
|
6
|
+
|
7
|
+
it 'has dedicated data structure' do
|
8
|
+
data = Timeframeable::Timeframe.new(1, 2)
|
9
|
+
data.start.should == 1
|
10
|
+
data.end.should == 2
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'Timeframeable.parse_date' do
|
14
|
+
|
15
|
+
it 'uses DateTime.parse for strings' do
|
16
|
+
data = DateTime.now.change(:usec => 0)
|
17
|
+
Timeframeable.parse_date(data.to_s).should == data
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'suppress exceptions on invalid strings' do
|
21
|
+
Timeframeable.parse_date('trololo').should be_nil
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'accepts composite param' do
|
25
|
+
Timeframeable.parse_date({:year => 2013, :month => 1, :day => 15}).should == DateTime.new(2013, 1, 15)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'extrapolates range to its end' do
|
29
|
+
Timeframeable.parse_date({:year => 2013, :month => 1, :day => 15}, :end).should == DateTime.new(2013, 1, 15).end_of_day
|
30
|
+
Timeframeable.parse_date({:year => 2013, :month => 1}, :end).should == DateTime.new(2013, 1, 1).end_of_month.end_of_day
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'controller' do
|
35
|
+
let(:controller) { Class.new.new }
|
36
|
+
let(:default_options) {
|
37
|
+
{
|
38
|
+
:start_key => :start,
|
39
|
+
:end_key => :end,
|
40
|
+
:variable => :'@timeframe',
|
41
|
+
:defaults => [DateTime.now.beginning_of_month, DateTime.now]
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
before do
|
46
|
+
controller.class.class_eval do
|
47
|
+
include Timeframeable
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'Controller.timeframeable' do
|
52
|
+
|
53
|
+
before do
|
54
|
+
mock(controller.class).before_filter() {|block| controller.instance_eval(&block) }
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'timeframes with default options' do
|
58
|
+
options_given = {:defaults => default_options[:defaults]}
|
59
|
+
options_got = default_options
|
60
|
+
mock(controller).set_timeframe(options_got)
|
61
|
+
controller.class.timeframeable(options_given)
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'timeframes with symbol options' do
|
65
|
+
Timecop.freeze(DateTime.now) do
|
66
|
+
options_given = {:defaults => [:beginning_of_month, :now]}
|
67
|
+
options_got = default_options.merge(:defaults => [DateTime.now.beginning_of_month, DateTime.now])
|
68
|
+
mock(controller).set_timeframe(options_got)
|
69
|
+
controller.class.timeframeable(options_given)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'timeframes with custom options' do
|
74
|
+
Timecop.freeze(DateTime.now) do
|
75
|
+
options_given = {:defaults => [:beginning_of_month, :now], :start_key => :s, :end_key => :e, :variable => :'@tf'}
|
76
|
+
options_got = default_options.merge(options_given).merge(:defaults => [DateTime.now.beginning_of_month, DateTime.now])
|
77
|
+
mock(controller).set_timeframe(options_got)
|
78
|
+
controller.class.timeframeable(options_given)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe 'Controller#set_timeframe' do
|
84
|
+
it 'falls back to default values' do
|
85
|
+
stub(controller).params{ {} }
|
86
|
+
controller.send :set_timeframe, default_options
|
87
|
+
controller.instance_variable_get(default_options[:variable]).should ==
|
88
|
+
Timeframeable::Timeframe.new(*default_options[:defaults])
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'uses actual params' do
|
92
|
+
dates = default_options[:defaults].map{|d| d.prev_month.utc.change(:usec => 0) }
|
93
|
+
stub(controller).params{ Hash[[:start, :end].zip(dates.map(&:to_s))] }
|
94
|
+
controller.send :set_timeframe, default_options
|
95
|
+
controller.instance_variable_get(default_options[:variable]).should ==
|
96
|
+
Timeframeable::Timeframe.new(*dates)
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'allows multiple timeframes to be set' do
|
100
|
+
stub(controller).params{ {} }
|
101
|
+
other_options = default_options.merge(:variable => :'@test', :defaults => default_options[:defaults].map(&:prev_month))
|
102
|
+
controller.send :set_timeframe, default_options
|
103
|
+
controller.send :set_timeframe, other_options
|
104
|
+
controller.instance_variable_get(default_options[:variable]).should ==
|
105
|
+
Timeframeable::Timeframe.new(*default_options[:defaults])
|
106
|
+
controller.instance_variable_get(other_options[:variable]).should ==
|
107
|
+
Timeframeable::Timeframe.new(*other_options[:defaults])
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'timeframeable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'timeframeable'
|
8
|
+
gem.version = Timeframeable::VERSION
|
9
|
+
gem.authors = ['AlexanderPavlenko']
|
10
|
+
gem.email = ['a.pavlenko@roundlake.ru']
|
11
|
+
gem.description = %q{controller module for extracting datetime range from request params}
|
12
|
+
gem.summary = %q{datetime range extractor}
|
13
|
+
gem.homepage = ''
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ['lib']
|
19
|
+
|
20
|
+
gem.add_dependency 'activesupport'
|
21
|
+
gem.add_development_dependency 'rspec'
|
22
|
+
gem.add_development_dependency 'rr'
|
23
|
+
gem.add_development_dependency 'timecop'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: timeframeable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- AlexanderPavlenko
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rr
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: timecop
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: controller module for extracting datetime range from request params
|
79
|
+
email:
|
80
|
+
- a.pavlenko@roundlake.ru
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- lib/timeframeable.rb
|
91
|
+
- lib/timeframeable/version.rb
|
92
|
+
- spec/lib/timeframeable_spec.rb
|
93
|
+
- spec/spec_helper.rb
|
94
|
+
- timeframeable.gemspec
|
95
|
+
homepage: ''
|
96
|
+
licenses: []
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ! '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 1.8.24
|
116
|
+
signing_key:
|
117
|
+
specification_version: 3
|
118
|
+
summary: datetime range extractor
|
119
|
+
test_files:
|
120
|
+
- spec/lib/timeframeable_spec.rb
|
121
|
+
- spec/spec_helper.rb
|
122
|
+
has_rdoc:
|