Class: NameSort::Sorter
- Inherits:
-
Object
- Object
- NameSort::Sorter
- Defined in:
- lib/name_sort.rb
Constant Summary collapse
- HDR =
%w(last_name first_name gender date_of_birth favorite_color)
Instance Attribute Summary collapse
-
#names ⇒ Object
readonly
Returns the value of attribute names.
-
#parsed ⇒ Object
readonly
Returns the value of attribute parsed.
Instance Method Summary collapse
- #format_date(value) ⇒ Object
- #format_fields(record) ⇒ Object
- #format_for(field, value) ⇒ Object
- #format_gender(value) ⇒ Object
- #header(delimiter) ⇒ Object
-
#initialize ⇒ Sorter
constructor
A new instance of Sorter.
- #input(delimiter, file) ⇒ Object
- #print_names(sort_by) ⇒ Object
- #sort_for(field) ⇒ Object
Constructor Details
#initialize ⇒ Sorter
Returns a new instance of Sorter.
9 10 11 |
# File 'lib/name_sort.rb', line 9 def initialize @names = [] end |
Instance Attribute Details
#names ⇒ Object (readonly)
Returns the value of attribute names.
8 9 10 |
# File 'lib/name_sort.rb', line 8 def names @names end |
#parsed ⇒ Object (readonly)
Returns the value of attribute parsed.
8 9 10 |
# File 'lib/name_sort.rb', line 8 def parsed @parsed end |
Instance Method Details
#format_date(value) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/name_sort.rb', line 43 def format_date(value) if value.count('-') == 2 Date.strptime(value, "%m-%d-%Y") elsif value.count('/') == 2 Date.strptime(value, "%m/%d/%Y") end end |
#format_fields(record) ⇒ Object
73 74 75 76 77 |
# File 'lib/name_sort.rb', line 73 def format_fields(record) record.map.with_index do |field,idx| HDR.index("date_of_birth") == idx ? field.strftime('%-m/%d/%Y') : field end end |
#format_for(field, value) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/name_sort.rb', line 24 def format_for(field,value) case field.strip() when 'gender' format_gender(value.strip()) when 'date_of_birth' format_date(value.strip()) else value.strip() end end |
#format_gender(value) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/name_sort.rb', line 35 def format_gender(value) if value.match(/^(m|male)$/i) "Male" elsif value.match(/^(f|female)$/i) "Female" end end |
#header(delimiter) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/name_sort.rb', line 51 def header(delimiter) unless [' ','|',','].include?(delimiter) raise InvalidDeliminatorError end hdr = %w(last_name first_name) case delimiter when '|' hdr += %w(middle_initial gender favorite_color date_of_birth) when ' ' hdr += %w(middle_initial gender date_of_birth favorite_color) when ',' hdr += %w(gender favorite_color date_of_birth) else hdr end end |
#input(delimiter, file) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/name_sort.rb', line 13 def input(delimiter,file) header_fields = header(delimiter) parsed = CSV.read(file, col_sep: delimiter, headers: header_fields) parsed.delete('middle_initial') data = [] parsed.each do |line| data << HDR.map{|field| line[field].nil? ? nil : format_for(field,line[field]) }.compact end @names += data end |
#print_names(sort_by) ⇒ Object
79 80 81 82 83 |
# File 'lib/name_sort.rb', line 79 def print_names(sort_by) sort_for(sort_by).each do |record| puts format_fields(record).join(" ") end end |
#sort_for(field) ⇒ Object
68 69 70 71 |
# File 'lib/name_sort.rb', line 68 def sort_for(field) field_index = HDR.index(field) @names.sort_by{|n|n[field_index]} end |