monthify 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.
data/.gitignore ADDED
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in month.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec'
7
+ gem 'activesupport'
8
+ gem 'guard'
9
+ gem 'guard-rspec'
10
+ gem 'rb-fsevent'
data/Guardfile ADDED
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara request specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
24
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Matt Royal
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,29 @@
1
+ # Monthify
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'monthify'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install monthify
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 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,13 @@
1
+ module Monthify
2
+ module CoreExt
3
+ module Object
4
+ def to_month
5
+ ::Kernel::Month(self)
6
+ end
7
+ end
8
+ end
9
+ end
10
+
11
+ class ::Object
12
+ include Monthify::CoreExt::Object
13
+ end
@@ -0,0 +1,6 @@
1
+ require 'monthify/core_ext/object'
2
+
3
+ module Monthify
4
+ module CoreExt
5
+ end
6
+ end
@@ -0,0 +1,104 @@
1
+ require "active_support/core_ext"
2
+
3
+ module Monthify
4
+ class Month
5
+ include Comparable
6
+
7
+ attr_reader :month, :year
8
+
9
+ def self.current
10
+ today = Date.current
11
+ new(today.year, today.month)
12
+ end
13
+
14
+ def self.containing(datish)
15
+ Month.new(datish.year, datish.month)
16
+ end
17
+
18
+ def self.load(date_yaml)
19
+ date = YAML.load(date_yaml)
20
+ Month.containing(date)
21
+ end
22
+
23
+ def self.dump(month)
24
+ YAML.dump(month.first_day)
25
+ end
26
+
27
+ def initialize(year, month)
28
+ @year, @month = year, month
29
+ end
30
+
31
+ def first_day
32
+ Date.new(year, month, 1)
33
+ end
34
+
35
+ def last_day
36
+ first_day.end_of_month
37
+ end
38
+
39
+ def first_moment
40
+ first_day.beginning_of_day
41
+ end
42
+
43
+ def last_moment
44
+ last_day.end_of_day
45
+ end
46
+
47
+ def previous
48
+ self - 1.month
49
+ end
50
+
51
+ def next
52
+ self + 1.month
53
+ end
54
+
55
+ def date_range
56
+ Range.new(first_day, last_day)
57
+ end
58
+
59
+ def time_range
60
+ Range.new(first_moment, last_moment)
61
+ end
62
+
63
+ def contains?(datish)
64
+ date = datish.to_date
65
+ year == date.year && month == date.month
66
+ end
67
+
68
+ def +(duration)
69
+ Month.containing(first_day + duration)
70
+ end
71
+
72
+ def -(duration)
73
+ Month.containing(first_day - duration)
74
+ end
75
+
76
+ def <=>(other)
77
+ if year == other.year
78
+ month <=> other.month
79
+ else
80
+ year <=> other.year
81
+ end
82
+ end
83
+
84
+ def hash
85
+ [self.class, year, month].hash
86
+ end
87
+
88
+ def to_s
89
+ "%d/%02d" % [year, month]
90
+ end
91
+ end
92
+ end
93
+
94
+ module ::Kernel
95
+ def Month(convertee)
96
+ if convertee.is_a?(Month)
97
+ convertee
98
+ elsif convertee.respond_to?(:to_date)
99
+ Month.containing(convertee.to_date)
100
+ else
101
+ raise ArgumentError, "Don't know how to convert #{convertee.inspect} to Month"
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,3 @@
1
+ module Monthify
2
+ VERSION = "0.0.1"
3
+ end
data/lib/monthify.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'monthify/version'
2
+ require 'monthify/month'
3
+ require 'monthify/core_ext'
4
+
5
+ module Monthify
6
+ ::Month = Monthify::Month
7
+ end
data/monthify.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'monthify/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "monthify"
8
+ gem.version = Monthify::VERSION
9
+ gem.authors = ["Matt Royal"]
10
+ gem.email = ["mroyal@gmail.com"]
11
+ gem.description = %q{The missing Month class every project ends up needing}
12
+ gem.summary = %q{}
13
+ gem.homepage = "https://github.com/matt-royal/monthify"
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
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+ require 'monthify'
3
+
4
+
5
+ describe Monthify::CoreExt::Object do
6
+ it 'is included in Object' do
7
+ Object.should be_a(Monthify::CoreExt::Object)
8
+ end
9
+ end
10
+
11
+ describe ::Object do
12
+ describe "#to_month" do
13
+ it 'returns the result of Kernel::Month()' do
14
+ receiver = ::Object.new
15
+ result = double(:result)
16
+
17
+ ::Kernel.should_receive(:Month).with(receiver).
18
+ and_return(result)
19
+ receiver.to_month.should == result
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,253 @@
1
+ require 'spec_helper'
2
+ require 'monthify'
3
+
4
+ describe Month do
5
+ describe '.current' do
6
+ it 'is the current month' do
7
+ Month.current.month.should == Date.current.month
8
+ Month.current.year.should == Date.current.year
9
+ end
10
+ end
11
+
12
+ describe '.containing' do
13
+ it 'returns the month containing the date' do
14
+ Month.containing(Date.new(2011, 1, 15)).should == Month.new(2011, 1)
15
+ end
16
+
17
+ it 'returns the month containing the time' do
18
+ Month.containing(Time.local(2012, 3, 15, 6, 9)).should == Month.new(2012, 3)
19
+ end
20
+ end
21
+
22
+ context 'serialization:' do
23
+ describe '.load' do
24
+ it 'converts date YAML into a Month' do
25
+ date_yaml = YAML.dump(Date.new(2011, 5, 22))
26
+ Month.load(date_yaml).should == Month.new(2011, 5)
27
+ end
28
+ end
29
+
30
+ describe '.dump' do
31
+ it 'converts a Month to the YAML representation of its first day' do
32
+ Month.dump(Month.new(2013, 8)).should == YAML.dump(Date.new(2013, 8, 1))
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ describe "#initialize" do
39
+ it 'sets month and year' do
40
+ Month.new(2012,1).month.should == 1
41
+ Month.new(2012,1).year.should == 2012
42
+ end
43
+ end
44
+
45
+ describe "#first_day" do
46
+ it 'is the first day of the month' do
47
+ Month.new(2011, 7).first_day.should == Date.new(2011, 7, 1)
48
+ end
49
+ end
50
+
51
+ describe "#last_day" do
52
+ it 'is the last day of the month' do
53
+ Month.new(2011, 7).last_day.should == Date.new(2011, 7, 31)
54
+ Month.new(2011, 2).last_day.should == Date.new(2011, 2, 28)
55
+ end
56
+ end
57
+
58
+
59
+ describe "#first_moment" do
60
+ it 'is the time at the start of the first day of the month' do
61
+ Month.new(2011, 7).first_moment.should == Time.local(2011, 7, 1, 0, 0, 0, 0)
62
+ end
63
+ end
64
+
65
+ describe "#last_moment" do
66
+ it 'is the time at the end of the last day of the month' do
67
+ Month.new(2011, 7).last_moment.usec.should == 999_999
68
+ Month.new(2011, 7).last_moment.should == Time.local(2011, 7, 31).end_of_day
69
+ end
70
+ end
71
+
72
+ describe "#next" do
73
+ it 'is the next month' do
74
+ Month.new(2011, 11).next.should == Month.new(2011, 12)
75
+ Month.new(2011, 12).next.should == Month.new(2012, 1)
76
+ end
77
+ end
78
+
79
+ describe "#previous" do
80
+ it 'is the previous month' do
81
+ Month.new(2011, 2).previous.should == Month.new(2011, 1)
82
+ Month.new(2011, 1).previous.should == Month.new(2010, 12)
83
+ end
84
+ end
85
+
86
+ describe "#date_range" do
87
+ let(:month) { Month.new(2011, 3) }
88
+
89
+ it 'is the range of days in the month' do
90
+ month.date_range.should == Range.new(month.first_day, month.last_day)
91
+ end
92
+ end
93
+
94
+ describe "#time_range" do
95
+ let(:month) { Month.new(2011, 3) }
96
+
97
+ it 'is the range of times from the start to the end of the month' do
98
+ Month.new(2011, 3).time_range.should == Range.new(month.first_moment, month.last_moment)
99
+ end
100
+ end
101
+
102
+ describe "#contains?" do
103
+ let(:month) { Month.new(2012, 6) }
104
+ subject { month.contains?(datish) }
105
+
106
+ context 'with a Date in the month' do
107
+ let(:datish) { Date.new(2012, 6, 7) }
108
+ it { should == true }
109
+ end
110
+
111
+ context 'with a Date in another month' do
112
+ let(:datish) { Date.new(2012, 7, 1) }
113
+ it { should == false }
114
+ end
115
+
116
+ context 'with a Time in the month' do
117
+ let(:datish) { Time.local(2012, 6, 7, 12, 30) }
118
+ it { should == true }
119
+ end
120
+
121
+ context 'with a Time in another month' do
122
+ let(:datish) { Time.local(2012, 7, 1, 0, 0) }
123
+ it { should == false }
124
+ end
125
+
126
+ context 'with an object that has a #to_date in the month' do
127
+ let(:datish) {
128
+ double(:datish_in_month, to_date: Date.new(2012, 6, 7))
129
+ }
130
+ it { should == true }
131
+ end
132
+
133
+ context 'with an object that has a #to_date in another month' do
134
+ let(:datish) {
135
+ double(:datish_in_another_month, to_date: Date.new(2012, 7, 1))
136
+ }
137
+
138
+ it { should == false }
139
+ end
140
+ end
141
+
142
+ describe "comparison:" do
143
+ let(:dec_2010) { Month.new(2010, 12) }
144
+ let(:jan_2011) { Month.new(2011, 1) }
145
+ let(:jan_2011_dup) { Month.new(2011, 1) }
146
+ let(:feb_2011) { Month.new(2011, 2) }
147
+
148
+ describe "==" do
149
+ it 'is true for two copies of the same month' do
150
+ jan_2011.should == jan_2011_dup
151
+ end
152
+ it 'is false for a different month' do
153
+ jan_2011.should_not == feb_2011
154
+ end
155
+ it 'is false for a non-month' do
156
+ jan_2011.should_not == Object.new
157
+ end
158
+ end
159
+
160
+ describe '<=>' do
161
+ it 'is 0 for the same month' do
162
+ (jan_2011 <=> jan_2011_dup).should == 0
163
+ end
164
+
165
+ it 'is 1 for an earlier month' do
166
+ (jan_2011 <=> dec_2010).should == 1
167
+ end
168
+
169
+ it 'is -1 for a later month' do
170
+ (jan_2011 <=> feb_2011).should == -1
171
+ end
172
+ end
173
+
174
+ it 'is Comparable' do
175
+ Month.current.should be_a(Comparable)
176
+ end
177
+ end
178
+
179
+ describe "+" do
180
+ let(:month) { Month.new(2012, 8) }
181
+
182
+ it 'correctly adds month durations' do
183
+ (month + 1.month).should == month.next
184
+ (month + 6.months).should == Month.new(2013, 2)
185
+ end
186
+
187
+ it 'correctly adds year durations' do
188
+ (month + 1.year).should == Month.new(2013, 8)
189
+ (month + 50.years).should == Month.new(2062, 8)
190
+ end
191
+ end
192
+
193
+ describe "-" do
194
+ let(:month) { Month.new(2012, 8) }
195
+
196
+ it 'correctly subtracts month durations' do
197
+ (month - 1.month).should == month.previous
198
+ (month - 6.months).should == Month.new(2012, 2)
199
+ end
200
+
201
+ it 'correctly subtracts year durations' do
202
+ (month - 1.year).should == Month.new(2011, 8)
203
+ (month - 50.years).should == Month.new(1962, 8)
204
+ end
205
+ end
206
+
207
+ describe "#hash" do
208
+ it 'is the same for two equal months' do
209
+ Month.new(2010, 3).hash.should == Month.new(2010, 3).hash
210
+ end
211
+
212
+ it 'is different for two different months' do
213
+ Month.new(2010, 3).hash.should_not == Month.new(2000, 1).hash
214
+ end
215
+ end
216
+
217
+ describe "#to_s" do
218
+ it 'is in the format YYYY/MM' do
219
+ Month.new(2010, 2).to_s.should == '2010/02'
220
+ end
221
+ end
222
+
223
+ describe "Kernel::Month()" do
224
+ it 'returns Month objects unchanged' do
225
+ month = Month.current
226
+ Kernel::Month(month).should == month
227
+ Kernel::Month(month).should eq month
228
+ end
229
+
230
+ it 'converts a date to its Month' do
231
+ Kernel::Month(Date.current).should == Month.current
232
+ end
233
+
234
+ it 'converts a time to its Month' do
235
+ Kernel::Month(Time.now).should == Month.current
236
+ end
237
+
238
+ it 'converts an object that has a date representation to the right Month' do
239
+ datish = double(:datish, to_date: Date.new(2001, 4))
240
+ Kernel::Month(datish).should == Month.new(2001, 4)
241
+ end
242
+
243
+ it 'raises for other inputs' do
244
+ expect {
245
+ Kernel::Month(0)
246
+ }.to raise_exception(ArgumentError)
247
+
248
+ expect {
249
+ Kernel::Month(nil)
250
+ }.to raise_exception(ArgumentError)
251
+ end
252
+ end
253
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monthify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt Royal
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-24 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
+ description: The missing Month class every project ends up needing
31
+ email:
32
+ - mroyal@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .rspec
39
+ - Gemfile
40
+ - Guardfile
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - lib/monthify.rb
45
+ - lib/monthify/core_ext.rb
46
+ - lib/monthify/core_ext/object.rb
47
+ - lib/monthify/month.rb
48
+ - lib/monthify/version.rb
49
+ - monthify.gemspec
50
+ - spec/lib/monthify/core_ext/object_spec.rb
51
+ - spec/lib/monthify/month_spec.rb
52
+ - spec/spec_helper.rb
53
+ homepage: https://github.com/matt-royal/monthify
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.24
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: ''
77
+ test_files:
78
+ - spec/lib/monthify/core_ext/object_spec.rb
79
+ - spec/lib/monthify/month_spec.rb
80
+ - spec/spec_helper.rb