Module: DS::ClassMethods
- Included in:
- DS
- Defined in:
- lib/ds.rb
Constant Summary collapse
- @@centuries =
nil- @@logger =
nil- @@loggers =
{}
Instance Method Summary collapse
-
#adjust_for_century(range) ⇒ String
Adjust date ranges so that intended results are returned for century values.
-
#calculate_century(year) ⇒ Integer
Given a year, return to the corresponding century as an integer following this pattern:.
- #configure_logger_for(classname) ⇒ Object
- #logger ⇒ Object
- #logger_for(classname) ⇒ Object
-
#lookup_century(century) ⇒ String
Look up the URI for
century, where century is an integer like1,12,-3, etc. - #mark_long(s) ⇒ Object
- #timestamp ⇒ Object
-
#transform_centuries_to_aat(centuries_string, rec_sep: '|', sub_sep: ';') ⇒ String
Take a formatted string of century integers and return the string AAT century URIs, retaining the divisions.
-
#transform_dates_to_centuries(dates) ⇒ String
Given a pipe separated list of single years or ranges of years, return a pipe- and semicolon-separated list of century integers.
Instance Method Details
#adjust_for_century(range) ⇒ String
Adjust date ranges so that intended results are returned for century values. Thus:
1400 to 1499 => 15th C. CE
1401 to 1500 => 15th C. CE
And, thus:
-1499 to -1400 => 15th C. BCE
-1500 to -1401 => 15th C. BCE
This method adjusts the end year for CE dates and the start year for BCE dates as needed:
DS.adjust_for_century '1325' # => '1325'; no change needed
DS.adjust_for_century '1400^1499' # => '1400^1499'; no change needed
DS.adjust_for_century '1401^1500' # => '1401^1499'
DS.adjust_for_century '-1500^-1401' # => '-1500^-1401'; no change needed
DS.adjust_for_century '-1499^-1400' # => '-1499^-1401'
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/ds.rb', line 136 def adjust_for_century range # return a single date return range if range =~ %r{^-?\d+$} start_year, end_year = range.split('^') start_int, end_int = start_year.to_i, end_year.to_i # end dates divisible by 100 need to be reduced by one: # 1500 => 1499; -1500 => -1501 end_int -= 1 if end_int % 100 == 0 [start_int, end_int].uniq.join '^' end |
#calculate_century(year) ⇒ Integer
Given a year, return to the corresponding century as an integer following this pattern:
-
the 16th C. CE is years 1500 to 1599
-
the 1st C. CE is years 0 to 99
-
the 16th C. BCE is years -1599 to -1501
Thus:
DS.calculate_century '1501' # => 16
DS.calculate_century '1600' # => 17
DS.calculate_century '-1600' # => -16
DS.calculate_century '-1501' # => -16
DS.calculate_century '1' # => 1
DS.calculate_century '0' # => 1
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/ds.rb', line 168 def calculate_century year # remove leading 0s; 4-digit numbers starting with 0 are octal year_int = Integer year # year <=> 0 returns 1 if year > 0; -1 if year < 0; 0 if year == 0 # 0 is a special year; its sign is 1 and its absolute value is 1 sign = year_int == 0 ? 1 : (year_int <=> 0) abs_val = year_int == 0 ? 1 : year_int.abs offset = sign < 0 ? 1 : 0 # if year is 1501, sign == 1 and abs_val == 1501 # => 1 * ((1501 - 1)/100 +1) => 1 * (1500/100 + 1) => 1 * (15 + 1) = 16 # if year is 1500, sign == 1 and abs_val == 1500 # => 1 * ((1500 - 1)/100 +1) => 1 * (1499/100 + 1) => 1 * (14 + 1) = 15 sign * ((abs_val - offset) / 100 + 1) end |
#configure_logger_for(classname) ⇒ Object
228 229 230 231 232 233 |
# File 'lib/ds.rb', line 228 def configure_logger_for(classname) logger = Logger.new(STDOUT) logger.progname = classname logger.level = Settings.ds.log_level || :warn logger end |
#logger ⇒ Object
218 219 220 221 |
# File 'lib/ds.rb', line 218 def logger return @@logger if @@logger @@logger = DS.logger_for self.class.name end |
#logger_for(classname) ⇒ Object
224 225 226 |
# File 'lib/ds.rb', line 224 def logger_for(classname) @@loggers[classname] ||= configure_logger_for(classname) end |
#lookup_century(century) ⇒ String
Look up the URI for century, where century is an integer like 1, 12, -3, etc. Values are read in from the file data/getty-aat-centuries.csv and converted to a hash of Getty AAT century URIs. Keys are century integers, like ‘1’, ‘2’, ‘3’, ‘-1’, ‘-2’, ‘-3’, etc. and values are AAT URIs.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/ds.rb', line 198 def lookup_century century if @@centuries.nil? path = File. '../ds/data/getty-aat-centuries.csv', __FILE__ # aat_id,label,number # http://vocab.getty.edu/aat/300404465,fifteenth century (dates CE),15 # http://vocab.getty.edu/aat/300404493,first century (dates CE),1 @@centuries = CSV.read(path).inject({}) do |h, row| if row.first == 'aat_id' h else h.update({ row.last => row.first }) end end.freeze end @@centuries[century.to_s] end |
#mark_long(s) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/ds.rb', line 53 def mark_long s return s if s.to_s.size <= DS::MAX_WIKIBASE_FIELD_LENGTH splits = Recon::Type::Splits._lookup_single s, from_column: 'authorized_label' return "SPLIT: #{s}" if splits.blank? splits end |
#timestamp ⇒ Object
184 185 186 |
# File 'lib/ds.rb', line 184 def DateTime.now.iso8601.to_s end |
#transform_centuries_to_aat(centuries_string, rec_sep: '|', sub_sep: ';') ⇒ String
Take a formatted string of century integers and return the string AAT century URIs, retaining the divisions.
103 104 105 106 107 108 109 110 111 |
# File 'lib/ds.rb', line 103 def transform_centuries_to_aat centuries_string, rec_sep: '|', sub_sep: ';' return if centuries_string.to_s.strip.empty? centuries_string.split(rec_sep).map { |century_range| century_range.split(sub_sep).map { |century_int| lookup_century century_int }.join sub_sep }.join rec_sep end |
#transform_dates_to_centuries(dates) ⇒ String
Given a pipe separated list of single years or ranges of years, return a pipe- and semicolon-separated list of century integers. Year ranges are separated by the ^ character, so that - can unambiguously be used for BCE years as negative integers (1099-1000 BCE => -1099^-1000)
For example,
DS.transform_dates_to_centuries('1400') # => '15'
DS.transform_dates_to_centuries('1400^1499') # => '15'
DS.transform_dates_to_centuries('1325|1400^1499') # => '14|15'
DS.transform_dates_to_centuries('890^1020') # => '9;10;11'
DS.transform_dates_to_centuries('-800^-701') # => '-8'
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ds.rb', line 79 def transform_dates_to_centuries dates return if dates.blank? dates.to_s.split('|').flat_map { |date_range| next [] if date_range.strip.empty? # don't process empty values # Adjust ranges to return sensible centuries for ranges like # '1400-1499' or '1401-1500' date_range = adjust_for_century date_range # turn the date/date range into a [min,max] array of century integers: # 1350-1550 => [14,16] # 1350 => [14] centuries = date_range.split('^').map { |i| calculate_century i }.sort # get an array for the range of centuries: # [14,16] => 14, 15, 16 # join them; throw away zero if range spans BCE/CE (centuries.first..centuries.last).to_a.reject(&:zero?).join ';' # join list of centuries by semicolons }.join '|' end |