split_time 0.0.2

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 089fde51d4d389b8b158e51e67a40b8fc2ecc67c
4
+ data.tar.gz: 7b713408df3c509978892a52bed3204f365ecdcd
5
+ SHA512:
6
+ metadata.gz: f498dc47f6c0c1effabc8fcefa1c2af2708e2a8091527b32684828988e862a0096216d6940c1295502f69f951821316c09c70328d11222db11289609e7023425
7
+ data.tar.gz: 764947a38c470acdd9c4272e9c819840fe2d281bae78345dd9d06a70a6e360b82887a1bbfd3663ca191a45f5f4ede5ad9e98595946888e8369f84b365cf57d80
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in split_time.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ershad Kunnakkadan
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.
@@ -0,0 +1,29 @@
1
+ # SplitTime
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'split_time'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install split_time
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ require "split_time/version"
2
+ require "split_time/compute"
3
+ require "split_time/months"
4
+
5
+ module SplitTime
6
+ end
@@ -0,0 +1,82 @@
1
+ require 'ostruct'
2
+
3
+ module SplitTime
4
+ class Compute
5
+
6
+ def initialize(start_date, end_date)
7
+ @start_date = parse_date start_date
8
+ @end_date = parse_date end_date
9
+
10
+ @years = ( @start_date.year .. @end_date.year ).to_a
11
+ end
12
+
13
+ def time_span
14
+ @years.inject({}) do |hash, year|
15
+ hash.merge({year => years(year)})
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def parse_date(date)
22
+ OpenStruct.new Date._parse(date)
23
+ end
24
+
25
+ def compute_month(months, start_or_end, c_days, all_days_for_all = false)
26
+ months.inject({}) do |hash, month|
27
+ hash.merge!({
28
+ month => { days: (start_or_end == month && c_days.is_a?(Array) ? c_days : "all_days" ) }
29
+ })
30
+ end
31
+ end
32
+
33
+ def years(year)
34
+ c_months = months(year)
35
+ c_days = days(year)
36
+
37
+ if c_months == "full year" && c_days == "full months"
38
+ "full_year"
39
+ else
40
+ compute_month(c_months, (@years.first == year ? c_months.first : c_months.last), c_days)
41
+ end
42
+ end
43
+
44
+ def months(year)
45
+ months_ids = case year
46
+ when @years.last
47
+ ( 1 .. @end_date.mon ).to_a
48
+ when @years.first
49
+ ( @start_date.mon .. 12 ).to_a
50
+ else
51
+ ( 1 .. 12 ).to_a
52
+ end
53
+
54
+ if months_ids.count == 12
55
+ "full year"
56
+ else
57
+ months_ids.map{ |month_id| SplitTime::Months.all[month_id] }
58
+ end
59
+ end
60
+
61
+ def days(year)
62
+ month_days = case year
63
+ when @years.last
64
+ ( 1 .. @end_date.mday ).to_a
65
+ when @years.first
66
+ ( @start_date.mday .. SplitTime::Months.days_in_month(@start_date.mon, @start_date.year) ).to_a
67
+ else
68
+ ( 1 .. SplitTime::Months.days_in_month(@start_date.mon, @start_date.year) ).to_a
69
+ end
70
+
71
+ if month_days.count == SplitTime::Months.days_in_month(@start_date.mon, @start_date.year)
72
+ if months(year) == "full year"
73
+ "full months"
74
+ else
75
+ ""
76
+ end
77
+ else
78
+ month_days
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,14 @@
1
+ module SplitTime
2
+ class Months
3
+ COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
4
+
5
+ def self.all
6
+ [nil, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'July', 'Aug', 'Sept', 'Oct' , 'Nov', 'Dec']
7
+ end
8
+
9
+ def self.days_in_month(month, year = Time.now.year)
10
+ return 29 if month == 2 && Date.gregorian_leap?(year)
11
+ COMMON_YEAR_DAYS_IN_MONTH[month]
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module SplitTime
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/split_time/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Ershad K"]
6
+ gem.email = ["ershad@bangthetable.com"]
7
+ gem.description = %q{Split time into largest chunks}
8
+ gem.summary = %q{A ruby gem to split time into largest chunks}
9
+ gem.homepage = "https://github.com/ershad/split_time"
10
+ gem.licenses = ["MIT License"]
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "split_time"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = SplitTime::VERSION
18
+
19
+ gem.add_development_dependency "rspec"
20
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: split_time
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ershad K
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Split time into largest chunks
28
+ email:
29
+ - ershad@bangthetable.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - Gemfile
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - lib/split_time.rb
40
+ - lib/split_time/compute.rb
41
+ - lib/split_time/months.rb
42
+ - lib/split_time/version.rb
43
+ - split_time.gemspec
44
+ homepage: https://github.com/ershad/split_time
45
+ licenses:
46
+ - MIT License
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.0.6
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: A ruby gem to split time into largest chunks
68
+ test_files: []