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

Commit

Permalink
Pretty printing for value schemas
Browse files Browse the repository at this point in the history
Change-Id: Ic7e3e388d9ac7a5cb22c83a6ae523201796d8ad7
  • Loading branch information
mpage committed May 16, 2012
1 parent 91d34d1 commit 90b1fe7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/membrane/schema_parser.rb
Expand Up @@ -6,9 +6,9 @@ module Membrane
class Membrane::SchemaParser class Membrane::SchemaParser
class Dsl class Dsl
OptionalKeyMarker = Struct.new(:key) OptionalKeyMarker = Struct.new(:key)
DictionaryMarker = Struct.new(:key_schema, :value_schema) DictionaryMarker = Struct.new(:key_schema, :value_schema)
EnumMarker = Struct.new(:elem_schemas) EnumMarker = Struct.new(:elem_schemas)
TupleMarker = Struct.new(:elem_schemas) TupleMarker = Struct.new(:elem_schemas)


def any def any
Membrane::Schema::Any.new Membrane::Schema::Any.new
Expand Down Expand Up @@ -39,12 +39,25 @@ def self.parse(&blk)
new.parse(&blk) new.parse(&blk)
end end


def self.deparse(schema)
new.deparse(schema)
end

def parse(&blk) def parse(&blk)
intermediate_schema = Dsl.new.instance_eval(&blk) intermediate_schema = Dsl.new.instance_eval(&blk)


do_parse(intermediate_schema) do_parse(intermediate_schema)
end end


def deparse(schema)
case schema
when Membrane::Schema::Value
schema.value.inspect
else
schema.inspect
end
end

private private


def do_parse(object) def do_parse(object)
Expand Down
10 changes: 10 additions & 0 deletions spec/schema_parser_spec.rb
Expand Up @@ -3,6 +3,16 @@
describe Membrane::SchemaParser do describe Membrane::SchemaParser do
let(:parser) { Membrane::SchemaParser.new } let(:parser) { Membrane::SchemaParser.new }


describe "#deparse" do
it "should call inspect on the value of a Value schema" do
val = "test"
val.should_receive(:inspect).twice
schema = Membrane::Schema::Value.new(val)

Membrane::SchemaParser.deparse(schema).should == val.inspect
end
end

describe "#parse" do describe "#parse" do
it "should leave instances derived from Membrane::Schema::Base unchanged" do it "should leave instances derived from Membrane::Schema::Base unchanged" do
old_schema = Membrane::Schema::ANY old_schema = Membrane::Schema::ANY
Expand Down

0 comments on commit 90b1fe7

Please sign in to comment.