From 994ed1432a7e8a3423f536257b2e4db832792be9 Mon Sep 17 00:00:00 2001 From: James Healy Date: Tue, 28 Oct 2008 18:33:53 +1100 Subject: [PATCH] add support for measurements to APAProduct --- CHANGELOG | 3 + lib/onix/apa_product.rb | 127 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 5b3d7c0..a43c372 100644 --- a/CHANGELOG +++ b/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 diff --git a/lib/onix/apa_product.rb b/lib/onix/apa_product.rb index 8f2bfa6..bf7def3 100644 --- a/lib/onix/apa_product.rb +++ b/lib/onix/apa_product.rb @@ -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 @@ -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 @@ -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 }