Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fresh new world

  • Loading branch information...
commit fb6680f854d75dac9f3503cec12e8f1e6df55483 1 parent 3556499
@asanghi authored
View
18 .gitignore
@@ -1,5 +1,17 @@
-*.sw?
-.DS_Store
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
coverage
-rdoc
+doc/
+lib/bundler/man
pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
View
3  .travis.yml
@@ -0,0 +1,3 @@
+rvm:
+ - 1.8.7
+ - 1.9.3
View
4 Gemfile
@@ -0,0 +1,4 @@
+source "https://rubygems.org"
+
+gemspec
+
View
60 Rakefile
@@ -1,57 +1,7 @@
-require 'rubygems'
-require 'rake'
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
+require 'rspec/core/rake_task'
-begin
- require 'jeweler'
- Jeweler::Tasks.new do |gem|
- gem.name = "fiscali"
- gem.summary = %Q{Fiscal Year Date Functions}
- gem.email = "aditya.sanghi@risingsunbilling.com"
- gem.homepage = "http://asanghi.github.com/fiscali"
- gem.authors = ["Aditya Sanghi"]
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
- gem.add_dependency('activesupport')
- end
-
-rescue LoadError
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
-end
-
-require 'rake/testtask'
-Rake::TestTask.new(:test) do |test|
- test.libs << 'lib' << 'test'
- test.pattern = 'test/**/*_test.rb'
- test.verbose = true
-end
-
-begin
- require 'rcov/rcovtask'
- Rcov::RcovTask.new do |test|
- test.libs << 'test'
- test.pattern = 'test/**/*_test.rb'
- test.verbose = true
- end
-rescue LoadError
- task :rcov do
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
- end
-end
-
-
-task :default => :test
-
-require 'rdoc/task'
-Rake::RDocTask.new do |rdoc|
- if File.exist?('VERSION.yml')
- config = YAML.load(File.read('VERSION.yml'))
- version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
- else
- version = ""
- end
-
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = "fiscali #{version}"
- rdoc.rdoc_files.include('README*')
- rdoc.rdoc_files.include('lib/**/*.rb')
-end
+RSpec::Core::RakeTask.new(:spec)
+task :default => :spec
View
1  VERSION
@@ -1 +0,0 @@
-2.0.0
View
57 fiscali.gemspec
@@ -1,48 +1,23 @@
-# Generated by jeweler
-# DO NOT EDIT THIS FILE DIRECTLY
-# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/rising_sun/version', __FILE__)
-Gem::Specification.new do |s|
- s.name = "fiscali"
- s.version = "2.0.0"
+Gem::Specification.new do |gem|
+ gem.name = "fiscali"
+ gem.version = RisingSun::Fiscali::VERSION
+ gem.authors = ["Aditya Sanghi"]
+ gem.email = ["asanghi@me.com"]
+ gem.description = %q{Fiscal Year Date Functions}
+ gem.summary = %q{Fiscal Year Date Functions}
+ gem.homepage = "https://github.com/asanghi/fiscali"
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
- s.authors = ["Aditya Sanghi"]
- s.date = "2012-02-15"
- s.email = "aditya.sanghi@risingsunbilling.com"
- s.extra_rdoc_files = [
- "LICENSE",
- "README.markdown"
- ]
- s.files = [
- ".document",
- "LICENSE",
- "README.markdown",
- "Rakefile",
- "VERSION",
- "fiscali.gemspec",
- "install.rb",
- "lib/fiscali.rb",
- "lib/rising_sun/fiscali.rb",
- "test/fiscali_test.rb",
- "test/test_helper.rb"
- ]
- s.homepage = "http://asanghi.github.com/fiscali"
- s.require_paths = ["lib"]
- s.rubygems_version = "1.8.11"
- s.summary = "Fiscal Year Date Functions"
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ gem.files = `git ls-files`.split("\n")
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ gem.require_paths = ["lib"]
- if s.respond_to? :specification_version then
- s.specification_version = 3
+ gem.add_dependency 'activesupport'
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- s.add_runtime_dependency(%q<activesupport>, [">= 0"])
- else
- s.add_dependency(%q<activesupport>, [">= 0"])
- end
- else
- s.add_dependency(%q<activesupport>, [">= 0"])
- end
+ gem.add_development_dependency 'rake'
+ gem.add_development_dependency 'rspec', '~> 2.8'
end
View
3  lib/fiscali.rb
@@ -1,3 +1,6 @@
+require 'date'
+require 'active_support'
+require 'active_support/core_ext'
require 'rising_sun/fiscali'
Date.send(:include, RisingSun::Fiscali)
Time.send(:include, RisingSun::Fiscali)
View
164 lib/rising_sun/fiscali.rb
@@ -3,34 +3,33 @@ module Fiscali
def self.included(base)
base.extend ClassMethods
end
-
+
FISCAL_ZONE = {:india => 4, :uk => 4, :us => 10, :pakistan => 7,
:australia => 7, :ireland => 1, :nz => 7, :japan => 4}
FY_START_MONTH = 1
-
+
module ClassMethods
- mattr_accessor :fiscali_zone, :fiscali_start_month
def fiscal_zone=(zone)
- self.fiscali_start_month = FISCAL_ZONE[zone] || FY_START_MONTH
- self.fiscali_zone = zone
+ Thread.current[:fiscali_start_month] = FISCAL_ZONE[zone] || FY_START_MONTH
+ Thread.current[:fiscali_zone] = zone
end
def fy_start_month
- fiscali_start_month || FY_START_MONTH
+ Thread.current[:fiscali_start_month] || FY_START_MONTH
end
def fiscal_zone
- fiscali_zone
+ Thread.current[:fiscali_zone]
end
def fy_start_month=(month)
- self.fiscali_zone = nil
- self.fiscali_start_month = month
+ Thread.current[:fiscali_zone] = nil
+ Thread.current[:fiscali_start_month] = month
end
- def financial_year_start(year=::Date.today.year)
- ::Date.new(year,fy_start_month,1)
+ def financial_year_start(year=Date.today.year)
+ new(year,fy_start_month,1)
end
def financial_months
@@ -39,102 +38,99 @@ def financial_months
end
- module InstanceMethods
- def financial_year
- self.month < start_month ? self.year - 1 : self.year
- end
+ def financial_year
+ self.month < start_month ? self.year - 1 : self.year
+ end
- def beginning_of_financial_year
- change(:year => financial_year, :month => start_month, :day => 1)
- end
+ def beginning_of_financial_year
+ change(:year => financial_year, :month => start_month, :day => 1)
+ end
- def end_of_financial_year
- (beginning_of_financial_year + 1.year - 1.month).end_of_month
- end
+ def end_of_financial_year
+ (beginning_of_financial_year + 1.year - 1.month).end_of_month
+ end
- alias :beginning_of_financial_q1 :beginning_of_financial_year
- def end_of_financial_q1
- end_of_financial_year - 9.months
- end
+ alias :beginning_of_financial_q1 :beginning_of_financial_year
+ def end_of_financial_q1
+ end_of_financial_year - 9.months
+ end
- def beginning_of_financial_q2
- beginning_of_financial_year + 3.months
- end
+ def beginning_of_financial_q2
+ beginning_of_financial_year + 3.months
+ end
- def end_of_financial_q2
- end_of_financial_year - 6.months
- end
+ def end_of_financial_q2
+ end_of_financial_year - 6.months
+ end
- def beginning_of_financial_q3
- beginning_of_financial_year + 6.months
- end
+ def beginning_of_financial_q3
+ beginning_of_financial_year + 6.months
+ end
- def end_of_financial_q3
- end_of_financial_year - 3.months
- end
+ def end_of_financial_q3
+ end_of_financial_year - 3.months
+ end
- def beginning_of_financial_q4
- beginning_of_financial_year + 9.months
- end
- alias :end_of_financial_q4 :end_of_financial_year
+ def beginning_of_financial_q4
+ beginning_of_financial_year + 9.months
+ end
+ alias :end_of_financial_q4 :end_of_financial_year
- alias :beginning_of_financial_h1 :beginning_of_financial_year
- alias :end_of_financial_h1 :end_of_financial_q2
+ alias :beginning_of_financial_h1 :beginning_of_financial_year
+ alias :end_of_financial_h1 :end_of_financial_q2
- alias :beginning_of_financial_h2 :beginning_of_financial_q3
- alias :end_of_financial_h2 :end_of_financial_year
+ alias :beginning_of_financial_h2 :beginning_of_financial_q3
+ alias :end_of_financial_h2 :end_of_financial_year
- def financial_quarter
- "Q#{( months_between / 3 ).floor + 1} #{financial_year}"
- end
+ def financial_quarter
+ "Q#{( months_between / 3 ).floor + 1} #{financial_year}"
+ end
- def financial_half
- "H#{( months_between / 6 ).floor + 1} #{financial_year}"
- end
+ def financial_half
+ "H#{( months_between / 6 ).floor + 1} #{financial_year}"
+ end
- def next_financial_quarter
- beginning_of_financial_year.months_since(((months_between / 3).floor + 1 ) * 3)
- end
+ def next_financial_quarter
+ beginning_of_financial_year.months_since(((months_between / 3).floor + 1 ) * 3)
+ end
- def next_financial_half
- beginning_of_financial_year.months_since(((months_between / 6).floor + 1) * 6)
- end
+ def next_financial_half
+ beginning_of_financial_year.months_since(((months_between / 6).floor + 1) * 6)
+ end
- def beginning_of_financial_quarter
- beginning_of_financial_year.months_since(((months_between / 3).floor) * 3)
- end
+ def beginning_of_financial_quarter
+ beginning_of_financial_year.months_since(((months_between / 3).floor) * 3)
+ end
- def beginning_of_financial_half
- beginning_of_financial_year.months_since(((months_between / 6).floor) * 6)
- end
+ def beginning_of_financial_half
+ beginning_of_financial_year.months_since(((months_between / 6).floor) * 6)
+ end
- def previous_financial_quarter
- beginning_of_financial_quarter.months_ago(3)
- end
+ def previous_financial_quarter
+ beginning_of_financial_quarter.months_ago(3)
+ end
- def previous_financial_half
- beginning_of_financial_half.months_ago(6)
- end
+ def previous_financial_half
+ beginning_of_financial_half.months_ago(6)
+ end
- def financial_month_of(month)
- if month < start_month
- ::Date.new(year+1,month,1)
- else
- ::Date.new(year,month,1)
- end
+ def financial_month_of(month)
+ if month < start_month
+ Date.new(year+1,month,1)
+ else
+ Date.new(year,month,1)
end
+ end
- private
-
- def months_between
- soy = self.beginning_of_financial_year
- (self.month - soy.month) + 12 * (self.year - soy.year)
- end
+ private
- def start_month
- self.class.fy_start_month || FY_START_MONTH
- end
+ def months_between
+ soy = self.beginning_of_financial_year
+ (self.month - soy.month) + 12 * (self.year - soy.year)
+ end
+ def start_month
+ self.class.fy_start_month || FY_START_MONTH
end
end
View
5 lib/rising_sun/version.rb
@@ -0,0 +1,5 @@
+module RisingSun
+ module Fiscali
+ VERSION = "2.0.0"
+ end
+end
View
90 spec/fiscali_spec.rb
@@ -0,0 +1,90 @@
+require 'spec_helper'
+
+describe "fiscali" do
+ it "should be possible to read fiscal zone" do
+ Date.fiscal_zone.should be_nil
+ end
+
+ it "should be possible to write fiscal zone" do
+ Date.fiscal_zone = :india
+ Date.fiscal_zone.should eql(:india)
+ end
+
+ it "should be possible to read fiscal start month" do
+ Date.fy_start_month = 2
+ Date.fy_start_month.should eql(2)
+ end
+
+ it "should report financial year start" do
+ Date.fiscal_zone = :india
+ this_year = Date.today.year
+ Date.financial_year_start.should eql(Date.new(this_year,4,1))
+
+ Date.financial_year_start(2009).should eql(Date.new(2009,4,1))
+ end
+
+ context "should report correct date field" do
+ before(:each) do
+ @d = Date.new(2009,1,1)
+ end
+ it "should report correct financial year" do
+ @d.financial_year.should eql(2008)
+ end
+
+ it "should report correct beginning and of financial year" do
+ @d.beginning_of_financial_year.should eql(Date.new(2008,4,1))
+ @d.end_of_financial_year.should eql(Date.new(2009,3,31))
+ end
+
+ it "should report correct beginning of financial halves and quarters" do
+ @d.beginning_of_financial_h1.should eql(Date.new(2008,4,1))
+ @d.beginning_of_financial_h2.should eql(Date.new(2008,10,1))
+ @d.beginning_of_financial_q1.should eql(Date.new(2008,4,1))
+ @d.beginning_of_financial_q2.should eql(Date.new(2008,7,1))
+ @d.beginning_of_financial_q3.should eql(Date.new(2008,10,1))
+ @d.beginning_of_financial_q4.should eql(Date.new(2009,1,1))
+ end
+
+ it "should report correct end of financial halves and quarters" do
+ @d.end_of_financial_h1.should eql(Date.new(2008,9,30))
+ @d.end_of_financial_h2.should eql(Date.new(2009,3,31))
+ @d.end_of_financial_q1.should eql(Date.new(2008,6,30))
+ @d.end_of_financial_q2.should eql(Date.new(2008,9,30))
+ @d.end_of_financial_q3.should eql(Date.new(2008,12,31))
+ @d.end_of_financial_q4.should eql(Date.new(2009,3,31))
+ end
+
+ it "should report financial quarters" do
+ @d.financial_quarter.should eql('Q4 2008')
+ end
+
+ it "should report financial half" do
+ @d.financial_half.should eql('H2 2008')
+ Date.new(2009,11,30).financial_half.should eql("H2 2009")
+ end
+
+ it "should report next half and quarter" do
+ @d.next_financial_half.should eql(Date.new(2009,4,1))
+ Date.new(2009,6,1).next_financial_half.should eql(Date.new(2009,10,1))
+
+ @d.next_financial_quarter.should eql(Date.new(2009,4,1))
+ Date.new(2009,10,30).next_financial_quarter.should eql(Date.new(2010,1,1))
+ end
+
+ it "should report beginning of financial half and quarter" do
+ @d.beginning_of_financial_half.should eql(Date.new(2008,10,1))
+ @d.beginning_of_financial_quarter.should eql(Date.new(2009,1,1))
+ Date.new(2009,6,1).beginning_of_financial_half.should eql(Date.new(2009,4,1))
+ Date.new(2009,10,30).beginning_of_financial_quarter.should eql(Date.new(2009,10,1))
+ end
+
+ it "should report previous financial half and quarter" do
+ @d.previous_financial_half.should eql(Date.new(2008,4,1))
+ @d.previous_financial_quarter.should eql(Date.new(2008,10,1))
+ Date.new(2009,6,1).previous_financial_half.should eql(Date.new(2008,10,1))
+ Date.new(2009,10,30).previous_financial_quarter.should eql(Date.new(2009,7,1))
+ end
+
+ end
+
+end
View
5 spec/spec_helper.rb
@@ -0,0 +1,5 @@
+$:.unshift File.expand_path('..', __FILE__)
+$:.unshift File.expand_path('../../lib', __FILE__)
+require 'rspec'
+require 'fiscali'
+
View
62 test/fiscali_test.rb
@@ -1,62 +0,0 @@
-require 'test_helper'
-
-class FiscaliTest < ActiveSupport::TestCase
- test "date test" do
- Date.fiscal_zone = :india
- assert_equal(Date.fiscal_zone,:india)
- assert_equal(Date.fy_start_month,4)
- Date.fy_start_month = 2
- assert_equal(Date.fy_start_month,2)
-
- Date.fiscal_zone = :india
-
- d = Date.financial_year_start(2009)
- assert_equal(d,Date.new(2009,4,1),'Financial Year start not correct')
-
- d = Date.new(2009,1,1)
- assert_equal(d.financial_year,2008,'Financial Year is not correct')
- assert_equal(d.beginning_of_financial_year,Date.new(2008,4,1),'Beginning of FY is not correct')
- assert_equal(d.beginning_of_financial_h1,Date.new(2008,4,1),'Beginning of h1 is not correct')
- assert_equal(d.beginning_of_financial_h2,Date.new(2008,10,1),'Beginning of h2 is not correct')
- assert_equal(d.beginning_of_financial_q1,Date.new(2008,4,1),'Beginning of q1 is not correct')
- assert_equal(d.beginning_of_financial_q2,Date.new(2008,7,1),'Beginning of q2 is not correct')
- assert_equal(d.beginning_of_financial_q3,Date.new(2008,10,1),'Beginning of q3 is not correct')
- assert_equal(d.beginning_of_financial_q4,Date.new(2009,1,1),'Beginning of q4 is not correct')
-
- assert_equal(d.end_of_financial_year,Date.new(2009,3,31),'End of FY is not correct')
- assert_equal(d.end_of_financial_h1,Date.new(2008,9,30),'End of h1 is not correct')
- assert_equal(d.end_of_financial_h2,Date.new(2009,3,31),'End of h2 is not correct')
- assert_equal(d.end_of_financial_q1,Date.new(2008,6,30),'End of q1 is not correct')
- assert_equal(d.end_of_financial_q2,Date.new(2008,9,30),'End of q2 is not correct')
- assert_equal(d.end_of_financial_q3,Date.new(2008,12,31),'End of q3 is not correct')
- assert_equal(d.end_of_financial_q4,Date.new(2009,3,31),'End of q4 is not correct')
-
- assert_equal(d.financial_quarter,'Q4 2008','Financial Quarter is not correct')
- assert_equal(Date.new(2008,4,1).financial_quarter,'Q1 2008','Financial Quarter is not correct')
- assert_equal(d.financial_half,'H2 2008','Financial Half is not correct')
- assert_equal(Date.new(2009,11,30).financial_half,'H2 2009','Financial Half is not correct')
-
- assert_equal(d.next_financial_half,Date.new(2009,4,1),'Next Financial Half is not correct')
- assert_equal(d.next_financial_quarter,Date.new(2009,4,1),'Next Financial Quarter is not correct')
- assert_equal(Date.new(2009,6,1).next_financial_half,Date.new(2009,10,1), 'Next Financial Half is not correct')
- assert_equal(Date.new(2009,10,30).next_financial_quarter,Date.new(2010,1,1),'Next Financial Quarter is not correct')
-
- assert_equal(d.beginning_of_financial_half,
- Date.new(2008,10,1),'Beginning of Financial Half is not correct')
- assert_equal(d.beginning_of_financial_quarter,
- Date.new(2009,1,1),'Beginning of Financial Quarter is not correct')
- assert_equal(Date.new(2009,6,1).beginning_of_financial_half,
- Date.new(2009,4,1), 'Beginning of Financial Half is not correct')
- assert_equal(Date.new(2009,10,30).beginning_of_financial_quarter,
- Date.new(2009,10,1),'Beginning of Financial Quarter is not correct')
-
- assert_equal(d.previous_financial_half,
- Date.new(2008,4,1),'Previous Financial Half is not correct')
- assert_equal(d.previous_financial_quarter,
- Date.new(2008,10,1),'Previous Financial Quarter is not correct')
- assert_equal(Date.new(2009,6,1).previous_financial_half,
- Date.new(2008,10,1), 'Previous Financial Half is not correct')
- assert_equal(Date.new(2009,10,30).previous_financial_quarter,
- Date.new(2009,7,1),'Previous Financial Quarter is not correct')
- end
-end
View
3  test/test_helper.rb
@@ -1,3 +0,0 @@
-require 'rubygems'
-require 'active_support'
-require 'active_support/test_case'
Please sign in to comment.
Something went wrong with that request. Please try again.