Skip to content
Browse files

clean up relationships a bit

  • Loading branch information...
1 parent f46ab92 commit efa8bea065102e24a7edc50f1416cca2e6eae5f0 @codekitchen committed
Showing with 11 additions and 25 deletions.
  1. +0 −3 README.md
  2. +3 −3 lib/beerxml.rb
  3. +6 −13 lib/beerxml/model.rb
  4. +0 −4 lib/beerxml/recipe.rb
  5. +2 −2 spec/full_stack_spec.rb
View
3 README.md
@@ -2,6 +2,3 @@ BeerXML.rb
==========
A ruby library for parsing and generating BeerXML documents (http://www.beerxml.com/). Also contains a lot of useful brewing calculations, conversions, and other tools.
-
-Version 0.0.1
--------------
View
6 lib/beerxml.rb
@@ -3,9 +3,6 @@
require 'nokogiri'
module Beerxml
- # This'll have to go eventually, but for now it's nice
- DataMapper.setup(:default, "abstract::")
-
def self.parse(string_or_io)
Beerxml::Model.from_xml(Nokogiri::XML(string_or_io).root)
end
@@ -17,3 +14,6 @@ def self.round(float, to = 0)
end
require 'beerxml/model'
+
+# This'll have to go eventually, but for now it's nice
+DataMapper.setup(:default, "abstract::")
View
19 lib/beerxml/model.rb
@@ -43,7 +43,9 @@ def from_xml(node)
read_xml_field(node, property.name.to_s)
end
# load any has-many relationships with other beerxml models
- each_beerxml_relationship do |rel|
+ relationships.each do |name, rel|
+ # don't ever serialize the parent recipe
+ next if rel.name == :recipe
# look for the plural element in the children of this node
# e.g., for Hop, see if there's any HOPS element.
(node>rel.child_model.beerxml_plural_name).each do |child_wrapper_node|
@@ -83,7 +85,9 @@ def to_beerxml(parent = Nokogiri::XML::Document.new)
x.content = self.class.properties[attr].dump(val)
node.add_child(x)
end
- each_beerxml_relationship do |rel|
+ relationships.each do |name, rel|
+ # don't ever serialize the parent recipe
+ next if rel.name == :recipe
objs = self.send(rel.name)
next if objs.empty?
sub_node = Nokogiri::XML::Node.new(rel.child_model.beerxml_plural_name, node)
@@ -101,17 +105,6 @@ def to_xml
def self.xml_attr_name(name)
name.to_s.upcase
end
-
- def beerxml_relationships
- []
- end
-
- def each_beerxml_relationship
- relationships.each do |name, rel|
- next unless beerxml_relationships.include?(rel.name)
- yield rel
- end
- end
end
%w(hop recipe fermentable yeast).
View
4 lib/beerxml/recipe.rb
@@ -17,10 +17,6 @@ class Beerxml::Recipe < Beerxml::Model
has n, :yeasts
# has n, :waters
- def beerxml_relationships
- [:hops, :fermentables, :miscs, :yeasts, :waters]
- end
-
property :asst_brewer, String
property :notes, String, :length => 65535
property :taste_notes, String, :length => 65535
View
4 spec/full_stack_spec.rb
@@ -72,8 +72,8 @@
r2 = Recipe.new.from_xml(xml.root)
r2.should be_valid
r2.attributes.should == @recipe.attributes
- r2.each_beerxml_relationship do |rel|
- r2.send(rel.name).map(&:attributes).should == @recipe.send(rel.name).map(&:attributes)
+ Recipe.relationships.each do |name, rel|
+ r2.send(name).map(&:attributes).should == @recipe.send(name).map(&:attributes)
end
end

0 comments on commit efa8bea

Please sign in to comment.
Something went wrong with that request. Please try again.