quarter_system 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +36 -0
- data/Rakefile +37 -0
- data/lib/quarter_system/argumentation/argument_merger.rb +120 -0
- data/lib/quarter_system/argumentation/arguments.rb +39 -0
- data/lib/quarter_system/argumentation/constants.rb +14 -0
- data/lib/quarter_system/argumentation/errors.rb +33 -0
- data/lib/quarter_system/argumentation/object.rb +18 -0
- data/lib/quarter_system/argumentation.rb +28 -0
- data/lib/quarter_system/integer.rb +11 -0
- data/lib/quarter_system/quarter.rb +120 -0
- data/lib/quarter_system/quarter_boundary_date.rb +18 -0
- data/lib/quarter_system/version.rb +3 -0
- data/lib/quarter_system.rb +13 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/assets/javascripts/application.js +9 -0
- data/test/dummy/app/assets/stylesheets/application.css +7 -0
- data/test/dummy/app/controllers/application_controller.rb +3 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/config/application.rb +45 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +30 -0
- data/test/dummy/config/environments/production.rb +60 -0
- data/test/dummy/config/environments/test.rb +39 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +10 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +58 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/schema.rb +16 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +23 -0
- data/test/dummy/log/test.log +0 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +26 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/script/rails +6 -0
- data/test/quarter_system_test.rb +224 -0
- data/test/test_helper.rb +10 -0
- metadata +162 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 caleon.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
= QuarterSystem
|
2
|
+
|
3
|
+
Currently the types of quarters are hardcoded but it shouldn't be tough to expand the kinds of quarter systems you wish to define for your specific project. By default the gem sets up the US fiscal calendar's definition of quarters (October-December, January-March, April-June, and July-September).
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
=== Quarter.general()
|
8
|
+
|
9
|
+
Calling Quarter.general(n) where n refers to the n'th quarter of this particular year returns the Quarter object (which is a subclass of Range with a begin and end date). The boundary dates are of a subclass QuarterBoundaryDate.
|
10
|
+
|
11
|
+
If n exceeds the number of quarters in the current system, it will cycle so that in a system with 4 quarters, Quarter.general(1) is the same as Quarter.general(5) or Quarter.general(4n+1).
|
12
|
+
|
13
|
+
|
14
|
+
=== Quarter.specific()
|
15
|
+
|
16
|
+
On the other hand, Quarter.specific(n) where n exceeds the number of quarters will increment or decrement the year as required, so Quarter.specific(20) is a call for the value of the Quarter object which is the 20th quarter from the current calendar year.
|
17
|
+
|
18
|
+
=== Quarter.current()
|
19
|
+
|
20
|
+
Quarter.current simply returns the Quarter object which contains the current date, and Quarter.current_number returns the numeric value of the current quarter in the quarter system (i.e. 1 for 1st quarter of the year).
|
21
|
+
|
22
|
+
== Additional information
|
23
|
+
|
24
|
+
=== Contributors
|
25
|
+
|
26
|
+
We have a short list of valued contributors. Check them all at:
|
27
|
+
|
28
|
+
http://github.com/caleon/quarter_system/contributors
|
29
|
+
|
30
|
+
=== Maintainers
|
31
|
+
|
32
|
+
* caleon (http://github.com/caleon)
|
33
|
+
|
34
|
+
== License
|
35
|
+
|
36
|
+
MIT License. Copyright 2011 caleon.
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'CoreUtilities'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
Bundler::GemHelper.install_tasks
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
|
29
|
+
Rake::TestTask.new(:test) do |t|
|
30
|
+
t.libs << 'lib'
|
31
|
+
t.libs << 'test'
|
32
|
+
t.pattern = 'test/**/*_test.rb'
|
33
|
+
t.verbose = false
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
task :default => :test
|
@@ -0,0 +1,120 @@
|
|
1
|
+
class ArgumentMerger
|
2
|
+
instance_methods.each do |method|
|
3
|
+
undef_method(method) if method !~ /^(__|instance_eval|class|object_id|respond_to?|puts|logger|extend|kind_of?)/
|
4
|
+
end
|
5
|
+
|
6
|
+
attr_accessor :archetype
|
7
|
+
def initialize(archetype, arguments, opts={})
|
8
|
+
@archetype = archetype
|
9
|
+
@arguments = arguments.compact
|
10
|
+
@opts = opts.tap { |o| o[:else] ||= nil }
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
######
|
15
|
+
## Shit, all Private
|
16
|
+
######
|
17
|
+
private unless Rails.env.development?
|
18
|
+
def archetype_respond_to_proc
|
19
|
+
proc { |method_sym| @archetype.__send__(:respond_to?, method_sym) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def archetype_send_proc
|
23
|
+
proc { |method_sym, *args, &b| @archetype.__send__(method_sym, *constrained_args_for(method_sym, args)) }
|
24
|
+
end
|
25
|
+
|
26
|
+
def archetype_respond_to?(method_sym)
|
27
|
+
archetype_respond_to_proc.call(method_sym)
|
28
|
+
rescue NameError => e
|
29
|
+
throw(:argument_error, raise(ArchetypeMethodsError, "For #{@archetype}, calling #archetype_respond_to? using method_sym=#{method_sym} led to argument_error"))
|
30
|
+
end
|
31
|
+
|
32
|
+
def archetype_send(method_sym, *args, &b)
|
33
|
+
archetype_send_proc.call(method_sym, *args, &b)
|
34
|
+
rescue NameError => e
|
35
|
+
throw(:argument_error, raise(ArchetypeMethodsError, "For #{@archetype}, calling #archetype_send using method_sym=#{method_sym} and args=#{args} with &b=#{b} led to argument_error"))
|
36
|
+
end
|
37
|
+
|
38
|
+
#####
|
39
|
+
# @_archetype.method Info grabbing.
|
40
|
+
def archetype_method(method_sym)
|
41
|
+
@archetype.method(method_sym)
|
42
|
+
end
|
43
|
+
|
44
|
+
def archetype_method_params(method_sym)
|
45
|
+
archetype_method(method_sym).parameters
|
46
|
+
end
|
47
|
+
|
48
|
+
def archetype_method_params_size(method_sym)
|
49
|
+
archetype_method_params(method_sym).size
|
50
|
+
end
|
51
|
+
|
52
|
+
def archetype_method_arity(method_sym)
|
53
|
+
archetype_method(method_sym).arity
|
54
|
+
end
|
55
|
+
|
56
|
+
#####
|
57
|
+
# Minimum number of args
|
58
|
+
def minimum_args_from_params(method_sym)
|
59
|
+
archetype_method_params(method_sym).count { |type, meth| type == :req }
|
60
|
+
end
|
61
|
+
|
62
|
+
def minimum_args_from_arity(method_sym)
|
63
|
+
archetype_method_arity(method_sym).if?(:negative?).succ.abs # upto succ only happens to negatives.
|
64
|
+
end
|
65
|
+
|
66
|
+
def minimum_args(method_sym)
|
67
|
+
Array.new(2, "minimum_args_from_").zip(%w(params arity)).map { |strs| self.__send__(strs.join, method_sym) }.max
|
68
|
+
end
|
69
|
+
|
70
|
+
####
|
71
|
+
# Maximum number of args
|
72
|
+
def maximums_for_param_types
|
73
|
+
{ :req => 1, :opt => 1, :block => 1, :rest => 1.0/0 }
|
74
|
+
end
|
75
|
+
|
76
|
+
def maximum_args_from_params(method_sym)
|
77
|
+
return 1.0/0 if archetype_method_params(method_sym).detect { |type, meth| type == :rest }
|
78
|
+
archetype_method_params(method_sym).inject(0) { |sum, (type, meth)| sum += maximums_for_param_types[type] }
|
79
|
+
end
|
80
|
+
|
81
|
+
def maximum_args_from_arity(method_sym)
|
82
|
+
archetype_method_arity(method_sym).abs
|
83
|
+
end
|
84
|
+
|
85
|
+
def maximum_args(method_sym)
|
86
|
+
Array.new(2, "maximum_args_from_").zip(%w(params arity)).map { |strs| self.__send__(strs.join, method_sym) }.max # from .min...
|
87
|
+
end
|
88
|
+
|
89
|
+
####
|
90
|
+
# All those for these
|
91
|
+
def constrained_args_for(method_sym, args)
|
92
|
+
args.take(maximum_args(method_sym).unless?(:finite?) { args.size })
|
93
|
+
end
|
94
|
+
|
95
|
+
def provided_minimum_arguments?(method_sym, arguments)
|
96
|
+
minimum_args(method_sym) > maximum_args(method_sym) and raise ArchetypeError, "Range of argument arity is invalid!"
|
97
|
+
minimum_args(method_sym) <= arguments.size
|
98
|
+
end
|
99
|
+
|
100
|
+
def method_missing(method_sym, *arguments, &block)
|
101
|
+
return @opts[:else] if check_conditions?
|
102
|
+
arguments = Array.wrap(arguments.last.is_a?(Proc) ? lambda { |*args| @arguments | arguments.pop.call(*args) } : @arguments) + arguments.compact
|
103
|
+
|
104
|
+
catch(:argument_error) do
|
105
|
+
archetype_respond_to?(method_sym) or raise ArchetypeMethodsError, "Issue with #{method_sym}"
|
106
|
+
provided_minimum_arguments?(method_sym, arguments) or raise ArchetypeArgumentsError, "Issue with #{method_sym}"
|
107
|
+
archetype_send(method_sym, *arguments, &block)
|
108
|
+
end
|
109
|
+
rescue ArchetypeError => e
|
110
|
+
logger.error "<errorClass>#{e.class}: <method>#{method_sym}: <msg>#{e.message}" if Rails.env.development?
|
111
|
+
super
|
112
|
+
rescue
|
113
|
+
logger.error "Giving up rescuing #{@archetype.name rescue nil}##{method_sym}. Arguments are #{arguments.inspect}" if Rails.env.development?
|
114
|
+
@opts[:else]
|
115
|
+
end
|
116
|
+
|
117
|
+
def check_conditions?
|
118
|
+
@opts[:nonnil] && arguments.empty? or @opts[:present] && arguments.any?(:present?)
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class Arguments < Array
|
2
|
+
def self.new(args)
|
3
|
+
arguer = args.last.is_a?(Class) && args.pop
|
4
|
+
super.tap {|args_obj| args_obj.arguer = arguer }
|
5
|
+
end
|
6
|
+
|
7
|
+
attr_accessor :arguer
|
8
|
+
|
9
|
+
# kondition needs to be a string to be evaluated when appended to the element. Refer to Arguments#passes_condition?
|
10
|
+
def find_with_reqs(reqs={}) # Arguments itself can be any length. But these three are things allowed to be defined.
|
11
|
+
[ total_match_value(reqs[:class], reqs[:condition]), quey_match_value(reqs[:quey]), default_value_for(reqs[:quey]) ].compact.first
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
def default_value_for(quey) # not schrod. arguer can be nil or methodmissing or result in raise.
|
16
|
+
arguer.send(:"default_#{quey}") rescue nil
|
17
|
+
end
|
18
|
+
|
19
|
+
def total_match_value(*args)
|
20
|
+
detect { |arg| total_match?(arg, *args) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def quey_match_value(quey=nil) # hm, allowing nil as quey
|
24
|
+
last[quey] if last.is_a?(Hash)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def matches_class?(arg, klass=nil)
|
29
|
+
arg.is_a?(klass || Object)
|
30
|
+
end
|
31
|
+
|
32
|
+
def passes_condition?(arg, konditions=nil)
|
33
|
+
instance_eval(%{arg#{konditions}})
|
34
|
+
end
|
35
|
+
|
36
|
+
def total_match?(arg, *checks) # total_match?(thing, klass, konditions)
|
37
|
+
matches_class?(arg, checks[0]) && passes_condition?(arg, checks[1])
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
REGES = { :with_method_name => '(?:_with_([a-z0-9]+(?:[a-z0-9]|_(?![_=\b]))*?))?',
|
2
|
+
:method_name => '([a-z0-9]+(?:[a-z0-9]|_(?![_=\b]))*?)',
|
3
|
+
:method_name_with_setter => '([a-z0-9]+(?:[a-z0-9]|_(?![_=\b]))*?)(=?)',
|
4
|
+
:method_name_without_capture => '(?:[a-z0-9]+(?:[a-z0-9]|_(?![_=\b]))*?)' }
|
5
|
+
|
6
|
+
# REGEX is the Regular Expression that is not anchored.
|
7
|
+
REGEX = Hash[*REGES.to_a.map do |arr|
|
8
|
+
[ arr[0],
|
9
|
+
if arr[1].is_a?(Hash)
|
10
|
+
Hash[*arr[1].to_a.map { |key, reges| [ key, Regexp.new(reges, Regexp::IGNORECASE) ] }.flatten]
|
11
|
+
else
|
12
|
+
Regexp.new(arr[1], Regexp::IGNORECASE)
|
13
|
+
end ]
|
14
|
+
end.flatten]
|
@@ -0,0 +1,33 @@
|
|
1
|
+
#########
|
2
|
+
# Error classes.
|
3
|
+
class ArgumentMergerLogger < ActiveSupport::BufferedLogger
|
4
|
+
SEVERITIES = Severity.constants.inject([]) { |arr, c| arr[Severity.const_get(c)] = c; arr }
|
5
|
+
|
6
|
+
def format_message(severity, timestamp, progname, msg)
|
7
|
+
"(#{timestamp.to_s(:db)})[#{SEVERITIES[severity]}]: #{msg.strip}\n"
|
8
|
+
end
|
9
|
+
|
10
|
+
def add(severity, message=nil, progname=nil, &block)
|
11
|
+
return if @level > severity
|
12
|
+
message = (message || block.try(:call) || progname).to_s
|
13
|
+
message = format_message(severity, Time.zone.now, progname, message)
|
14
|
+
buffer << message
|
15
|
+
auto_flush
|
16
|
+
message
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# ArgumentMerger.send(:include, SetupLoggers)
|
21
|
+
# ArgumentMerger.logger = ArgumentMergerLogger.new(File.join(Rails.root, 'log', "argument_merger_#{Rails.env}.log"))
|
22
|
+
|
23
|
+
class ArchetypeError < StandardError
|
24
|
+
def initialize(msg=nil, opts={})
|
25
|
+
ArgumentMerger.logger.send(opts[:severity] || :warn, "#{name}: #{msg}")
|
26
|
+
end
|
27
|
+
|
28
|
+
def name
|
29
|
+
this.class.name
|
30
|
+
end
|
31
|
+
end
|
32
|
+
class ArchetypeMethodsError < ArchetypeError; end
|
33
|
+
class ArchetypeArgumentsError < ArchetypeError; end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
##########################
|
2
|
+
# Custom handling or arguments:
|
3
|
+
# Allows things like User.with_arguments(1, 2, 3).find(:order => 'id DESC')
|
4
|
+
# result: => User.find(1, 2, 3, :order => 'id DESC')
|
5
|
+
# but also checks to see that arguments are valid.
|
6
|
+
#
|
7
|
+
# Also, allows us to condense patterns like: record.perform_action! if record.respond_to?(:perform_action!)
|
8
|
+
# by doing the following instead record.maybe.perform_action!
|
9
|
+
class Object
|
10
|
+
def with_arguments(*arguments)
|
11
|
+
arg_merger = ArgumentMerger.new(self, arguments)
|
12
|
+
block_given? ? yield(arg_merger) : arg_merger
|
13
|
+
end
|
14
|
+
[ :with_argument, :maybe_with, :maybe ].each { |meth| alias_method meth, :with_arguments; }
|
15
|
+
|
16
|
+
def with_nonnil(*arguments); with_arguments(*arguments.merge_options(:nonnil => true)); end
|
17
|
+
def with_present(*arguments); with_arguments(*arguments.merge_options(:present => true)); end
|
18
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'quarter_system/argumentation/constants'
|
2
|
+
|
3
|
+
module Argumentation
|
4
|
+
def build_arguments(*args)
|
5
|
+
args = args.first if args.size == 1 and args.first.is_a?(Array)
|
6
|
+
Arguments.new(args + [self])
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
def find_from_args(arguments, reqs={})
|
11
|
+
build_arguments(*arguments).find_with_reqs(reqs)
|
12
|
+
end
|
13
|
+
|
14
|
+
def method_missing(method_symbol, *arguments, &block)
|
15
|
+
method_symbol.to_s.match(methods_match_regexp).only_if_a?(MatchData) do |md|
|
16
|
+
md[1].split('_and_').push(md[2]).map { |prop_name| :"#{prop_name}_from_args" }.map { |method_sym| send(method_sym, *arguments) if respond_to?(method_sym) }
|
17
|
+
end || super
|
18
|
+
end
|
19
|
+
|
20
|
+
def methods_match_regexp
|
21
|
+
/^((?:#{REGEX[:method_name_without_capture].source}(?:_and_)(?!from_args))+)#{REGEX[:method_name].source}_from_args$/
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'quarter_system/argumentation/arguments'
|
26
|
+
require 'quarter_system/argumentation/object'
|
27
|
+
require 'quarter_system/argumentation/argument_merger'
|
28
|
+
require 'quarter_system/argumentation/errors'
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# There is already a Fixnum#ordinalize
|
2
|
+
class Integer
|
3
|
+
ORDINALS = %w(first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth)
|
4
|
+
def ordinal(nonnumeric=false)
|
5
|
+
if nonnumeric && (1..ORDINALS.size.succ).include?(self)
|
6
|
+
ORDINALS[pred]
|
7
|
+
elsif nonnumeric
|
8
|
+
to_s + ([ [ nil, 'st','nd','rd' ], [] ][self / 10 == 1 && 1 || 0][self % 10] || 'th')
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
class Quarter < Range
|
2
|
+
extend Argumentation
|
3
|
+
|
4
|
+
DEFAULTS = { :us_fiscal => [ [ 10, 12 ], # Q1
|
5
|
+
[ 1, 3 ], # Q2
|
6
|
+
[ 4, 6 ], # Q3
|
7
|
+
[ 7, 9 ] ] # Q4
|
8
|
+
}
|
9
|
+
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def default_type
|
13
|
+
:us_fiscal
|
14
|
+
end
|
15
|
+
|
16
|
+
def default_year
|
17
|
+
Time.zone_default.today.year rescue Time.new.year
|
18
|
+
end # depends on time zone.
|
19
|
+
|
20
|
+
def default_number
|
21
|
+
1
|
22
|
+
end
|
23
|
+
|
24
|
+
# Changing this so that table type can be input as an option. This is so that when changing quarter system, the dev can use
|
25
|
+
# the with_options to change the type easily for all relevant calls.
|
26
|
+
# Ex: Quarter.quarters_table(:uk_fiscal)
|
27
|
+
# Quarter.quarters_table(:type => :uk_fiscal)
|
28
|
+
def quarters_table(*args)
|
29
|
+
DEFAULTS[type_from_args(args)]
|
30
|
+
end
|
31
|
+
|
32
|
+
def number_of_quarters(*args);
|
33
|
+
quarters_table(*args).size
|
34
|
+
end
|
35
|
+
|
36
|
+
# Sequential returns the Quarter for the correct year depending on the magnitude of the `number` argument.
|
37
|
+
def specific(*args)
|
38
|
+
number, year, type = number_and_year_and_type_from_args(*args)
|
39
|
+
index = number.pred % number_of_quarters(*args)
|
40
|
+
opts = args.extract_options! # at this point, args not needed in its original form.
|
41
|
+
|
42
|
+
months_array = quarters_table(type)[index]
|
43
|
+
first_months_array = quarters_table(type)[0]
|
44
|
+
specified_quarter_month = months_array[0]
|
45
|
+
first_quarter_month = first_months_array[0]
|
46
|
+
|
47
|
+
year += opts[:this_year] ? 0 : (number.pred / number_of_quarters) + (first_quarter_month > specified_quarter_month ? 1 : 0)
|
48
|
+
|
49
|
+
new(Date.new(year, months_array[0], 1), Date.new(year, months_array[1], 1).end_of_month, :number => number)
|
50
|
+
end
|
51
|
+
|
52
|
+
def all(*args)
|
53
|
+
type, year = type_and_year_from_args(*args)
|
54
|
+
1.upto(number_of_quarters(type)).map { |number| specific(*args.merge_options(:number => number)) }
|
55
|
+
end
|
56
|
+
|
57
|
+
def general(*args)
|
58
|
+
specific(*args.merge_options(:this_year => true))
|
59
|
+
end
|
60
|
+
|
61
|
+
def current(*args)
|
62
|
+
all(*args.merge_options(:this_year => true)).detect { |rng| rng.include? ::Time.zone_default.today }
|
63
|
+
end
|
64
|
+
|
65
|
+
def current_number(*args)
|
66
|
+
current(*args).number
|
67
|
+
end
|
68
|
+
|
69
|
+
# ARGUMENTS MUST NOT BE SPLATTED INTO THIS. Point is for this method to not be destructive.
|
70
|
+
# Also, whatever `type` is defined within normal list of arguments overrides any written as a hash value.
|
71
|
+
# Thing about this whole file is that `type` is the only argument ever that's a symbol,
|
72
|
+
# `year` is the only argument that's ever a Numeric of length == 4,
|
73
|
+
# `number` is the only argument that's ever a Numeric of length < 4.
|
74
|
+
def type_from_args(*args)
|
75
|
+
find_from_args(args, :quey => :type, :class => Symbol)
|
76
|
+
end
|
77
|
+
|
78
|
+
def year_from_args(*args)
|
79
|
+
find_from_args(args, :quey => :year, :class => Numeric, :condition => ".to_s.size == 4")
|
80
|
+
end
|
81
|
+
|
82
|
+
def number_from_args(*args)
|
83
|
+
find_from_args(args, :quey => :number, :class => Numeric, :condition => ".to_s.size < 3")
|
84
|
+
end
|
85
|
+
|
86
|
+
def method_missing(method_symbol, *arguments, &block)
|
87
|
+
ordinals = Integer::ORDINALS
|
88
|
+
method_symbol.to_s.match(/^(#{ordinals.join('|')})$/).only_if_a?(MatchData) { |md| specific(ordinals.index(md[1]), *arguments) } || super
|
89
|
+
end
|
90
|
+
private :method_missing
|
91
|
+
end
|
92
|
+
|
93
|
+
attr_accessor :count, :number, :type
|
94
|
+
|
95
|
+
def initialize(*args)
|
96
|
+
attrs = args.extract_options!
|
97
|
+
self.type = attrs.delete(:type)
|
98
|
+
self.count = attrs.delete(:number)
|
99
|
+
self.number = (self.count.pred % Quarter.number_of_quarters(:type => self.type)).next
|
100
|
+
super
|
101
|
+
end
|
102
|
+
|
103
|
+
def include?(date_or_time)
|
104
|
+
if date_or_time.is_a?(Time)
|
105
|
+
super(date_or_time.in_time_zone(Time.zone_default).to_date)
|
106
|
+
else
|
107
|
+
super
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# Instance method from a Quarter object, such that quarter.next returns the following Quarter object. Doesn't mean next one in relation to the current.
|
112
|
+
# for that you would need to use Quarter.current.next or Quarter.next on the class itself.
|
113
|
+
%w(next prev).each_with_index do |method_name, i|
|
114
|
+
class_eval(%{
|
115
|
+
def #{method_name}(size=1)
|
116
|
+
Quarter.specific(number.send(:#{%w(+ -)[i]}, size))
|
117
|
+
end
|
118
|
+
})
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class QuarterBoundaryDate < Date
|
2
|
+
# args for Date#new are year, month, day, and an optional "day of calendar reform"
|
3
|
+
# just not gonna worry about day of calendar reform at all.
|
4
|
+
def self.new(*args)
|
5
|
+
args.unshift(this_year) if args.size < 2
|
6
|
+
super(*args)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class QuarterBeginDate < QuarterBoundaryDate
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
class QuarterEndDate < QuarterBoundaryDate
|
15
|
+
def self.new(*args)
|
16
|
+
super.end_of_month
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'active_support/core_ext/numeric/time'
|
2
|
+
require 'active_support/time_with_zone'
|
3
|
+
require 'active_support/values/time_zone'
|
4
|
+
|
5
|
+
require 'quarter_system/argumentation'
|
6
|
+
|
7
|
+
require 'quarter_system/quarter'
|
8
|
+
require 'quarter_system/quarter_boundary_date'
|
9
|
+
require 'quarter_system/integer'
|
10
|
+
|
11
|
+
module QuarterSystem
|
12
|
+
|
13
|
+
end
|
data/test/dummy/Rakefile
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
3
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
4
|
+
|
5
|
+
require File.expand_path('../config/application', __FILE__)
|
6
|
+
|
7
|
+
Dummy::Application.load_tasks
|
@@ -0,0 +1,9 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into including all the files listed below.
|
2
|
+
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
|
3
|
+
// be included in the compiled file accessible from http://example.com/assets/application.js
|
4
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
5
|
+
// the compiled file.
|
6
|
+
//
|
7
|
+
//= require jquery
|
8
|
+
//= require jquery_ujs
|
9
|
+
//= require_tree .
|
@@ -0,0 +1,7 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll automatically include all the stylesheets available in this directory
|
3
|
+
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
|
4
|
+
* the top of the compiled file, but it's generally better to create a new file per style scope.
|
5
|
+
*= require_self
|
6
|
+
*= require_tree .
|
7
|
+
*/
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
require 'rails/all'
|
4
|
+
|
5
|
+
Bundler.require
|
6
|
+
require "quarter_system"
|
7
|
+
|
8
|
+
module Dummy
|
9
|
+
class Application < Rails::Application
|
10
|
+
# Settings in config/environments/* take precedence over those specified here.
|
11
|
+
# Application configuration should go into files in config/initializers
|
12
|
+
# -- all .rb files in that directory are automatically loaded.
|
13
|
+
|
14
|
+
# Custom directories with classes and modules you want to be autoloadable.
|
15
|
+
# config.autoload_paths += %W(#{config.root}/extras)
|
16
|
+
|
17
|
+
# Only load the plugins named here, in the order given (default is alphabetical).
|
18
|
+
# :all can be used as a placeholder for all plugins not explicitly named.
|
19
|
+
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
|
20
|
+
|
21
|
+
# Activate observers that should always be running.
|
22
|
+
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
|
23
|
+
|
24
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
25
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
26
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
27
|
+
|
28
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
29
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
30
|
+
# config.i18n.default_locale = :de
|
31
|
+
|
32
|
+
# Configure the default encoding used in templates for Ruby 1.9.
|
33
|
+
config.encoding = "utf-8"
|
34
|
+
|
35
|
+
# Configure sensitive parameters which will be filtered from the log file.
|
36
|
+
config.filter_parameters += [:password]
|
37
|
+
|
38
|
+
# Enable the asset pipeline
|
39
|
+
config.assets.enabled = true
|
40
|
+
|
41
|
+
# Version of your assets, change this if you want to expire all your assets
|
42
|
+
config.assets.version = '1.0'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# SQLite version 3.x
|
2
|
+
# gem install sqlite3
|
3
|
+
#
|
4
|
+
# Ensure the SQLite 3 gem is defined in your Gemfile
|
5
|
+
# gem 'sqlite3'
|
6
|
+
development:
|
7
|
+
adapter: sqlite3
|
8
|
+
database: db/development.sqlite3
|
9
|
+
pool: 5
|
10
|
+
timeout: 5000
|
11
|
+
|
12
|
+
# Warning: The database defined as "test" will be erased and
|
13
|
+
# re-generated from your development database when you run "rake".
|
14
|
+
# Do not set this db to the same as development or production.
|
15
|
+
test:
|
16
|
+
adapter: sqlite3
|
17
|
+
database: db/test.sqlite3
|
18
|
+
pool: 5
|
19
|
+
timeout: 5000
|
20
|
+
|
21
|
+
production:
|
22
|
+
adapter: sqlite3
|
23
|
+
database: db/production.sqlite3
|
24
|
+
pool: 5
|
25
|
+
timeout: 5000
|