Skip to content

Commit

Permalink
add support for measurements to APAProduct
Browse files Browse the repository at this point in the history
  • Loading branch information
yob committed Oct 28, 2008
1 parent c36dcb3 commit 994ed14
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
@@ -1,3 +1,6 @@
v0.4.1 (28th October 2008)
- Added accesses to various product measurements. Height, weight, etc.

v0.4.0 (28th October 2008)
- Major rework: now based on ROXML instead of xml-mapping
- Mostly API Compatible
Expand Down
127 changes: 127 additions & 0 deletions lib/onix/apa_product.rb
Expand Up @@ -11,6 +11,20 @@ class APAProduct < SimpleProduct
delegate :publishing_status, :publishing_status=
delegate :publication_date, :publication_date=

attr_reader :measurement_system

def initialize
@measurement_system = :metric
end

def measurement_system=(value)
if value == :metric || value == :imperial
@measurement_system = value
else
raise ArgumentError, "#{value} is not a recognised measurement system"
end
end

# retrieve the current EAN
def ean
identifier(3).andand.id_value
Expand Down Expand Up @@ -390,6 +404,98 @@ def rrp_inc_sales_tax=(num)
price_set(2, num)
end

# retrieve the height of the product
#
# If APAProduct#measurement_system is metric, these will be in mm, otherwise they
# will be in inches.
#
def height
# TODO: auto unit conversion
measurement(1).andand.measurement
end

# set the height of the book
#
# If APAProduct#measurement_system is metric, this should be in mm, otherwise it
# will be in inches.
#
def height=(value)
if measurement_system == :metric
measurement_set(1,value, "mm")
elsif measurement_system == :imperial
measurement_set(1,value, "in")
end
end

# retrieve the width of the product
#
# If APAProduct#measurement_system is metric, these will be in mm, otherwise they
# will be in inches.
#
def width
# TODO: auto unit conversion
measurement(2).andand.measurement
end

# set the width of the product
#
# If APAProduct#measurement_system is metric, this should be in mm, otherwise it
# will be in inches.
#
def width=(value)
if measurement_system == :metric
measurement_set(2,value, "mm")
elsif measurement_system == :imperial
measurement_set(2,value, "in")
end
end

# retrieve the weight of the product
#
# If APAProduct#measurement_system is metric, these will be in grams, otherwise they
# will be in ounces.
#
def weight
# TODO: auto unit conversion
measurement(8).andand.measurement
end

# set the weight of the product
#
# If APAProduct#measurement_system is metric, this should be in grams, otherwise it
# will be in ounces.
#
def weight=(value)
if measurement_system == :metric
measurement_set(8,value, "gr")
elsif measurement_system == :imperial
measurement_set(8,value, "oz")
end
end

# retrieve the thickness of the product
#
# If APAProduct#measurement_system is metric, these will be in mm, otherwise they
# will be in inches.
#
def thickness
# TODO: auto unit conversion
measurement(3).andand.measurement
end

# set the thickness of the product
#
# If APAProduct#measurement_system is metric, this should be in mm, otherwise it
# will be in inches.
#
def thickness=(value)
if measurement_system == :metric
measurement_set(3,value, "mm")
elsif measurement_system == :imperial
measurement_set(3,value, "in")
end
end

private

# add a new subject to this product
Expand Down Expand Up @@ -431,6 +537,27 @@ def identifier_set(type, value)
isbn_id.id_value = value
end

# retrieve the value of a particular measurement
def measurement(type)
product.measurements.find { |m| m.measure_type_code == type }
end

# set the value of a particular measurement
def measurement_set(type, value, unit)
measure = measurement(type)

# create a new isbn record if we need to
if measure.nil?
measure = ONIX::Measure.new
measure.measure_type_code = type
product.measurements << measure
end

# store the new value
measure.measurement = value
measure.measure_unit_code = unit.to_s
end

# retrieve the value of a particular media file
def media_file(type)
product.media_files.find { |m| m.media_file_type_code == type }
Expand Down

0 comments on commit 994ed14

Please sign in to comment.