minute 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +4 -0
  2. data/lib/minute.rb +108 -0
  3. metadata +46 -0
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ Minute
2
+ ======
3
+
4
+ Natural Language Date/Time parsing library for Ruby
data/lib/minute.rb ADDED
@@ -0,0 +1,108 @@
1
+ class Minute
2
+
3
+ VERSION = "0.1"
4
+
5
+ def self.add_unit(name, value)
6
+ class_eval("def self.#{name}; #{value} end", __FILE__, __LINE__ + 1)
7
+ end
8
+
9
+ def self.rule(options = {})
10
+ rules.merge! options
11
+ end
12
+
13
+ def self.rules
14
+ @rules ||= {}
15
+ end
16
+
17
+ def self.parse(text, options = {})
18
+ obj = new(options)
19
+ obj.parse_string(text)
20
+ end
21
+
22
+ def initialize(options = {})
23
+ @time = options[:now] || Time.now
24
+ @current_time = @time
25
+ end
26
+
27
+ def parse_string(text)
28
+ rules.keys.each do |key|
29
+ if key.is_a?(Regexp) && text =~ key
30
+ @current_time = rules[key].call(@time, Regexp.last_match)
31
+ elsif key.is_a?(String) && text.include?(key)
32
+ @current_time = rules[key].call(@time)
33
+ end
34
+ end
35
+ current_time
36
+ end
37
+
38
+ def rules
39
+ @rules ||= self.class.rules
40
+ end
41
+
42
+ def to_date(time = nil)
43
+ time ||= @current_time
44
+ Date.new(time.year, time.month, time.day)
45
+ end
46
+
47
+ def current_time
48
+ @current_time
49
+ end
50
+
51
+ add_unit 'year', 31536000
52
+ add_unit 'month', 3600 * 24 * 30
53
+ add_unit 'week', 3600 * 24 * 7
54
+ add_unit 'day', 3600 * 24
55
+ add_unit 'hour', 3600
56
+ add_unit 'minute', 60
57
+ add_unit 'second', 1
58
+
59
+ rule 'tomorrow' => lambda {|time| time + day }
60
+ rule 'yesterday' => lambda {|time| time - day }
61
+ rule 'today' => lambda {|time| time }
62
+
63
+ rule 'next year' => lambda {|time| time + year }
64
+ rule 'next month' => lambda {|time| time + month }
65
+ rule 'next week' => lambda {|time| time + week }
66
+ rule 'next day' => lambda {|time| time + day }
67
+ rule 'next hour' => lambda {|time| time + hour }
68
+ rule 'next minute' => lambda {|time| time + minute }
69
+ rule 'next second' => lambda {|time| time + second }
70
+
71
+ rule 'last year' => lambda {|time| time - year }
72
+ rule 'last month' => lambda {|time| time - month }
73
+ rule 'last week' => lambda {|time| time - week }
74
+ rule 'last day' => lambda {|time| time - day }
75
+ rule 'last hour' => lambda {|time| time - hour }
76
+ rule 'last minute' => lambda {|time| time - minute }
77
+ rule 'last second' => lambda {|time| time - second }
78
+
79
+ rule /(\d+) years?/ => lambda {|time, m| time + (m[1].to_i * year) }
80
+ rule /(\d+) years? ago/ => lambda {|time, m| time - (m[1].to_i * year) }
81
+ rule /(\d+) months?/ => lambda {|time, m| time + (m[1].to_i * month) }
82
+ rule /(\d+) months? ago/ => lambda {|time, m| time - (m[1].to_i * month) }
83
+ rule /(\d+) weeks?/ => lambda {|time, m| time + (m[1].to_i * week) }
84
+ rule /(\d+) weeks? ago/ => lambda {|time, m| time - (m[1].to_i * week) }
85
+ rule /(\d+) days?/ => lambda {|time, m| time + (m[1].to_i * day) }
86
+ rule /(\d+) days? ago/ => lambda {|time, m| time - (m[1].to_i * day) }
87
+ rule /(\d+) hours?/ => lambda {|time, m| time + (m[1].to_i * hour) }
88
+ rule /(\d+) hours? ago/ => lambda {|time, m| time - (m[1].to_i * hour) }
89
+ rule /(\d+) minutes?/ => lambda {|time, m| time + (m[1].to_i * minute) }
90
+ rule /(\d+) minutes? ago/ => lambda {|time, m| time - (m[1].to_i * minute) }
91
+ rule /(\d+) seconds?/ => lambda {|time, m| time + (m[1].to_i * second) }
92
+ rule /(\d+) seconds? ago/ => lambda {|time, m| time - (m[1].to_i * second) }
93
+
94
+ # TODO
95
+ # this december
96
+ # last december
97
+ # 13 dec 2010
98
+ # dec 13 2010
99
+ # next jan 13
100
+ # every wed
101
+ # this morning
102
+ # tonight
103
+ # between 8pm and 3pm
104
+ # 3pm to 5pm
105
+ # dec 13 to dec 14
106
+ #
107
+ end
108
+
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minute
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bryan Goines
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-23 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email: bryann83@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - lib/minute.rb
22
+ homepage: https://github.com/bry4n/minute
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.15
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Natural Language Date/Time parsing library for Ruby
46
+ test_files: []