Class: String
Overview
Cast a string to an id.
Instance Method Summary collapse
-
#to_date_id ⇒ Object
Cast me to a date id.
-
#to_date_ids ⇒ Object
Cast me to a list of date ids.
-
#to_i_id ⇒ Object
Cast me to an integer id.
-
#to_i_ids ⇒ Object
Cast me (a comma-separated list) to a list of integer ids.
-
#to_s_id ⇒ Object
Cast me to a string id.
-
#to_s_ids ⇒ Object
Cast me to a list of string ids.
-
#to_s_uuid ⇒ Object
Cast me to a string uuid.
-
#to_s_uuids ⇒ Object
Cast me to a list of string ids.
-
#to_stint_id ⇒ Object
Cast me to a stint id.
-
#to_stint_ids ⇒ Object
Cast me to a list of stint ids.
Instance Method Details
#to_date_id ⇒ Object
Cast me to a date id.
" 2000-12-31 ".to_date_id
#=> "2000-12-31"
12 13 14 15 |
# File 'lib/sixarm_ruby_to_id/string.rb', line 12 def to_date_id s = self.strip s =~ /\A\d\d\d\d-\d\d-\d\d\z/ ? s : nil end |
#to_date_ids ⇒ Object
Cast me to a list of date ids. The string is comma-separated.
"2000-12-31,2001-12-31,2002-12-31".to_date_id
#=> ["2000-12-31", "2001-12-31", "2002-12-31"]
23 24 25 |
# File 'lib/sixarm_ruby_to_id/string.rb', line 23 def to_date_ids split(/,/).map{|x| x.to_date_id} end |
#to_i_id ⇒ Object
Cast me to an integer id.
" 1 ".to_i_id
#=> 1
65 66 67 |
# File 'lib/sixarm_ruby_to_id/string.rb', line 65 def to_i_id strip.to_i end |
#to_i_ids ⇒ Object
Cast me (a comma-separated list) to a list of integer ids. The string is comma-separated.
"1,2,3".to_s_ids
#=> [1, 2, 3]
75 76 77 |
# File 'lib/sixarm_ruby_to_id/string.rb', line 75 def to_i_ids split(/,/).map{|x| x.to_i_id} end |
#to_s_id ⇒ Object
Cast me to a string id.
" a "
#=> "a"
84 85 86 |
# File 'lib/sixarm_ruby_to_id/string.rb', line 84 def to_s_id strip end |
#to_s_ids ⇒ Object
Cast me to a list of string ids. The string is comma-separated.
"a,b,c".to_s_ids
#=> ["a", "b", "c"]
94 95 96 |
# File 'lib/sixarm_ruby_to_id/string.rb', line 94 def to_s_ids split(/,/).to_s_ids end |
#to_s_uuid ⇒ Object
Cast me to a string uuid.
" 00000000-0000-0000-0000-000000000000 ".to_s_uuids
#=> "00000000-0000-0000-0000-000000000000"
103 104 105 |
# File 'lib/sixarm_ruby_to_id/string.rb', line 103 def to_s_uuid strip.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/) ? $& : nil end |
#to_s_uuids ⇒ Object
Cast me to a list of string ids. The string is comma-separated.
"00000000-0000-0000-0000-000000000000,11111111-1111-1111-1111-111111111111".to_s_uuids
#=> [
"00000000-0000-0000-0000-000000000000"
"11111111-1111-1111-1111-111111111111"
]
116 117 118 |
# File 'lib/sixarm_ruby_to_id/string.rb', line 116 def to_s_uuids split(/,/).to_s_uuids end |
#to_stint_id ⇒ Object
Cast me to a stint id.
The string is two ISO dates in YYYY-MM-DD.
The separator is either:
* two dots ".." to include the stop date
* three dots "..." to exclude the stop date.
Examples:
" 2000-12-30..2000-12-31".to_stint_id
#=> "2000-12-30-2000-12-31"
" 2000-12-30...2000-12-31".to_stint_id
#=> "2000-12-30-2000-12-31"
45 46 47 48 |
# File 'lib/sixarm_ruby_to_id/string.rb', line 45 def to_stint_id s = self.strip s =~ /\A\d\d\d\d-\d\d-\d\d\.\.\.?\d\d\d\d-\d\d-\d\d\z/ ? s : nil end |
#to_stint_ids ⇒ Object
Cast me to a list of stint ids. The string is comma-separated.
"2000-12-30-2000-12-31,2001-12-30-2001-12-31,2002-12-30-2002-12-31".to_date_id
#=> ["2000-12-30-2000-12-31"], ["2001-12-30-2001-12-31"], ["2002-12-30-2002-12-31"]]
56 57 58 |
# File 'lib/sixarm_ruby_to_id/string.rb', line 56 def to_stint_ids split(/,/).map{|x| x.to_stint_id} end |