Sha256: 4f6d582bcc9b44ee073c67c04979585683b4209312ad16270b68c40022eefa5b

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

class Array
  # 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
  def print_with(fields, system_less)
    extend Hirb::Console
    table self, :fields      => fields,
                :vertical    => true,
                :headers     => HEADERS,
                :system_less => system_less
  end

  # 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
  def sort_with!(field, direction)
    self          if 'usefulness' == field && 'desc' == direction
    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

  # Parses all 'date_created' fields to DateTime object
  def parse_date_time
    self.each do |review|
      review['date_created'] = DateTime.parse(review['date_created'])
    end
  end

  # Replace each review's rating into some symbols
  # Usage: array.replace_rating_with("★", "☆")
  def replace_rating_with(up, empty)
    self.each do |review|
      review['rating'] = up * review['rating'] + empty * (5 - review['rating'])
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
previews-0.0.3 lib/previews/array.rb
previews-0.0.2 lib/previews/array.rb