Permalink
Browse files

RespondTo config parsing methods

  • Loading branch information...
1 parent 707d40e commit 5d9cd1b31ea6e56c6df11acf3885f549890e1c07 @krakatoa krakatoa committed Feb 12, 2013
Showing with 67 additions and 7 deletions.
  1. +21 −0 lib/protocol/policies/respond_to.rb
  2. +46 −7 spec/respond_to_spec.rb
@@ -51,7 +51,28 @@ def self.parse_policy_pair_to_hash(pair)
policy_hash
end
+ def self.parse_match_to_pair(symbol, word)
+ symbol = nil if symbol.eql?("")
+ case symbol
+ when "*"
+ return Cucub::Protocol::Policies::RespondTo::LVL_ANY
+ when "~"
+ return Cucub::Protocol::Policies::RespondTo::LVL_SAME
+ when nil
+ return [LVL_SPECIFIC, word]
+ end
+ end
+
def self.parse(text_config)
+
+ regexp = /\s*(((~?)(\w*)|(\*))\s*)(>\s*((~?)(\w*)|(\*))\s*)(>\s*((~?)(\w+)|(\*)))/
+
+ matches = text_config.match(regexp)
+
+ instance_pair = Cucub::Protocol::Policies::RespondTo.parse_match_to_pair(matches[5] || matches[3], matches[4])
+ class_pair = Cucub::Protocol::Policies::RespondTo.parse_match_to_pair(matches[10] || matches[8], matches[9])
+ zone_pair = Cucub::Protocol::Policies::RespondTo.parse_match_to_pair(matches[15] || matches[13], matches[14])
+
Cucub::Protocol::Policies::RespondTo.new(instance_pair, class_pair, zone_pair)
end
end
View
@@ -39,16 +39,55 @@
end
describe ".parse" do
#subject { Cucub::Protocol::Loader.new }
+ context "when parsing a valid config" do
+ subject { Cucub::Protocol::Policies::RespondTo.parse("* > ~ > ~zone") }
+ it { subject.valid_instances_string.should == "any" }
+ it { subject.valid_classes_string.should == "same" }
+ it { subject.valid_zones_string.should == "same" }
+ end
+
+ context "when parsing a valid config" do
+ subject { Cucub::Protocol::Policies::RespondTo.parse("* > core > ~zone") }
+ it { subject.valid_instances_string.should == "any" }
+ it { subject.valid_classes_string.should == "specific{core}" }
+ it { subject.valid_zones_string.should == "same" }
+ end
+
+ context "when parsing a valid config" do
+ subject { Cucub::Protocol::Policies::RespondTo.parse("~ > ~ > ~zone") }
+ it { subject.valid_instances_string.should == "same" }
+ it { subject.valid_classes_string.should == "same" }
+ it { subject.valid_zones_string.should == "same" }
+ end
+
+ context "when parsing a valid config" do
+ subject { Cucub::Protocol::Policies::RespondTo.parse("~ > ~ > *") }
+ it { subject.valid_instances_string.should == "same" }
+ it { subject.valid_classes_string.should == "same" }
+ it { subject.valid_zones_string.should == "any" }
+ end
+
#context "when parsing a valid config" do
- # subject { Cucub::Protocol::Policies::RespondTo.parse("test") }
- # it { subject.valid_instances_string.should == "test" }
+ # subject { Cucub::Protocol::Policies::RespondTo.parse("*") }
+ # it { subject.valid_instances_string.should == "any" }
+ # it { subject.valid_classes_string.should == "same" }
+ # it { subject.valid_zones_string.should == "any" }
#end
-
- #context "when parsing an unexisting file" do
- # before { subject.set_path(@filepath + "/mock/unexisting.ini") }
- # it { subject.parse.should == false }
+ #
+ #context "when parsing a valid config" do
+ # subject { Cucub::Protocol::Policies::RespondTo.parse("* > *") }
+ # it { subject.valid_instances_string.should == "any" }
+ # it { subject.valid_classes_string.should == "any" }
+ # it { subject.valid_zones_string.should == "any" }
#end
-
+ #
+ #context "when parsing a valid config" do
+ # subject { Cucub::Protocol::Policies::RespondTo.parse("* > engine") }
+ # it { subject.valid_instances_string.should == "any" }
+ # it { subject.valid_classes_string.should == "specific{engine}" }
+ # it { subject.valid_zones_string.should == "any" }
+ #end
+
end
end

0 comments on commit 5d9cd1b

Please sign in to comment.