Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/previews/array.rb
Instance Method Summary collapse
-
#parse_date_time ⇒ Object
Parses all ‘date_created’ fields to DateTime object.
-
#print_with(fields, system_less) ⇒ Object
Using Hirb to print reviews.
-
#replace_rating_with(up, empty) ⇒ Object
Replace each review’s rating into some symbols Usage: array.replace_rating_with(“★”, “☆”).
-
#sort_with!(field, direction) ⇒ Object
By default reviews sorted by usefulness.
Instance Method Details
#parse_date_time ⇒ Object
Parses all ‘date_created’ fields to DateTime object
28 29 30 31 32 |
# File 'lib/previews/array.rb', line 28 def parse_date_time self.each do |review| review['date_created'] = DateTime.parse(review['date_created']) end end |
#print_with(fields, system_less) ⇒ Object
Using Hirb to print reviews. :fields is fields to display print out :vertical table HEADERS is Hash of headers passing system_less as a option
7 8 9 10 11 12 13 |
# File 'lib/previews/array.rb', line 7 def print_with(fields, system_less) extend Hirb::Console table self, :fields => fields, :vertical => true, :headers => HEADERS, :system_less => system_less end |
#replace_rating_with(up, empty) ⇒ Object
Replace each review’s rating into some symbols Usage: array.replace_rating_with(“★”, “☆”)
36 37 38 39 40 |
# File 'lib/previews/array.rb', line 36 def (up, empty) self.each do |review| review['rating'] = up * review['rating'] + empty * (5 - review['rating']) end end |
#sort_with!(field, direction) ⇒ Object
By default reviews sorted by usefulness. We can reverse it to sort by ascending For others fields we sorting by comparing each element of Array
17 18 19 20 21 22 23 24 25 |
# File 'lib/previews/array.rb', line 17 def sort_with!(field, direction) return self if 'usefulness' == field && 'desc' == direction return self.reverse! if 'usefulness' == field && 'asc' == direction if 'asc' == direction self.sort! { |review1, review2| review1[field] <=> review2[field]} elsif 'desc' == direction self.sort! { |review1, review2| review2[field] <=> review1[field]} end end |