Class: ExtensionParameter

Inherits:
Object
  • Object
show all
Defined in:
lib/arch_obj_models/extension.rb

Overview

A parameter (AKA option, AKA implementation-defined value) supported by an extension

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ext, name, data) ⇒ ExtensionParameter

Returns a new instance of ExtensionParameter.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/arch_obj_models/extension.rb', line 38

def initialize(ext, name, data)
  @archdef = ext.arch_def
  @data = data
  @name = name
  @desc = data["description"]
  @schema = Schema.new(data["schema"])
  @extra_validation = data["extra_validation"]
  also_defined_in = []
  unless data["also_defined_in"].nil?
    if data["also_defined_in"].is_a?(String)
      other_ext = @archdef.extension(data["also_defined_in"])
      raise "Definition error in #{ext.name}.#{name}: #{data['also_defined_in']} is not a known extension" if other_ext.nil?
      also_defined_in << other_ext
    else
      unless data["also_defined_in"].is_a?(Array) && data["also_defined_in"].all? { |e| e.is_a?(String) }
        raise "schema error: also_defined_in should be a string or array of strings"
      end

      data["also_defined_in"].each do |other_ext_name|
        other_ext = @archdef.extension(other_ext_name)
        raise "Definition error in #{ext.name}.#{name}: #{data['also_defined_in']} is not a known extension" if other_ext.nil?
        also_defined_in << other_ext
      end
    end
  end
  @exts = [ext] + also_defined_in
  @idl_type = @schema.to_idl_type.make_const.freeze
end

Instance Attribute Details

#archdefArchDef (readonly)

Returns The defining Arch def.

Returns:

  • (ArchDef)

    The defining Arch def



9
10
11
# File 'lib/arch_obj_models/extension.rb', line 9

def archdef
  @archdef
end

#descString (readonly)

Returns Asciidoc description.

Returns:

  • (String)

    Asciidoc description



15
16
17
# File 'lib/arch_obj_models/extension.rb', line 15

def desc
  @desc
end

#extra_validationString? (readonly)

Returns:

  • (String)

    Ruby code to perform validation above and beyond JSON schema

  • (nil)

    If there is no extra validatino



22
23
24
# File 'lib/arch_obj_models/extension.rb', line 22

def extra_validation
  @extra_validation
end

#extsArray<Extension> (readonly)

Some parameters are defined by multiple extensions (e.g., CACHE_BLOCK_SIZE by Zicbom and Zicboz). When defined in multiple places, the parameter must mean the extact same thing.

Returns:

  • (Array<Extension>)

    The extension(s) that define this parameter



28
29
30
# File 'lib/arch_obj_models/extension.rb', line 28

def exts
  @exts
end

#idl_typeObject (readonly)

Returns the value of attribute idl_type.



31
32
33
# File 'lib/arch_obj_models/extension.rb', line 31

def idl_type
  @idl_type
end

#nameString (readonly)

Returns Parameter name.

Returns:

  • (String)

    Parameter name



12
13
14
# File 'lib/arch_obj_models/extension.rb', line 12

def name
  @name
end

#schemaSchema (readonly)

Returns JSON Schema for this param.

Returns:

  • (Schema)

    JSON Schema for this param



18
19
20
# File 'lib/arch_obj_models/extension.rb', line 18

def schema
  @schema
end

Instance Method Details

#<=>(other) ⇒ Object

sorts by name

Raises:

  • (ArgumentError)


86
87
88
89
90
# File 'lib/arch_obj_models/extension.rb', line 86

def <=>(other)
  raise ArgumentError, "ExtensionParameters are only comparable to other extension parameters" unless other.is_a?(ExtensionParameter)

  @name <=> other.name
end

#defined_in_extension_version?(version) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/arch_obj_models/extension.rb', line 67

def defined_in_extension_version?(version)
  return true if @data.dig("when", "version").nil?

  Gem::Requirement.new(@data["when"]["version"]).satisfied_by?(Gem::Version.new(version))
end

Returns:

  • (String)

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
82
83
# File 'lib/arch_obj_models/extension.rb', line 74

def name_potentially_with_link(exts)
  raise ArgumentError, "Expecting Array" unless exts.is_a?(Array)
  raise ArgumentError, "Expecting Array[Extension]" unless exts[0].is_a?(Extension)

  if exts.size == 1
    "<<ext-#{exts[0].name}-param-#{name}-def,#{name}>>"
  else
    "#{name}"
  end
end

#schema_typeObject

Pretty convert extension schema to a string.



34
35
36
# File 'lib/arch_obj_models/extension.rb', line 34

def schema_type
  @schema.to_pretty_s
end