Skip to content
Browse files

Added deparse method for schema classes.

- The method: Membrane::Schema::Base#deparse returns string
  representation of a schema.
- Changed class: Membrane::Schema::Dictionary to sub-class
  Membrane::Schema::Base.
- Changed method: Membrane::SchemaParser#deparse to call
  `name` instead of `inspect` on a Membrane::Schema::Class
   object.
- Added/changed tests.

Change-Id: I0b7e41c925ed26781ec235b1cb4f536440c0788d
  • Loading branch information...
1 parent 9b49be7 commit 78fcfc94f24b5863086501cdfb1818d80f50e909 @kowshik kowshik committed Jul 19, 2012
View
4 lib/membrane/schema/base.rb
@@ -14,4 +14,8 @@ class Membrane::Schema::Base
def validate(object)
raise NotImplementedError
end
+
+ def deparse
+ Membrane::SchemaParser.deparse(self)
+ end
end
View
2 lib/membrane/schema/dictionary.rb
@@ -6,7 +6,7 @@ module Schema
end
end
-class Membrane::Schema::Dictionary
+class Membrane::Schema::Dictionary < Membrane::Schema::Base
attr_reader :key_schema
attr_reader :value_schema
View
8 lib/membrane/schema_parser.rb
@@ -58,7 +58,7 @@ def deparse(schema)
when Membrane::Schema::Bool
"bool"
when Membrane::Schema::Class
- schema.klass.inspect
+ schema.klass.name
when Membrane::Schema::Dictionary
"dict(%s, %s)" % [deparse(schema.key_schema),
deparse(schema.value_schema)]
@@ -75,11 +75,7 @@ def deparse(schema)
when Membrane::Schema::Value
schema.value.inspect
when Membrane::Schema::Base
- if schema.respond_to?(:deparse)
- schema.deparse
- else
- schema.inspect
- end
+ schema.inspect
else
emsg = "Expected instance of Membrane::Schema::Base, given instance of" \
+ " #{schema.class}"
View
2 lib/membrane/version.rb
@@ -1,3 +1,3 @@
module Membrane
- VERSION = "0.0.1"
+ VERSION = "0.0.2"
end
View
16 spec/base_schema_spec.rb
@@ -0,0 +1,16 @@
+require "spec_helper"
+
+
+describe Membrane::Schema::Base do
+ describe "#validate" do
+ let(:schema) { Membrane::Schema::Base.new }
+
+ it "should raise error" do
+ expect { schema.validate }.to raise_error
+ end
+
+ it "should deparse" do
+ schema.deparse.should == schema.inspect
+ end
+ end
+end
View
11 spec/schema_parser_spec.rb
@@ -24,12 +24,12 @@
parser.deparse(schema).should == "bool"
end
- it "should call inspect on the class of a Membrane::Schema::Class schema" do
+ it "should call name on the class of a Membrane::Schema::Class schema" do
klass = String
- klass.should_receive(:inspect).twice
+ klass.should_receive(:name).twice
schema = Membrane::Schema::Class.new(klass)
- parser.deparse(schema).should == klass.inspect
+ parser.deparse(schema).should == klass.name
end
it "should deparse the k/v schemas of a Membrane::Schema::Dictionary schema" do
@@ -99,6 +99,11 @@
parser.deparse(enum_schema).should == 'tuple(String, Integer, "test")'
end
+ it "should call inspect on a Membrane::Schema::Base schema" do
+ schema = Membrane::Schema::Base.new
+ parser.deparse(schema).should == schema.inspect
+ end
+
it "should raise an error if given a non-schema" do
expect do
parser.deparse({})

0 comments on commit 78fcfc9

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