Skip to content

Commit

Permalink
Added os method to Template model
Browse files Browse the repository at this point in the history
  • Loading branch information
jprovaznik committed Dec 3, 2012
1 parent 891ee6b commit 1845bf4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
19 changes: 17 additions & 2 deletions app/models/tim/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,25 @@ class Template < Tim::Base
attr_accessible :xml
attr_protected :id

OS = Struct.new(:name, :version, :arch)

# Used in views to display the xml elements of this template
def xml_elements
::Nokogiri::XML::Document.parse(xml).xpath("//template/*").to_xml
parsed_xml.xpath("//template/*").to_xml
end

def os
OS.new(
parsed_xml.xpath("//template/os/name").text,
parsed_xml.xpath("//template/os/version").text,
parsed_xml.xpath("//template/os/arch").text
)
end

private

def parsed_xml
@parsed_xml ||= ::Nokogiri::XML::Document.parse(xml)
end
end
end
end
11 changes: 10 additions & 1 deletion spec/models/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@ module Tim
Template.find(template).base_images.size.should == 2
end
end

describe "XML helper methods" do
it 'should return os struct' do
os = FactoryGirl.build(:template).os
os.name.should == 'Fedora'
os.version.should == '15'
os.arch.should == 'x86_64'
end
end
end
end
end

0 comments on commit 1845bf4

Please sign in to comment.