Class: Minute
- Inherits:
-
Object
- Object
- Minute
- Defined in:
- lib/minute.rb
Overview
Minute.parse(“I came back from May 13, 2025”, now: (Time.now + 23.years))
Constant Summary collapse
- VERSION =
"0.1"
- MONTHS =
Date::MONTHNAMES.zip(Date::ABBR_MONTHNAMES).flatten.compact.map(&:downcase).freeze
- DATE_WITH_ORDINAL =
/(\d{1,2})(st|nd|rd|th)?\s+(#{MONTHS.join("|")})(\s+\d{4})?/
- ENGLISH_DATE =
/(#{MONTHS.join("|")}),?\s(\d{1,2})(\s+\d{4})?/
- DATE =
/(\d{1,4})-?(\d{1,4})-?(\d{1,4})?/
Instance Attribute Summary collapse
-
#current_time ⇒ Object
readonly
Returns the value of attribute current_time.
-
#now ⇒ Object
readonly
Returns the value of attribute now.
Class Method Summary collapse
-
.match(pattern, block = nil, &blk) ⇒ Object
Public: Create a match pattern to capture date/time strings.
-
.parse(text, options = {}) ⇒ Object
Public: parse the String to capture Date/Time.
- .patterns ⇒ Object
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Minute
constructor
A new instance of Minute.
- #parse_string(text) ⇒ Object
- #patterns ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Minute
Returns a new instance of Minute.
55 56 57 58 |
# File 'lib/minute.rb', line 55 def initialize( = {}) @now = [:now] || Time.now @current_time = @now end |
Instance Attribute Details
#current_time ⇒ Object (readonly)
Returns the value of attribute current_time.
53 54 55 |
# File 'lib/minute.rb', line 53 def current_time @current_time end |
#now ⇒ Object (readonly)
Returns the value of attribute now.
53 54 55 |
# File 'lib/minute.rb', line 53 def now @now end |
Class Method Details
.match(pattern, block = nil, &blk) ⇒ Object
Public: Create a match pattern to capture date/time strings.
Examples:
match "tomorrow", lambda {|time| time + 1.day}
match "yesterday" do |time|
time - 1.day
end
Returns Hash of match patterns.
33 34 35 |
# File 'lib/minute.rb', line 33 def self.match(pattern, block = nil, &blk) patterns.merge!({pattern => (block ? block : blk)}) end |
.parse(text, options = {}) ⇒ Object
Public: parse the String to capture Date/Time
Examples:
Minute.parse("tomorrow")
Returns an instance of Time
44 45 46 47 |
# File 'lib/minute.rb', line 44 def self.parse(text, = {}) obj = new() obj.parse_string(text.to_s.downcase.strip) end |
.patterns ⇒ Object
49 50 51 |
# File 'lib/minute.rb', line 49 def self.patterns @patterns ||= {} end |
Instance Method Details
#parse_string(text) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/minute.rb', line 60 def parse_string(text) patterns.keys.each do |pattern| begin if pattern.is_a?(Regexp) && text =~ pattern @now = patterns[pattern].call(current_time, Regexp.last_match) elsif pattern.is_a?(String) && text.include?(pattern) @now = patterns[pattern].call(current_time) end rescue @now end end @now end |
#patterns ⇒ Object
75 76 77 |
# File 'lib/minute.rb', line 75 def patterns @patterns ||= self.class.patterns end |