date_plus 0.0.1

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: ac057d70451cc0db7f56314183931c251834a5e5
4
+ data.tar.gz: c98b42f0ffa14e3015af8c3cca9624aca895a28e
5
+ SHA512:
6
+ metadata.gz: 7dffd0416344b012f538c06de7047342a66fecc0e18e05e6f2a6c5d5b4ed32fd5502961747aa02ddfdfa56ad4193b5e1db44f883aafed4200e041d665b6602b4
7
+ data.tar.gz: 4ad3656e75714bc4c8903df047236e90ba3a20caebfbd55eaababfd98afeed89c40cf2fd2c843355a7de0bd3499a0bcfee535960d6ec778d31e04b47265831e3
@@ -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 date_plus.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Yaniv Savir
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,103 @@
1
+ # DatePlus
2
+
3
+ Additional methods for date objects.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'date_plus'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install date_plus
18
+
19
+ ## Usage
20
+
21
+ Date_plus adds methods directly to instances of Date objects:
22
+
23
+ ```ruby
24
+ date_object = Date.new(2014, 7, 24)
25
+ # => #<Date: 2014-07-24 ((2456863j,0s,0n),+0s,2299161j)>
26
+ ```
27
+
28
+ ### Methods for weekday and month names
29
+
30
+ ```ruby
31
+ date_object.weekday_name
32
+ => "Thursday"
33
+
34
+ date_object.month_name
35
+ => "July"
36
+ ```
37
+
38
+ ### Find a specific future date
39
+ ```ruby
40
+ date_object.next_instance_of(:month => 12, :day => 5)
41
+ # => #<Date: 2014-12-05 ((2456997j,0s,0n),+0s,2299161j)>
42
+
43
+ date_object.next_instance_of(:month_name => 'July', :day => 4, :weekday_name => 'Sunday')
44
+ # => #<Date: 2021-07-04 ((2459400j,0s,0n),+0s,2299161j)>
45
+
46
+ # next_instance_of accepts any combination of date methods
47
+ ```
48
+
49
+ ### Start and end of time periods
50
+
51
+ ```ruby
52
+ date_object.start_of_week
53
+ # => #<Date: 2014-07-20 ((2456859j,0s,0n),+0s,2299161j)>
54
+
55
+ date_object.end_of_week
56
+ # => #<Date: 2014-07-26 ((2456865j,0s,0n),+0s,2299161j)>
57
+
58
+ date_object.start_of_month
59
+ # => #<Date: 2014-07-01 ((2456840j,0s,0n),+0s,2299161j)>
60
+
61
+ date_object.end_of_month
62
+ # => #<Date: 2014-07-31 ((2456870j,0s,0n),+0s,2299161j)>
63
+
64
+ date_object.start_of_year
65
+ # #<Date: 2014-01-01 ((2456659j,0s,0n),+0s,2299161j)>
66
+
67
+ date_object.end_of_year
68
+ # => #<Date: 2014-12-31 ((2457023j,0s,0n),+0s,2299161j)>
69
+ ```
70
+
71
+ ### Next or future instance of specific date values
72
+
73
+ ```ruby
74
+ date_object.future_instance_of_weekday('sunday')
75
+ # => #<Date: 2014-07-27 ((2456866j,0s,0n),+0s,2299161j)>
76
+
77
+ date_object.future_instance_of_day(21)
78
+ # => #<Date: 2014-08-21 ((2456891j,0s,0n),+0s,2299161j)>
79
+
80
+ date_object.future_instance_of_month('March')
81
+ # => #<Date: 2015-03-01 ((2457083j,0s,0n),+0s,2299161j)>
82
+
83
+ # Each of the above methods my optionally be given a second integer parameter n
84
+ # to return the nth instance from the date
85
+ date_object.future_instance_of_weekday('sunday', 4)
86
+ # => #<Date: 2014-08-17 ((2456887j,0s,0n),+0s,2299161j)>
87
+
88
+ # a second integer argument will find the nth future weekday
89
+ date_object.future_instance_of_month('march', 6)
90
+ # => #<Date: 2020-03-01 ((2458910j,0s,0n),+0s,2299161j)>
91
+ ```
92
+
93
+ ## Contributing
94
+
95
+ 1. Fork it
96
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
97
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
98
+ 4. Push to the branch (`git push origin my-new-feature`)
99
+ 5. Create new Pull Request
100
+
101
+ ### Things to Contribute
102
+ * Optimization
103
+ * Additional appropriate methods
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'date_plus/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "date_plus"
8
+ spec.version = DatePlus::VERSION
9
+ spec.authors = ["Yaniv Savir"]
10
+ spec.email = ["saviry@gmail.com"]
11
+ spec.description = %q{Additional methods for date objects}
12
+ spec.summary = %q{Filling in necessary holes and eliminating unnecessary steps in date objects}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,119 @@
1
+ require "date_plus/version"
2
+ require 'date'
3
+
4
+ class Date
5
+
6
+ def weekday_name
7
+ self.strftime('%A')
8
+ end
9
+
10
+ def month_name
11
+ self.strftime('%B')
12
+ end
13
+
14
+ def start_of_week
15
+ self - cwday
16
+ end
17
+
18
+ def start_of_month
19
+ self - day + 1
20
+ end
21
+
22
+ def start_of_year
23
+ self - yday + 1
24
+ end
25
+
26
+ def end_of_week
27
+ self + (6 - cwday)
28
+ end
29
+
30
+ def end_of_month
31
+ Date.civil(year, month, -1)
32
+ end
33
+
34
+ def end_of_year
35
+ Date.civil(year, -1, -1)
36
+ end
37
+
38
+ # options can accept any method that responds to date
39
+ # so long as the criteria matches the output of that method
40
+ def next_instance_of(options)
41
+ options.each { |k, v| options[k] = v.downcase.capitalize if v.class == String }
42
+ return check_dates_by_year(self, end_of_year, options)
43
+ end
44
+
45
+ def future_instance_of_weekday(weekday, instances_away=1)
46
+ raise 'Argument passed is not a weekday' unless weekday.class == String || !weekday_names.include?(weekday.downcase.capitalize)
47
+ fixnum_or_nil_or_error(instances_away)
48
+ self.step(self + (7 * instances_away), step=+1).reverse_each do |current_day|
49
+ return current_day if current_day.weekday_name == weekday.downcase.capitalize
50
+ end
51
+ end
52
+
53
+ def future_instance_of_day(day, instances_away=1)
54
+ valid_day_or_error(day)
55
+ fixnum_or_nil_or_error(instances_away)
56
+ self.step(self + (31 * instances_away), step=+1).reverse_each do |current_day|
57
+ return current_day if current_day.day === day
58
+ end
59
+ end
60
+
61
+ def future_instance_of_month(month_name, instances_away=1)
62
+ valid_month_or_error(month_name)
63
+ fixnum_or_nil_or_error(instances_away)
64
+ months = []
65
+ first_of_month = Date.today.start_of_month
66
+ (instances_away * 12).times do
67
+ months.push(first_of_month)
68
+ first_of_month = first_of_month.next_month
69
+ end
70
+ months.reverse.each {|first_of_month| return first_of_month if first_of_month.month == Date.parse(month_name).month }
71
+ end
72
+
73
+ def future_year(instances_away)
74
+ fixnum_nil_or_error(instances_away)
75
+ year = Date.today.start_of_year
76
+ years = []
77
+ (instances_away + 1).times {years.push(year); year = year.next_year}
78
+ return years.last
79
+ end
80
+
81
+ private
82
+
83
+ def weekday_names
84
+ ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
85
+ end
86
+
87
+ def month_names
88
+ ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
89
+ end
90
+
91
+ def matches_value(field, value)
92
+ self.send(field) == value
93
+ end
94
+
95
+ def valid_condition(field, condition)
96
+ self.send(field) == condition
97
+ end
98
+
99
+ def check_dates_by_year(start_date, end_date, options)
100
+ start_date.step(end_date, step=+1) do |date|
101
+ return date if options.all? { |k, v| date.valid_condition(k, v) }
102
+ end
103
+ next_year_start = end_date + 1
104
+ check_dates_by_year(next_year_start, next_year_start.end_of_year, options)
105
+ end
106
+
107
+ def fixnum_or_nil_or_error(arg)
108
+ raise 'Argument passed is not an integer' unless arg.class == Fixnum || arg.nil?
109
+ end
110
+
111
+ def valid_day_or_error(arg)
112
+ raise 'Argument passed is not a valid day' unless arg.class == Fixnum && 0 < arg && arg < 32
113
+ end
114
+
115
+ def valid_month_or_error(arg)
116
+ raise 'Argument passed is not a valid month' unless arg.class == String && month_names.include?(arg.downcase.capitalize)
117
+ end
118
+
119
+ end
@@ -0,0 +1,3 @@
1
+ module DatePlus
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: date_plus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yaniv Savir
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Additional methods for date objects
42
+ email:
43
+ - saviry@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - date_plus.gemspec
54
+ - lib/date_plus.rb
55
+ - lib/date_plus/version.rb
56
+ homepage: ''
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.1.11
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Filling in necessary holes and eliminating unnecessary steps in date objects
80
+ test_files: []