Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Commit

Permalink
add support for styles
Browse files Browse the repository at this point in the history
  • Loading branch information
codekitchen committed Mar 11, 2011
1 parent efa8bea commit c6e4216
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
38 changes: 25 additions & 13 deletions lib/beerxml/model.rb
Expand Up @@ -19,7 +19,7 @@ def self.beerxml_plural_name
def self.inherited(klass)
super
@models[klass.beerxml_name] = klass
@plurals["#{klass.beerxml_name}S"] = klass
@plurals[klass.beerxml_plural_name] = klass
end

##########################
Expand All @@ -42,15 +42,21 @@ def from_xml(node)
properties.each do |property|
read_xml_field(node, property.name.to_s)
end
# load any has-many relationships with other beerxml models
# load any relationships with other beerxml models
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|
collection = Beerxml::Model.collection_from_xml(child_wrapper_node)
self.send(rel.name).concat(collection)
if rel.max == 1
(node>rel.child_model.beerxml_name).each do |child_node|
self.send("#{rel.name}=", rel.child_model.new.from_xml(child_node))
end
else
# 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|
collection = Beerxml::Model.collection_from_xml(child_wrapper_node)
self.send(rel.name).concat(collection)
end
end
end
self
Expand Down Expand Up @@ -88,11 +94,17 @@ def to_beerxml(parent = Nokogiri::XML::Document.new)
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)
node.add_child(sub_node)
objs.each { |o| o.to_beerxml(sub_node) }
if rel.max == 1
obj = self.send(rel.name)
next if obj.nil?
obj.to_beerxml(node)
else
objs = self.send(rel.name)
next if objs.empty?
sub_node = Nokogiri::XML::Node.new(rel.child_model.beerxml_plural_name, node)
node.add_child(sub_node)
objs.each { |o| o.to_beerxml(sub_node) }
end
end
parent.add_child(node)
parent
Expand All @@ -107,5 +119,5 @@ def self.xml_attr_name(name)
end
end

%w(hop recipe fermentable yeast).
%w(hop recipe fermentable yeast style).
each { |f| require "beerxml/#{f}" }
5 changes: 2 additions & 3 deletions lib/beerxml/recipe.rb
@@ -1,9 +1,8 @@
class Beerxml::Recipe < Beerxml::Model
include DataMapper::Resource

property :name, String, :required => true
property :type, String, :set => ['Extract', 'Partial Mash', 'All Grain'], :required => true
# has 1, :style, :required => true
has 1, :style
validates_presence_of :style
property :brewer, String, :required => true
property :batch_size, Volume, :required => true
property :boil_size, Volume, :required => true
Expand Down
25 changes: 25 additions & 0 deletions lib/beerxml/style.rb
@@ -0,0 +1,25 @@
class Beerxml::Style < Beerxml::Model
property :name, String, :required => true
property :category, String, :required => true
property :category_number, String, :required => true
property :style_letter, String, :required => true
property :style_guide, String, :required => true
property :type, String, :set => %w(Lager Ale Mead Wheat Mixed Cider), :required => true
property :og_min, Float, :required => true
property :og_max, Float, :required => true
property :fg_min, Float, :required => true
property :fg_max, Float, :required => true
property :ibu_min, Float, :required => true
property :ibu_max, Float, :required => true
property :color_min, Float, :required => true
property :color_max, Float, :required => true

property :carb_min, Float
property :carb_max, Float
property :abv_min, Float
property :abv_max, Float
property :notes, String, :length => 65535
property :profile, String, :length => 65535
property :ingredients, String, :length => 65535
property :examples, String, :length => 65535
end
17 changes: 16 additions & 1 deletion spec/full_stack_spec.rb
Expand Up @@ -57,6 +57,19 @@
:form => 'Dry',
:amount => 0.2.liters,
:attenuation => 75)
@recipe.style = Style.new(:name => "Blonde Ale",
:category => 'North American Origin',
:category_number => '1',
:style_letter => 'gba',
:type => 'Ale',
:og_min => 1.045,
:og_max => 1.056,
:fg_min => 1.008,
:fg_max => 1.016,
:ibu_min => 15,
:ibu_max => 25,
:color_min => 3,
:color_max => 7)
@recipe.should be_valid
end

Expand All @@ -73,7 +86,8 @@
r2.should be_valid
r2.attributes.should == @recipe.attributes
Recipe.relationships.each do |name, rel|
r2.send(name).map(&:attributes).should == @recipe.send(name).map(&:attributes)
next if @recipe.send(name).nil?
Array(r2.send(name)).map(&:attributes).should == Array(@recipe.send(name)).map(&:attributes)
end
end

Expand All @@ -87,6 +101,7 @@
:efficiency => 70.0)
@extract.hops.concat @recipe.hops
@extract.yeasts.concat @recipe.yeasts
@extract.style = @recipe.style.clone
@extract.fermentables << Fermentable.new(:name => 'Extra Light DME',
:type => 'Dry Extract',
:amount => U(5, 'lb'),
Expand Down

0 comments on commit c6e4216

Please sign in to comment.