Class: Classifieds::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/classifieds/item.rb

Overview

describes the thing in a listing that is for sale

Direct Known Subclasses

Vehicle

Constant Summary collapse

COLUMN_SEPARATION =
5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, price, condition, detail_url) ⇒ Item

It is expected to be subclassed based on the type of item for sale (e.g., vehicle, clothing, furniture, etc.) To improve response time, an item’s detail_values are only loaded on demand (from the detail url)



5
6
7
8
9
10
11
# File 'lib/classifieds/item.rb', line 5

def initialize(title, price, condition, detail_url)
  @title = title
  @price = price
  @detail_url = detail_url
  @condition = condition
  @detail_values = {}
end

Class Method Details

.clearObject

Empty list of created objects



14
15
16
# File 'lib/classifieds/item.rb', line 14

def self.clear
  # all.clear  # not used, for now.
end

Instance Method Details

#details_to_string(addon_details) ⇒ Object

Return an item’s detail data formatted for display



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/classifieds/item.rb', line 21

def details_to_string(addon_details)
  Classifieds::Listing.scrape_listing_details(self.class, @detail_url, @condition, @detail_values) if @detail_values.empty?

  # Setup attribute/value details array
  detail_values_array = @detail_values.to_a
  addon_details.delete(:Phone) if detail_phone?  # do not addon phone if item details has a phone.
  detail_values_array.concat(addon_details.to_a)
  col2_offset = ((detail_values_array.size-1) / 2) + ((detail_values_array.size-1) % 2) # remove 1 from array size for description. It will get its own row.

  col_attr_width = []
  col_width = []

  # Calculate column 1 widths
  widths = max_col_widths(detail_values_array, 0, col2_offset)  # [max_attr_width, max_val_width, max_column_width]
  col_attr_width[0] = widths[0]
  col_width[0] = [widths[2], Classifieds::Item.col_width_limit(1)].min  # limit col width to max col width
  col_width[0] += COLUMN_SEPARATION

  # Calculate column 2 widths
  widths = max_col_widths(detail_values_array, col2_offset+1, detail_values_array.size)  # [max_attr_width, max_val_width, max_column_width]
  col_attr_width[1] = widths[0]
  col_width[1] = [widths[2], Classifieds::Item.col_width_limit(2)].min  # limit col width to max col width

  # Description value spans all columns.
  attribute = detail_values_array[0][0].to_s
  value = detail_values_array[0][1]
  result = "  #{Classifieds::Listing.format_detail(attribute, col_attr_width[0], value)}\n"

  # Then display remaining details in two column format.
  (1..col2_offset).each { |index|
    result << "  #{Classifieds::Item.format_pair(detail_values_array, index,             col_attr_width[0], col_width[0])}"
    next if (index+col2_offset) == detail_values_array.size  # when odd number of details.
    result << "  #{Classifieds::Item.format_pair(detail_values_array, index+col2_offset, col_attr_width[1], col_width[1])}\n"
  }
  result
end