Class: Bisac::POLineItem

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/bisac/po_line_item.rb

Overview

represents a single line on the purchase order. Has attributes like price, qty and description

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#authorObject

Returns the value of attribute author.



12
13
14
# File 'lib/bisac/po_line_item.rb', line 12

def author
  @author
end

#catalogue_codeObject

Returns the value of attribute catalogue_code.



11
12
13
# File 'lib/bisac/po_line_item.rb', line 11

def catalogue_code
  @catalogue_code
end

#isbnObject

Returns the value of attribute isbn.



13
14
15
# File 'lib/bisac/po_line_item.rb', line 13

def isbn
  @isbn
end

#line_item_numberObject

Returns the value of attribute line_item_number.



10
11
12
# File 'lib/bisac/po_line_item.rb', line 10

def line_item_number
  @line_item_number
end

#po_numberObject

Returns the value of attribute po_number.



10
11
12
# File 'lib/bisac/po_line_item.rb', line 10

def po_number
  @po_number
end

#priceObject

Returns the value of attribute price.



11
12
13
# File 'lib/bisac/po_line_item.rb', line 11

def price
  @price
end

#qtyObject

Returns the value of attribute qty.



11
12
13
# File 'lib/bisac/po_line_item.rb', line 11

def qty
  @qty
end

#sequence_numberObject

Returns the value of attribute sequence_number.



10
11
12
# File 'lib/bisac/po_line_item.rb', line 10

def sequence_number
  @sequence_number
end

#titleObject

Returns the value of attribute title.



12
13
14
# File 'lib/bisac/po_line_item.rb', line 12

def title
  @title
end

Class Method Details

.load_from_string(data) ⇒ Object

returns a new Bisac::POLineItem object using the data passed in as a string refer to the bisac spec for the expected format of the string

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bisac/po_line_item.rb', line 17

def self.load_from_string(data)
  raise ArgumentError, 'data must be a string' unless data.kind_of? String
  data.strip!

  item = self.new

  item.sequence_number = data[2,5].to_i
  item.po_number = data[7,13].strip
  item.line_item_number = data[21,10].strip

  # prefer the 13 digit ISBN if it exists (it's a non-standard, Pacstream extension)
  # fallback to the ISBN10
  item.isbn = data[80,13]
  item.isbn ||= data[31,10]
  item.isbn.strip!
  item.qty = data[41,5].to_i
  item.catalogue_code = data[46,1].strip
  item.price = BigDecimal.new(data[47,6])

  return item
end

Instance Method Details

#isbn10Object



54
55
56
57
58
59
60
# File 'lib/bisac/po_line_item.rb', line 54

def isbn10
  if isbn? && @isbn[0,3] == "978"
    ISBN10.complete(@isbn[3,9])
  else
    @isbn
  end
end

#isbn?Boolean

is the isbn for this product valid?

Returns:

  • (Boolean)


50
51
52
# File 'lib/bisac/po_line_item.rb', line 50

def isbn?
  EAN13.valid?(@isbn)
end

#to_sObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bisac/po_line_item.rb', line 62

def to_s
  lines = [""]
  lines[0] << "40"
  lines[0] << @sequence_number.to_s.rjust(5,"0")
  lines[0] << " "
  lines[0] << @po_number.to_s.ljust(11," ")
  lines[0] << " " # TODO
  lines[0] << "Y" # TODO
  lines[0] << @line_item_number.to_s.rjust(10,"0")
  lines[0] << pad_trunc(isbn10, 10)
  lines[0] << @qty.to_s.rjust(5, "0")
  lines[0] << "00000000000000000000000000000" # TODO
  lines[0] << "     "

  # if we're ordering a valid ISBN, append a non-standard
  # ISBN13 to the line
  if isbn?
    lines[0] << @isbn
  end

  if @title && @title.to_s.size > 0
    lines << ""
    lines[1] << "41"
    lines[1] << (@sequence_number + 1).to_s.rjust(5,"0")
    lines[1] << " "
    lines[1] << @po_number.to_s.ljust(11," ")
    lines[1] << "    "
    lines[1] << pad_trunc(@title, 30)
  end

  if @author && @author.to_s.size > 0
    lines << ""
    lines[2] << "42"
    lines[2] << (@sequence_number + 2).to_s.rjust(5,"0")
    lines[2] << " "
    lines[2] << @po_number.to_s.ljust(11," ")
    lines[2] << "                  " # TODO
    lines[2] << pad_trunc(@author, 30)
  end

  lines.join("\n")
end