chronik 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ Chronik
2
+ =======
3
+
4
+ Chronik is a very small library used to translate dates' classes, as seen on
5
+ public transport timetables, like "Weekdays" or "Easter and Christmas", to
6
+ actual dates (or weekdays if it directly relates to them). It is inspired by
7
+ [mojombo/chronic](https://github.com/mojombo/chronic).
8
+
9
+ Instalation
10
+ -----------
11
+
12
+ gem install chronik
13
+
14
+ Usage
15
+ -----
16
+
17
+ Most basic usage: translate day(s) of the week to integer (from 0 to 6)
18
+
19
+ require 'chronik'
20
+ label = Chronik::Label.new "Soboty" # Polish word for Saturday
21
+ label.weekdays #=> [6]
22
+
23
+ Slightly more complicated: get the dates of given holidays. Since holidays are
24
+ repeted every once in a year and we don't want an infinite array, we need to
25
+ specify a date range.
26
+
27
+ require 'chronik'
28
+ require 'date'
29
+ range = Date.new(2011,1,1)..Date.new(2011,12,31)
30
+ label = Chronik::Label.new "Wielkanoc" # Polish word for Easter
31
+ label.holidays(range) #=> [Date.new(2011,4,24), Date.new(2011,4,25)]
32
+
33
+ Limitations
34
+ -----------
35
+
36
+ Currently only Polish language is supported, but it is super easy to add
37
+ another language. Look for constants in
38
+ [lib/chronik/chronik.rb](http://github.com/stanley/chronik/tree/master/lib/chronik/chronik.rb)
39
+ file.
40
+
41
+ License
42
+ -------
43
+
44
+ Copyright (C) 2011 by Stanisław Wasiutyński
45
+
46
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
47
+ this software and associated documentation files (the "Software"), to deal in
48
+ the Software without restriction, including without limitation the rights to
49
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
50
+ of the Software, and to permit persons to whom the Software is furnished to do
51
+ so, subject to the following conditions:
52
+
53
+ The above copyright notice and this permission notice shall be included in all
54
+ copies or substantial portions of the Software.
55
+
56
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
57
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
58
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
59
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
60
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
61
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
62
+ SOFTWARE.
data/lib/chronik.rb ADDED
@@ -0,0 +1,8 @@
1
+ $:.unshift File.dirname(__FILE__) # For testing
2
+
3
+ # stdlib
4
+ require 'date'
5
+
6
+ # internals
7
+ require 'chronik/chronik'
8
+ require 'chronik/label'
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ module Chronik
5
+ class << self
6
+
7
+ private
8
+
9
+ # The Easter Sunday in the given year
10
+ #
11
+ # year - [integer]
12
+ #
13
+ # Returns Date
14
+ def easter(year)
15
+ c = year/100
16
+ n = year-19*(year/19)
17
+ k = (c-17)/25
18
+ i = c-c/4-(c-k)/3+19*n+15
19
+ i = i-30*(i/30)
20
+ i = i-(i/28)*(1-(i/28)*(29/(i+1))*((21-n)/11))
21
+ j = year+year/4+i+2-c+c/4
22
+ j = j-7*(j/7)
23
+ l = i-j
24
+ month = 3+(l+40)/44
25
+ day = l+28-31*(month/4)
26
+
27
+ Date.new year, month, day
28
+ end
29
+
30
+ # Defines dates relative to Easter.
31
+ #
32
+ # holidays - [Integer] or [Range]: offset in regard to Easter.
33
+ #
34
+ # Returns Proc.
35
+ def easter_dates(holidays=0)
36
+ holidays = [holidays] unless holidays.respond_to?(:map)
37
+ dates(lambda{|year| holidays.map{|offset| easter(year) + offset} })
38
+ end
39
+
40
+ # Defines one day of a year without specifying the year itself.
41
+ #
42
+ # month - [Integer]
43
+ # day - [Integer]
44
+ #
45
+ # Returns Proc.
46
+ def fixed_dates(month, day)
47
+ dates(lambda{|year| Date.new(year, month, day) })
48
+ end
49
+
50
+ # Defines an infinite number of days, which can be retrieved by calling
51
+ # a block, returned by this method, with time range as an argument.
52
+ #
53
+ # days - [Proc]: calling it with a year should return a Date instance.
54
+ #
55
+ # Returns Proc.
56
+ def dates(days)
57
+ lambda do |range|
58
+ (range.first.year..range.last.year).map do |year|
59
+ days.call(year)
60
+ end.flatten.grep(range)
61
+ end
62
+ end
63
+ end
64
+
65
+ SUGAR = [:dni, :i, :w]
66
+
67
+ ALL_HOLIDAYS = [:'święta', :wolne]
68
+
69
+ HOLIDAYS = {
70
+ [:wielkanoc, :wielkanocy] => easter_dates(0..1),
71
+ [:wielkanocny] => easter_dates(1),
72
+ [:sylwester, :sylwestra] => fixed_dates(12,31)
73
+ }
74
+
75
+ WEEKDAYS = {
76
+ [:*, :'tydzień'] => (0..6).to_a,
77
+ [:powszedni, :powszednie] => (1..5).to_a,
78
+ [:weekend, :weekendy] => [0,6],
79
+ [:sobota, :soboty] => [6],
80
+ [:niedziela, :niedziele] => [0]
81
+ }
82
+
83
+ end
@@ -0,0 +1,45 @@
1
+ require 'unicode'
2
+
3
+ module Chronik
4
+ # The class of days.
5
+ class Label
6
+ def initialize(text)
7
+ @tokens = text.split.map{|word| Unicode.downcase(word).to_sym} - SUGAR
8
+ end
9
+
10
+ # Special days which are represented by this label. Those are probably one
11
+ # in a year holidays like Christmas.
12
+ #
13
+ # range - [Range]:
14
+ #
15
+ # Returns an Array of Dates.
16
+ def holidays(range=(Date.today..Date.today.next_month))
17
+ output = []
18
+ @tokens.each do |token|
19
+ if ALL_HOLIDAYS.include?(token)
20
+ return HOLIDAYS.values.map do |dates|
21
+ dates.call(range)
22
+ end.flatten.uniq
23
+ end
24
+ HOLIDAYS.each_pair do |labels,dates|
25
+ if labels.include?(token)
26
+ output.concat(dates.call(range))
27
+ end
28
+ end
29
+ end
30
+ output
31
+ end
32
+
33
+ # Day(s) of a week (Sunday is 0). Also range of days, like weekend.
34
+ #
35
+ # Returns an Array of Integers.
36
+ def weekdays
37
+ @tokens.each do |token|
38
+ WEEKDAYS.each_pair do |labels,wdays|
39
+ return wdays if labels.include?(token)
40
+ end
41
+ end
42
+ []
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chronik
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stanisław Wasiutyński
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-07-30 00:00:00.000000000 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: unicode
17
+ requirement: &7841380 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - =
21
+ - !ruby/object:Gem::Version
22
+ version: 0.4.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *7841380
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &7840980 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *7840980
37
+ - !ruby/object:Gem::Dependency
38
+ name: cucumber
39
+ requirement: &7840520 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *7840520
48
+ description:
49
+ email: staszek.wasiutynski@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - lib/chronik/chronik.rb
55
+ - lib/chronik/label.rb
56
+ - lib/chronik.rb
57
+ - README.md
58
+ has_rdoc: true
59
+ homepage:
60
+ licenses: []
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 1.6.2
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Dates' class parser
83
+ test_files: []