55
66# A parameter (AKA option, AKA implementation-defined value) supported by an extension
77class ExtensionParameter
8+ # @return [ArchDef] The defining Arch def
9+ attr_reader :archdef
10+
811 # @return [String] Parameter name
912 attr_reader :name
1013
1114 # @return [String] Asciidoc description
1215 attr_reader :desc
1316
14- # @return [Hash ] JSON Schema for the parameter value
17+ # @return [Schema ] JSON Schema for this param
1518 attr_reader :schema
1619
1720 # @return [String] Ruby code to perform validation above and beyond JSON schema
@@ -25,25 +28,46 @@ class ExtensionParameter
2528 attr_reader :exts
2629
2730 # @returns [Idl::Type] Type of the parameter
28- attr_reader :type
31+ attr_reader :idl_type
2932
3033 # Pretty convert extension schema to a string.
3134 def schema_type
32- Schema . new ( @schema ) . to_pretty_s
35+ @schema . to_pretty_s
3336 end
3437
35- def initialize ( name , desc , schema , extra_validation , exts )
38+ def initialize ( ext , name , data )
39+ @archdef = ext . arch_def
40+ @data = data
3641 @name = name
37- @desc = desc
38- @schema = schema
39- @extra_validation = extra_validation
40- @exts = exts
41- begin
42- @type = Idl ::Type . from_json_schema ( @schema ) . make_const . freeze
43- rescue
44- warn "While parsing scheme for ExtensionParameter #{ ext . name } .#{ name } "
45- raise
42+ @desc = data [ "description" ]
43+ @schema = Schema . new ( data [ "schema" ] )
44+ @extra_validation = data [ "extra_validation" ]
45+ also_defined_in = [ ]
46+ unless data [ "also_defined_in" ] . nil?
47+ if data [ "also_defined_in" ] . is_a? ( String )
48+ other_ext = @archdef . extension ( data [ "also_defined_in" ] )
49+ raise "Definition error in #{ ext . name } .#{ name } : #{ data [ 'also_defined_in' ] } is not a known extension" if other_ext . nil?
50+ also_defined_in << other_ext
51+ else
52+ unless data [ "also_defined_in" ] . is_a? ( Array ) && data [ "also_defined_in" ] . all? { |e | e . is_a? ( String ) }
53+ raise "schema error: also_defined_in should be a string or array of strings"
54+ end
55+
56+ data [ "also_defined_in" ] . each do |other_ext_name |
57+ other_ext = @archdef . extension ( other_ext_name )
58+ raise "Definition error in #{ ext . name } .#{ name } : #{ data [ 'also_defined_in' ] } is not a known extension" if other_ext . nil?
59+ also_defined_in << other_ext
60+ end
61+ end
4662 end
63+ @exts = [ ext ] + also_defined_in
64+ @idl_type = @schema . to_idl_type . make_const . freeze
65+ end
66+
67+ def defined_in_extension_version? ( version )
68+ return true if @data . dig ( "when" , "version" ) . nil?
69+
70+ Gem ::Requirement . new ( @data [ "when" ] [ "version" ] ) . satisfied_by? ( version )
4771 end
4872
4973 # @return [String]
@@ -147,31 +171,7 @@ def params
147171 @params = [ ]
148172 if @data . key? ( "params" )
149173 @data [ "params" ] . each do |param_name , param_data |
150- also_defined_in = [ ]
151- unless param_data [ "also_defined_in" ] . nil?
152- if param_data [ "also_defined_in" ] . is_a? ( String )
153- other_ext = arch_def . extension ( param_data [ "also_defined_in" ] )
154- raise "Definition error in #{ name } .#{ param_name } : #{ param_data [ 'also_defined_in' ] } is not a known extension" if other_ext . nil?
155- also_defined_in << other_ext
156- else
157- unless param_data [ "also_defined_in" ] . is_a? ( Array ) && param_data [ "also_defined_in" ] . all? { |e | e . is_a? ( String ) }
158- raise "schema error: also_defined_in should be a string or array of strings"
159- end
160-
161- param_data [ "also_defined_in" ] . each do |other_ext_name |
162- other_ext = arch_def . extension ( other_ext_name )
163- raise "Definition error in #{ name } .#{ param_name } : #{ param_data [ 'also_defined_in' ] } is not a known extension" if other_ext . nil?
164- also_defined_in << other_ext
165- end
166- end
167- end
168- @params << ExtensionParameter . new (
169- param_name ,
170- param_data [ "description" ] ,
171- param_data [ "schema" ] ,
172- param_data [ "extra_validation" ] ,
173- [ self ] + also_defined_in
174- )
174+ @params << ExtensionParameter . new ( self , param_name , param_data )
175175 end
176176 end
177177 @params
@@ -289,6 +289,11 @@ def ext(arch_def)
289289 arch_def . extension ( name )
290290 end
291291
292+ # @return [Array<ExtensionParameter>] The list of parameters for this extension version
293+ def params ( arch_def )
294+ ext ( arch_def ) . params . select { |p | p . defined_in_extension_version? ( @version ) }
295+ end
296+
292297 # @overload ==(other)
293298 # @param other [String] An extension name
294299 # @return [Boolean] whether or not this ExtensionVersion is named 'other'
0 commit comments