1- require " membrane/schemas"
1+ require ' membrane/schemas'
22
33module Membrane
44end
55
66class Membrane ::SchemaParser
7- DEPARSE_INDENT = " " . freeze
7+ DEPARSE_INDENT = ' ' . freeze
88
99 class Dsl
1010 OptionalKeyMarker = Struct . new ( :key )
@@ -37,47 +37,46 @@ def tuple(*elem_schemas)
3737 end
3838 end
3939
40- def self . parse ( &blk )
41- new . parse ( &blk )
40+ def self . parse ( &)
41+ new . parse ( &)
4242 end
4343
4444 def self . deparse ( schema )
4545 new . deparse ( schema )
4646 end
4747
48- def parse ( &blk )
49- intermediate_schema = Dsl . new . instance_eval ( &blk )
48+ def parse ( &)
49+ intermediate_schema = Dsl . new . instance_eval ( &)
5050
5151 do_parse ( intermediate_schema )
5252 end
5353
5454 def deparse ( schema )
5555 case schema
5656 when Membrane ::Schemas ::Any
57- " any"
57+ ' any'
5858 when Membrane ::Schemas ::Bool
59- " bool"
59+ ' bool'
6060 when Membrane ::Schemas ::Class
6161 schema . klass . name
6262 when Membrane ::Schemas ::Dictionary
63- "dict(%s, %s)" % [ deparse ( schema . key_schema ) ,
64- deparse ( schema . value_schema ) ]
63+ sprintf ( 'dict(%s, %s)' , deparse ( schema . key_schema ) , deparse ( schema . value_schema ) )
6564 when Membrane ::Schemas ::Enum
66- " enum(%s)" % [ schema . elem_schemas . map { |es | deparse ( es ) } . join ( ", " ) ]
65+ sprintf ( ' enum(%s)' , schema . elem_schemas . map { |es | deparse ( es ) } . join ( ', ' ) )
6766 when Membrane ::Schemas ::List
68- " [%s]" % [ deparse ( schema . elem_schema ) ]
67+ sprintf ( ' [%s]' , deparse ( schema . elem_schema ) )
6968 when Membrane ::Schemas ::Record
7069 deparse_record ( schema )
7170 when Membrane ::Schemas ::Regexp
7271 schema . regexp . inspect
7372 when Membrane ::Schemas ::Tuple
74- " tuple(%s)" % [ schema . elem_schemas . map { |es | deparse ( es ) } . join ( ", " ) ]
73+ sprintf ( ' tuple(%s)' , schema . elem_schemas . map { |es | deparse ( es ) } . join ( ', ' ) )
7574 when Membrane ::Schemas ::Value
7675 schema . value . inspect
7776 when Membrane ::Schemas ::Base
7877 schema . inspect
7978 else
80- emsg = " Expected instance of Membrane::Schemas::Base, given instance of" \
79+ emsg = ' Expected instance of Membrane::Schemas::Base, given instance of' \
8180 + " #{ schema . class } "
8281 raise ArgumentError . new ( emsg )
8382 end
@@ -97,7 +96,7 @@ def do_parse(object)
9796 Membrane ::Schemas ::Regexp . new ( object )
9897 when Dsl ::DictionaryMarker
9998 Membrane ::Schemas ::Dictionary . new ( do_parse ( object . key_schema ) ,
100- do_parse ( object . value_schema ) )
99+ do_parse ( object . value_schema ) )
101100 when Dsl ::EnumMarker
102101 elem_schemas = object . elem_schemas . map { |s | do_parse ( s ) }
103102 Membrane ::Schemas ::Enum . new ( *elem_schemas )
@@ -113,25 +112,23 @@ def do_parse(object)
113112
114113 def parse_list ( schema )
115114 if schema . empty?
116- raise ArgumentError . new ( " You must supply a schema for elements." )
115+ raise ArgumentError . new ( ' You must supply a schema for elements.' )
117116 elsif schema . length > 1
118- raise ArgumentError . new ( " Lists can only match a single schema." )
117+ raise ArgumentError . new ( ' Lists can only match a single schema.' )
119118 end
120119
121120 Membrane ::Schemas ::List . new ( do_parse ( schema [ 0 ] ) )
122121 end
123122
124123 def parse_record ( schema )
125- if schema . empty?
126- raise ArgumentError . new ( "You must supply at least one key-value pair." )
127- end
124+ raise ArgumentError . new ( 'You must supply at least one key-value pair.' ) if schema . empty?
128125
129126 optional_keys = [ ]
130127
131128 parsed = { }
132129
133130 schema . each do |key , value_schema |
134- if key . kind_of ?( Dsl ::OptionalKeyMarker )
131+ if key . is_a ?( Dsl ::OptionalKeyMarker )
135132 key = key . key
136133 optional_keys << key
137134 end
@@ -143,40 +140,38 @@ def parse_record(schema)
143140 end
144141
145142 def deparse_record ( schema )
146- lines = [ "{" ]
143+ lines = [ '{' ]
147144
148145 schema . schemas . each do |key , val_schema |
149- dep_key = nil
150- if schema . optional_keys . include? ( key )
151- dep_key = " optional(%s)" % [ key . inspect ]
152- else
153- dep_key = key . inspect
154- end
146+ nil
147+ dep_key = if schema . optional_keys . include? ( key )
148+ sprintf ( ' optional(%s)' , key . inspect )
149+ else
150+ key . inspect
151+ end
155152
156153 dep_key = DEPARSE_INDENT + dep_key
157154 dep_val_schema_lines = deparse ( val_schema ) . split ( "\n " )
158155
159156 dep_val_schema_lines . each_with_index do |line , line_idx |
160- to_append = nil
157+ nil
161158
162- if 0 == line_idx
163- to_append = " %s => %s" % [ dep_key , line ]
164- else
165- # Indent continuation lines
166- to_append = DEPARSE_INDENT + line
167- end
159+ to_append = if 0 == line_idx
160+ sprintf ( ' %s => %s' , dep_key , line )
161+ else
162+ # Indent continuation lines
163+ DEPARSE_INDENT + line
164+ end
168165
169166 # This concludes the deparsed schema, append a comma in preparation
170167 # for the next k-v pair.
171- if dep_val_schema_lines . size - 1 == line_idx
172- to_append += ","
173- end
168+ to_append += ',' if dep_val_schema_lines . size - 1 == line_idx
174169
175170 lines << to_append
176171 end
177172 end
178173
179- lines << "}"
174+ lines << '}'
180175
181176 lines . join ( "\n " )
182177 end
0 commit comments