Skip to content

Commit

Permalink
Added low level access to various product measurements
Browse files Browse the repository at this point in the history
- weight, height, etc
  • Loading branch information
yob committed Oct 28, 2008
1 parent 132ad63 commit c36dcb3
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 0 deletions.
5 changes: 5 additions & 0 deletions data/product.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<Measurement>720 </Measurement>
<MeasureUnitCode>gr</MeasureUnitCode>
</Measure>
<Height>100</Height>
<Width>200.5</Width>
<Weight>300</Weight>
<Thickness>300</Thickness>
<Dimensions>100x200</Dimensions>
<SupplyDetail>
<SupplierName>Oxford University Press Australia and New Zealand</SupplierName>
<ProductAvailability>21</ProductAvailability>
Expand Down
2 changes: 2 additions & 0 deletions lib/onix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'andand'

# custom xml-mapping node types
require File.join(File.dirname(__FILE__), "onix", "decimal_type")
require File.join(File.dirname(__FILE__), "onix", "etext_type")
require File.join(File.dirname(__FILE__), "onix", "integer_type")
require File.join(File.dirname(__FILE__), "onix", "two_digit_type")
Expand All @@ -33,6 +34,7 @@
require File.join(File.dirname(__FILE__), "onix", "stock")
require File.join(File.dirname(__FILE__), "onix", "price")
require File.join(File.dirname(__FILE__), "onix", "supply_detail")
require File.join(File.dirname(__FILE__), "onix", "measure")
require File.join(File.dirname(__FILE__), "onix", "product")
require File.join(File.dirname(__FILE__), "onix", "reader")
require File.join(File.dirname(__FILE__), "onix", "writer")
Expand Down
49 changes: 49 additions & 0 deletions lib/onix/decimal_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'bigdecimal'

module ONIX

class DecimalType < ROXML::XMLRef # ::nodoc::
attr_reader :cdata, :content

def initialize(accessor, args, &block)
super(accessor, args, &block)
@content = args.content?
@cdata = args.cdata?
end

# Updates the text in the given _xml_ block to
# the _value_ provided.
def update_xml(xml, value)
parent = wrap(xml)
if value.kind_of?(BigDecimal)
value = value.to_s("F")
else
value = value.to_s
end
add(parent.child_add(LibXML::XML::Node.new_element(name)), value)
xml
end

def value(xml)
if content
value = BigDecimal.new(xml.content)
else
child = xml.search(name).first
value = BigDecimal.new(child.content) if child
end
block ? block.call(value) : value
end

private

def add(dest, value)
if cdata
dest.child_add(LibXML::XML::Node.new_cdata(value.to_utf))
else
dest.content = value.to_utf
end
end
end
end

ROXML::TypeRegistry.register(:decimal, ONIX::DecimalType)
9 changes: 9 additions & 0 deletions lib/onix/measure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module ONIX
class Measure
include ROXML

xml_accessor :measure_type_code, :twodigit, :from => "MeasureTypeCode"
xml_accessor :measurement, :decimal, :from => "Measurement"
xml_accessor :measure_unit_code, :etext, :from => "MeasureUnitCode"
end
end
10 changes: 10 additions & 0 deletions lib/onix/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ class Product
xml_accessor :publication_date, :yyyymmdd, :from => "PublicationDate"
xml_accessor :year_first_published, :integer, :from => "YearFirstPublished"
xml_accessor :sales_restrictions, [ONIX::SalesRestriction], :from => "SalesRestriction"
xml_accessor :measurements, [ONIX::Measure], :from => "Measure"
xml_accessor :supply_details, [ONIX::SupplyDetail], :from => "SupplyDetail"

# some deprecated attributes. Read only
# - See the measures array for the current way of specifying
# various measurements of the product
xml_reader :height, :decimal, :from => "Height"
xml_reader :width, :decimal, :from => "Width"
xml_reader :thickness, :decimal, :from => "Thickness"
xml_reader :weight, :decimal, :from => "Weight"
xml_reader :dimensions, :etext, :from => "Dimensions"

end
end
12 changes: 12 additions & 0 deletions spec/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
product.publishing_status.should eql(4)
product.publication_date.should eql(Date.civil(1998,9,1))
product.year_first_published.should eql(1998)

# including ye olde, deprecated ones
product.height.should eql(100)
product.width.should eql(BigDecimal.new("200.5"))
product.weight.should eql(300)
product.thickness.should eql(300)
product.dimensions.should eql("100x200")
end

specify "should provide read access to product IDs" do
Expand All @@ -43,6 +50,11 @@
product.subjects.size.should eql(1)
end

specify "should provide read access to measurements" do
product = ONIX::Product.parse(@product_node.to_s)
product.measurements.size.should eql(1)
end

specify "should provide write access to first level attibutes" do
product = ONIX::Product.new

Expand Down

0 comments on commit c36dcb3

Please sign in to comment.