year_week 0.2.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 990238f50dc54ddaad7e70b650b9cc7b462b3b71
4
+ data.tar.gz: 67f470d6fa83f87551785b47708116b8623fadd5
5
+ SHA512:
6
+ metadata.gz: 3b1fb6467498ed951b94a93c0e8f3d04284d84116413874677f965841a527b3e407a4ebbe011fd03510feeea315f550fe679c2bb8fc0976490f9839edeb027d3
7
+ data.tar.gz: 38c914449d24fffddecdbdbe7225ce1e69ccb323909d0820d1d852800481cec81839a584c938527140d1351e6da7cd8bbeddf3613d35ea6c730c47c759a5eece
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in year_week.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Gert Goet
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # YearWeek
2
+
3
+ Makes working with weeks more fun.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'year_week'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install year_week
20
+
21
+ ## Usage
22
+
23
+
24
+ ```ruby
25
+ # Create:
26
+ > YearWeek.new(2016, 1)
27
+ => #<YearWeek year=2016, week=1>
28
+
29
+ > YearWeek.for_date(Date.parse('2016-1-4'))
30
+ => #<YearWeek year=2016, week=1>
31
+
32
+ # The current week:
33
+ > YearWeek.current
34
+ => #<YearWeek year=2016, week=9>
35
+
36
+ # 'Change':
37
+ # Weeks are immutable. By using `#with` you create a new week:
38
+ > YearWeek.current.with(week: 9)
39
+ => #<YearWeek year=2016, week=9>
40
+
41
+ # next and previous:
42
+ > YearWeek.new(2016, 1).succ == YearWeek.new(2016, 3).pred
43
+ => true
44
+
45
+ # Comparing
46
+ # Equality is based on value:
47
+ > YearWeek.current.with(week: 8) == YearWeek.new(2016, 8)
48
+ => true
49
+
50
+ > YearWeek.current.with(week: 8) < YearWeek.current.with(week: 9)
51
+ => true
52
+
53
+ # Check for inclusion:
54
+ > YearWeek.new(2016, 1).include?(Date.parse('2016-1-4'))
55
+ => true
56
+ ```
57
+
58
+ ### ActiveRecord/Arel
59
+
60
+ Given a `User`-class and a week `@week` you can do the following:
61
+
62
+ ```ruby
63
+ # All users created during the week:
64
+ User.where(created_at: @week)
65
+
66
+ # all users created before the week:
67
+ user_table = User.arel_table
68
+ User.where(@week.gt(user_table[:created_at]))
69
+ # ...including users created during week
70
+ User.where(@week.gteq(user_table[:created_at]))
71
+
72
+ # similar to get all users created after a week:
73
+ User.where(@week.lt(user_table[:created_at]))
74
+ # ...including users created during week
75
+ User.where(@week.lteq(user_table[:created_at]))
76
+ ```
77
+
78
+ ## Development
79
+
80
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
81
+
82
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
83
+
84
+ ## TODO
85
+
86
+ - [ ] range should have datetimes
87
+ - [ ] use ISO8601 for `#to_s`
88
+ - [ ] make it work without AR
89
+ - [ ] add `YearWeek()`
90
+ - [ ] range of weeks `YearWeek.new(2016, 1)...YearWeek.new(2016, 3)`
91
+
92
+ ## License
93
+
94
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
95
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "year_week"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/rake ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require "pathname"
10
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require "rubygems"
14
+ require "bundler/setup"
15
+
16
+ load Gem.bin_path("rake", "rake")
data/bin/rspec ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rspec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require "pathname"
10
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require "rubygems"
14
+ require "bundler/setup"
15
+
16
+ load Gem.bin_path("rspec-core", "rspec")
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/year_week.rb ADDED
@@ -0,0 +1,65 @@
1
+ require 'values'
2
+ require 'active_record'
3
+
4
+ class YearWeek < Value.new(:year, :week)
5
+ include Comparable
6
+
7
+ def <=>(other)
8
+ to_s <=> other.to_s
9
+ end
10
+
11
+ def to_s
12
+ "#{@year}-#{@week}"
13
+ end
14
+
15
+ def include?(other)
16
+ to_range.include?(other.to_date)
17
+ end
18
+
19
+ def self.for_date(date)
20
+ new(date.cwyear, date.cweek)
21
+ end
22
+
23
+ def self.current
24
+ for_date(Date.current)
25
+ end
26
+
27
+ def to_range
28
+ (self.begin...self.end)
29
+ end
30
+
31
+ def begin
32
+ Date.commercial(@year, @week)
33
+ end
34
+
35
+ def end
36
+ self.begin + 1.week
37
+ end
38
+
39
+ def succ
40
+ self.class.for_date(self.end)
41
+ end
42
+
43
+ def pred
44
+ self.class.for_date(self.begin.prev_day)
45
+ end
46
+
47
+ def lt(att)
48
+ att.gteq(self.end)
49
+ end
50
+
51
+ def lteq(att)
52
+ pred.lt(att)
53
+ end
54
+
55
+ def gt(att)
56
+ att.lt(self.begin)
57
+ end
58
+
59
+ def gteq(att)
60
+ succ.gt(att)
61
+ end
62
+ end
63
+
64
+ # TODO: move this to railtie
65
+ ActiveRecord::PredicateBuilder.register_handler(YearWeek, ->(attribute, value) { attribute.between(value.to_range) })
data/year_week.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "year_week"
7
+ spec.version = '0.2.0' # TODO: make this version accessible from class
8
+ spec.authors = ["Gert Goet"]
9
+ spec.email = ["gert@thinkcreate.nl"]
10
+
11
+ spec.summary = %q{Makes working with weeks more fun}
12
+ spec.description = %q{Makes working with weeks more fun}
13
+ spec.homepage = "https://gitlab.com/eval/year_week"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency "values", "~> 1.8.0"
30
+ spec.add_dependency "activerecord", "~> 4.2"
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.11"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ spec.add_development_dependency "sqlite3"
36
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: year_week
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Gert Goet
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: values
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.8.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.8.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Makes working with weeks more fun
98
+ email:
99
+ - gert@thinkcreate.nl
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/rake
113
+ - bin/rspec
114
+ - bin/setup
115
+ - lib/year_week.rb
116
+ - year_week.gemspec
117
+ homepage: https://gitlab.com/eval/year_week
118
+ licenses:
119
+ - MIT
120
+ metadata:
121
+ allowed_push_host: https://rubygems.org
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.5.1
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Makes working with weeks more fun
142
+ test_files: []