Module: ThirdBase::Date::ClassMethods
- Included in:
- ThirdBase::Date
- Defined in:
- lib/third_base/date.rb
Instance Method Summary collapse
-
#add_parser(type, pattern, &block) ⇒ Object
Add a parser to the parser type.
-
#add_parser_type(type) ⇒ Object
Add a parser type to the list of parser types.
-
#civil(year, mon, day) ⇒ Object
Returns a new Date with the given year, month, and day.
-
#commercial(cwyear, cweek, cwday = 5) ⇒ Object
Returns a new Date with the given commercial week year, commercial week, and commercial week day.
-
#jd(j) ⇒ Object
Returns a new Date with the given julian date.
-
#new(*args) ⇒ Object
Calls civil with the given arguments.
-
#ordinal(year, yday) ⇒ Object
Returns a new Date with the given year and day of year.
-
#parse(str, opts = {}) ⇒ Object
Parses the given string and returns a Date.
-
#reset_parsers! ⇒ Object
Reset the parsers, parser types, and order of parsers used to the default.
-
#strptime(str, fmt = strptime_default) ⇒ Object
Parse the string using the provided format (or the default format).
-
#today ⇒ Object
Returns a date with the current year, month, and date.
-
#use_parsers(*parsers) ⇒ Object
Set the order of parser types to use to the given parser types.
Instance Method Details
#add_parser(type, pattern, &block) ⇒ Object
Add a parser to the parser type. Arguments:
-
type - The parser type to which to add the parser, should be a Symbol.
-
pattern - Can be either a Regexp or String:
-
String - A strptime parser regular expression is created using pattern as the format string. If a block is given, it is used. If no block is given, the parser will operate identically to strptime.
-
Regexp - The regular expression is used directly. In this case, a block must be provided, or an error is raised.
-
The block, if provided, should take a single MatchData argument. It should return nil if it cannot successfully parse the string, an instance of this class, or a hash of values to be passed to new!.
106 107 108 109 110 111 112 113 114 |
# File 'lib/third_base/date.rb', line 106 def add_parser(type, pattern, &block) if pattern.is_a?(String) pattern, blk = strptime_pattern_and_block(pattern) block ||= blk else raise(ArgumentError, 'must provide block for Regexp parser') unless block_given? end parser_hash[type].unshift([pattern, block]) end |
#add_parser_type(type) ⇒ Object
Add a parser type to the list of parser types. Should be used if you want to add your own parser types.
119 120 121 |
# File 'lib/third_base/date.rb', line 119 def add_parser_type(type) parser_hash[type] ||= [] end |
#civil(year, mon, day) ⇒ Object
Returns a new Date with the given year, month, and day.
124 125 126 |
# File 'lib/third_base/date.rb', line 124 def civil(year, mon, day) new!(:civil=>[year, mon, day]) end |
#commercial(cwyear, cweek, cwday = 5) ⇒ Object
Returns a new Date with the given commercial week year, commercial week, and commercial week day.
130 131 132 |
# File 'lib/third_base/date.rb', line 130 def commercial(cwyear, cweek, cwday=5) new!(:commercial=>[cwyear, cweek, cwday]) end |
#jd(j) ⇒ Object
Returns a new Date with the given julian date.
135 136 137 |
# File 'lib/third_base/date.rb', line 135 def jd(j) new!(:jd=>j) end |
#new(*args) ⇒ Object
Calls civil with the given arguments.
140 141 142 |
# File 'lib/third_base/date.rb', line 140 def new(*args) civil(*args) end |
#ordinal(year, yday) ⇒ Object
Returns a new Date with the given year and day of year.
145 146 147 |
# File 'lib/third_base/date.rb', line 145 def ordinal(year, yday) new!(:ordinal=>[year, yday]) end |
#parse(str, opts = {}) ⇒ Object
Parses the given string and returns a Date. Raises an ArgumentError if no parser can correctly parse the date. Takes the following options:
-
:parser_types : an array of parser types to use, overriding the default or the ones specified by use_parsers.
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/third_base/date.rb', line 155 def parse(str, opts={}) s = str.strip parsers(opts[:parser_types]) do |pattern, block| if m = pattern.match(s) if res = block.call(m) return res.is_a?(Hash) ? new!(res) : res end end end raise ArgumentError, 'invalid date' end |
#reset_parsers! ⇒ Object
Reset the parsers, parser types, and order of parsers used to the default.
168 169 170 171 172 173 174 175 176 177 |
# File 'lib/third_base/date.rb', line 168 def reset_parsers! parser_hash.clear default_parser_hash.each do |type, parsers| add_parser_type(type) parsers.reverse.each do |re, parser| add_parser(type, re, &parser) end end use_parsers(*default_parser_list) end |
#strptime(str, fmt = strptime_default) ⇒ Object
Parse the string using the provided format (or the default format). Raises an ArgumentError if the format does not match the string.
181 182 183 184 185 186 187 188 189 |
# File 'lib/third_base/date.rb', line 181 def strptime(str, fmt=strptime_default) pattern, block = strptime_pattern_and_block(fmt) s = str.strip if m = pattern.match(s) block.call(m) else raise ArgumentError, 'invalid date' end end |
#today ⇒ Object
Returns a date with the current year, month, and date.
192 193 194 195 |
# File 'lib/third_base/date.rb', line 192 def today t = Time.now civil(t.year, t.mon, t.day) end |
#use_parsers(*parsers) ⇒ Object
Set the order of parser types to use to the given parser types.
198 199 200 |
# File 'lib/third_base/date.rb', line 198 def use_parsers(*parsers) parser_list.replace(parsers) end |