Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

basic specs for Subject class

  • Loading branch information...
commit e93cbe0f022b2c411557960f09675867c11f65de 1 parent ed88301
@yob authored
Showing with 49 additions and 1 deletion.
  1. +6 −0 data/subject.xml
  2. +3 −1 lib/onix/subject.rb
  3. +40 −0 spec/subject_spec.rb
View
6 data/subject.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Subject>
+ <SubjectSchemeIdentifier>03</SubjectSchemeIdentifier>
+ <SubjectSchemeName>RBA Subjects</SubjectSchemeName>
+ <SubjectCode>AABB</SubjectCode>
+</Subject>
View
4 lib/onix/subject.rb
@@ -2,7 +2,9 @@ module ONIX
class Subject
include ROXML
- xml_accessor :subject_scheme_id, :from => "SubjectSchemeIdentifier", :as => Fixnum # should be a 2 digit num
+ xml_name "Subject"
+
+ xml_accessor :subject_scheme_id, :from => "SubjectSchemeIdentifier", :as => Fixnum, :to_xml => ONIX::Formatters.two_digit
xml_accessor :subject_scheme_name, :from => "SubjectSchemeName"
xml_accessor :subject_scheme_version, :from => "SubjectSchemeVersion"
xml_accessor :subject_code, :from => "SubjectCode"
View
40 spec/subject_spec.rb
@@ -0,0 +1,40 @@
+# coding: utf-8
+
+$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
+
+require 'onix'
+
+context "ONIX::Subject" do
+
+ before(:each) do
+ data_path = File.join(File.dirname(__FILE__),"..","data")
+ file1 = File.join(data_path, "subject.xml")
+ @doc = LibXML::XML::Document.file(file1)
+ @root = @doc.root
+ end
+
+ specify "should correctly convert to a string" do
+ sub = ONIX::Subject.from_xml(@root.to_s)
+ sub.to_xml.to_s[0,9].should eql("<Subject>")
+ end
+
+ specify "should provide read access to first level attibutes" do
+ sub = ONIX::Subject.from_xml(@root.to_s)
+ sub.subject_scheme_id.should eql(3)
+ sub.subject_scheme_name.should eql("RBA Subjects")
+ sub.subject_code.should eql("AABB")
+ end
+
+ specify "should provide write access to first level attibutes" do
+ sub = ONIX::Subject.new
+
+ sub.subject_scheme_id = 2
+ sub.to_xml.to_s.include?("<SubjectSchemeIdentifier>02</SubjectSchemeIdentifier>").should be_true
+
+ sub.subject_code = "ABCD"
+ sub.to_xml.to_s.include?("<SubjectCode>ABCD</SubjectCode>").should be_true
+
+ end
+
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.