month_year 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: 01b15d9956e7fae917685ceb3532c8bd05e40109
4
+ data.tar.gz: ca5d222878850f017a1bf2ce57fffc0861ef5394
5
+ SHA512:
6
+ metadata.gz: e9690972ec5c71d1b10b87eb3d810d23c61603f112ad67c24a6d4167785aa552adae7a2e159b3ada9106ce61ba444f04490724b8cb0dad0a713787cd2b51c42e
7
+ data.tar.gz: 8c33f16dac9420f1ee03a96f8ed4310908a4adb2bdb4baae92a280e03f774e2fa846d4eaa2efffabb57df184d570985bb429aa84dc8dc8049d803f617a133cd4
@@ -0,0 +1 @@
1
+ service_name: travis-ci
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ rvm:
5
+ - jruby
6
+ - 2.2.0
7
+ - 2.1.0
8
+ - 2.0.0
9
+
10
+ script: 'bundle exec rake'
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in month_year.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Daniel Illi
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,38 @@
1
+ # MonthYear
2
+
3
+ A MonthYear represents a specific month in a specific year.
4
+ It implements comparison operations, can be used to create ranges, (de-)serializes to/from Integer and can
5
+ be used for attributes of ActiveRecord Models.
6
+
7
+ Inspired by a [blog post](blog post) by [Robert Pankowecki](https://github.com/paneq/). Thanks!
8
+
9
+ [![Build Status](https://travis-ci.org/daniel-illi/month_year.svg?branch=master)](https://travis-ci.org/daniel-illi/month_year)
10
+ [![Coverage Status](https://coveralls.io/repos/github/daniel-illi/month_year/badge.svg?branch=master)](https://coveralls.io/github/daniel-illi/month_year?branch=master)
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'month_year'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install month_year
27
+
28
+ ## Usage
29
+
30
+ TODO: Write usage instructions here
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it ( https://github.com/daniel-illi/month_year/fork )
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create a new Pull Request
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ # Default directory to look in is `/specs`
5
+ # Run with `rake spec`
6
+ RSpec::Core::RakeTask.new(:spec) do |task|
7
+ task.rspec_opts = ['--color', '--format', 'documentation']
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,118 @@
1
+ require 'month_year/version'
2
+
3
+ class MonthYear
4
+
5
+ attr_reader :year, :month
6
+
7
+ class << self
8
+ # Instantiate a `MonthYear` object from a `Date`-like object
9
+ # (a `Date` or `Time` instance,
10
+ # or in fact from any object responding to :year and :month).
11
+ #
12
+ # Example:
13
+ # >> MonthYear.from_date(Date.today)
14
+ # => #<MonthYear:0x00000001f15c10 @month=12, @year=2016>
15
+ #
16
+ # Arguments:
17
+ # date: (Date)
18
+ #
19
+ def from_date(date)
20
+ raise ArgumentError unless [:year, :month].all? {|v| date.respond_to?(v) }
21
+ self.new(date.year, date.month)
22
+ end
23
+
24
+ def load(month_year)
25
+ raise_load_error(month_year) unless month_year.is_a?(Integer)
26
+ year = month_year / 100
27
+ month = month_year % 100
28
+ self.new(year, month)
29
+ end
30
+
31
+ def dump(month_year)
32
+ raise_dump_error(month_year) unless month_year.is_a?(self)
33
+ month_year.to_i
34
+ end
35
+ end
36
+
37
+ # Instantiate a new `MonthYear` object.
38
+ #
39
+ # Example:
40
+ # >> MonthYear.new(2016, 12)
41
+ # => #<MonthYear:0x00000001f15c10 @month=12, @year=2016>
42
+ #
43
+ # Arguments:
44
+ # year: (Integer)
45
+ # month: (Integer)
46
+ #
47
+ def initialize(year, month)
48
+ raise ArgumentError unless [year, month].all? {|v| Fixnum === v }
49
+ raise ArgumentError unless (1..12).cover?(month)
50
+ @year, @month = year, month
51
+ end
52
+
53
+ def ==(other)
54
+ other.class == self.class && (self <=> other) == 0
55
+ end
56
+ alias_method :eql?, :==
57
+
58
+ def <=>(other)
59
+ (year <=> other.year).nonzero? || month <=> other.month
60
+ end
61
+
62
+ def hash
63
+ to_i.hash
64
+ end
65
+
66
+ def succ
67
+ if month == 12
68
+ self.class.new(year+1, 1)
69
+ else
70
+ self.class.new(year, month+1)
71
+ end
72
+ end
73
+ alias_method :next, :succ
74
+
75
+ # Return the numeric representation of this `MonthYear` instance.
76
+ #
77
+ # Example:
78
+ # >> MonthYear.new(2016, 12).to_i
79
+ # => 201612
80
+ #
81
+ def to_i
82
+ year * 100 + month
83
+ end
84
+
85
+ # Return the string representation of this `MonthYear` instance.
86
+ #
87
+ # Example:
88
+ # >> MonthYear.new(2016, 12).to_s
89
+ # => "2016-12"
90
+ #
91
+ def to_s
92
+ "#{year}-#{month}"
93
+ end
94
+
95
+ protected
96
+
97
+ def state
98
+ [year, month]
99
+ end
100
+
101
+ private
102
+
103
+ def active_record?
104
+ Object.const_defined?(::ActiveRecord::SerializationTypeMismatch)
105
+ end
106
+
107
+ def argument_error_class
108
+ active_record? ? ::ActiveRecord::SerializationTypeMismatch : ArgumentError
109
+ end
110
+
111
+ def throw_load_error(obj)
112
+ raise argument_error_class, "Argument was supposed to be an Integer, but was a #{obj.class}. -- #{obj.inspect}"
113
+ end
114
+
115
+ def throw_dump_error(obj)
116
+ raise argument_error_class, "Argument was supposed to be a #{self}, but was a #{obj.class}. -- #{obj.inspect}"
117
+ end
118
+ end
@@ -0,0 +1,3 @@
1
+ class MonthYear
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'month_year/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "month_year"
8
+ spec.version = MonthYear::VERSION
9
+ spec.authors = ["Daniel Illi"]
10
+ spec.email = ["daniel@illi.zone"]
11
+ spec.summary = %q{MonthYear represents a specific month in a specific year.}
12
+ spec.description = <<-DESC
13
+ A MonthYear instance represents a specific month in a specific year.
14
+ It implements comparison operations, can be used to create ranges, (de-)serializes to/from Integer and can
15
+ be used for attributes of ActiveRecord Models.
16
+ DESC
17
+ spec.homepage = "https://github.com/daniel-illi/month_year"
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0")
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "coveralls"
29
+ spec.add_development_dependency "pry"
30
+ end
@@ -0,0 +1,46 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ require 'month_year'
5
+ require 'pry'
6
+
7
+ describe MonthYear do
8
+ let(:date){ Date.new(2001, 2, 3) }
9
+ let(:time){ Time.new(2001, 2, 3, 4, 5, 6, "+01:00") }
10
+ let(:month_year){ MonthYear.new(2001, 2) }
11
+
12
+ context "::from_date" do
13
+ it 'instantiates with date argument' do
14
+ month_year = MonthYear.from_date(date)
15
+ expect(month_year.year).to eq(date.year)
16
+ expect(month_year.month).to eq(date.month)
17
+ end
18
+
19
+ it 'instantiates with time argument' do
20
+ month_year = MonthYear.from_date(time)
21
+ expect(month_year.year).to eq(time.year)
22
+ expect(month_year.month).to eq(time.month)
23
+ end
24
+
25
+ it 'thows error with string argument' do
26
+ expect { MonthYear.from_date('200102') }.to raise_error(ArgumentError)
27
+ end
28
+ end
29
+
30
+ it '::dump returns numeric value' do
31
+ expect(MonthYear.dump(month_year)).to eq(month_year.to_i)
32
+ end
33
+
34
+ it '::load instantiates from numeric value' do
35
+ expect(MonthYear.load(month_year.to_i)).to eq(month_year)
36
+ end
37
+
38
+ it '#eql?'
39
+ it '#=='
40
+ it '#<=>'
41
+ it '#hash'
42
+ it '#succ'
43
+ it '#next'
44
+ it '#to_i'
45
+ it '#to_s'
46
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: month_year
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Illi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-01 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: |
84
+ A MonthYear instance represents a specific month in a specific year.
85
+ It implements comparison operations, can be used to create ranges, (de-)serializes to/from Integer and can
86
+ be used for attributes of ActiveRecord Models.
87
+ email:
88
+ - daniel@illi.zone
89
+ executables: []
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - ".coveralls.yml"
94
+ - ".gitignore"
95
+ - ".travis.yml"
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - lib/month_year.rb
101
+ - lib/month_year/version.rb
102
+ - month_year.gemspec
103
+ - spec/year_month_spec.rb
104
+ homepage: https://github.com/daniel-illi/month_year
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.5.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: MonthYear represents a specific month in a specific year.
128
+ test_files:
129
+ - spec/year_month_spec.rb