Class: Schema

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

Overview

represents a JSON Schema

Used when an object in the database specifies a constraint using JSON schema For example, extension parameters

Instance Method Summary collapse

Constructor Details

#initialize(schema_hash) ⇒ Schema

Returns a new instance of Schema.

Raises:

  • (ArgumentError)


10
11
12
13
14
# File 'lib/arch_obj_models/schema.rb', line 10

def initialize(schema_hash)
    raise ArgumentError, "Expecting hash" unless schema_hash.is_a?(Hash)

    @schema_hash = schema_hash
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/arch_obj_models/schema.rb', line 133

def empty?
  @schema_hash.empty?
end

#is_power_of_two?(num) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
# File 'lib/arch_obj_models/schema.rb', line 147

def is_power_of_two?(num)
  return false if num < 1
  return (num & (num-1)) == 0
end

#large2hex(value) ⇒ Object

Convert large integers to hex str.



115
116
117
118
119
120
121
122
123
# File 'lib/arch_obj_models/schema.rb', line 115

def large2hex(value)
  if value.nil?
    ""
  elsif value.is_a?(Integer)
    (value > 999) ? "0x" + value.to_s(16) : value.to_s
  else
    value.to_s
  end
end

#merge(other_schema) ⇒ Object

Raises:

  • (ArgumentError)


125
126
127
128
129
130
131
# File 'lib/arch_obj_models/schema.rb', line 125

def merge(other_schema)
  raise ArgumentError, "Expecting Schema" unless (other_schema.is_a?(Schema) || other_schema.is_a?(Hash))

  other_hash = other_schema.is_a?(Schema) ? other_schema.instance_variable_get(:@schema_hash) : other_schema

  Schema.new(@schema_hash.merge(other_hash))
end

#num_bits(min, max) ⇒ Object

If min to max range represents an unsigned number of bits, return the number of bits. Otherwise return 0



154
155
156
157
# File 'lib/arch_obj_models/schema.rb', line 154

def num_bits(min, max)
    return 0 unless min == 0
    is_power_of_two?(max+1) ? max.bit_length : 0
end

#single_value?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/arch_obj_models/schema.rb', line 137

def single_value?
    @schema_hash.key?("const")
end

#to_hHash

Returns Hash representation of the JSON Schema.

Returns:

  • (Hash)

    Hash representation of the JSON Schema



17
# File 'lib/arch_obj_models/schema.rb', line 17

def to_h = @schema_hash

#to_idl_typeIdl::Type

Returns THe IDL-equivalent type for this schema object.

Returns:

  • (Idl::Type)

    THe IDL-equivalent type for this schema object



160
161
162
# File 'lib/arch_obj_models/schema.rb', line 160

def to_idl_type
  Idl::Type.from_json_schema(@schema_hash)
end

#to_pretty_s(schema_hash = @schema_hash) ⇒ String

Returns A human-readable description of the schema.

Returns:

  • (String)

    A human-readable description of the schema

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/arch_obj_models/schema.rb', line 25

def to_pretty_s(schema_hash = @schema_hash)
  raise ArgumentError, "Expecting hash" unless schema_hash.is_a?(Hash)
  raise ArgumentError, "Expecting non-empty hash" if schema_hash.empty?

  if schema_hash.key?("const")
    large2hex(schema_hash["const"])
  elsif schema_hash.key?("enum")
    "[#{schema_hash["enum"].join(', ')}]"
  elsif schema_hash.key?("type")
    case schema_hash["type"]
    when "integer"
      min = schema_hash["minimum"]
      minstr = large2hex(min)
      max = schema_hash["maximum"]
      maxstr = large2hex(max)
      if min && max
        sz = num_bits(min, max)
        (sz > 0) ? "#{sz}-bit integer" : "#{minstr} to #{maxstr}"
      elsif min
        "&#8805; #{minstr}"
      elsif max
        "&#8804; #{maxstr}"
      else
        "integer"
      end
    when "string"
      format = schema_hash["format"]
      pattern = schema_hash["pattern"]
      if format
        format
      elsif pattern
        "string matching #{pattern}"
      else
        "string"
      end
    when "boolean"
      "boolean"
    when "array"
      items = schema_hash["items"]
      min_items = schema_hash["minItems"]
      max_items = schema_hash["maxItems"]
      size_str = if min_items && max_items
        if min_items == max_items
            "#{min_items}-element "
        else
            "#{min_items}-element to #{max_items}-element "
        end
      elsif min_items
        "at least #{min_items}-element "
      elsif max_items
        "at most #{max_items}-element "
      else
        ""
      end

      array_str = if items.nil?
        size_str + "array"
      else
        if items.is_a?(Hash)
          "#{size_str}array of #{to_pretty_s(items)}"
        elsif items.is_a?(Array)
          str = size_str + "array where: +\n"
          items.each_with_index do |item,index|
            str = str + "&nbsp;&nbsp;[#{index}] is #{to_pretty_s(item)} +\n"
          end
          additional_items = schema_hash["additionalItems"]
          if additional_items
            str = str + "additional items are: +\n&nbsp;&nbsp;" +
              to_pretty_s(additional_items)
          end
          str
        else
          raise "to_pretty_s unknown array items #{items} in #{schema_hash}"
        end
      end

      if schema_hash.key?("contains")
        array_str = array_str + " Contains : [#{to_pretty_s(schema_hash["contains"])}]"
      end

      array_str
    else
      raise "to_pretty_s unknown type #{schema_hash["type"]} in #{schema_hash}"
    end
  else
    raise "TODO: to_pretty_s schema for #{schema_hash}"
  end
end

#type_prettyString

Returns Human-readable type of the schema (e.g., array, string, integer).

Returns:

  • (String)

    Human-readable type of the schema (e.g., array, string, integer)



20
21
22
# File 'lib/arch_obj_models/schema.rb', line 20

def type_pretty
  @schema_hash["type"]
end

#valueObject



141
142
143
144
145
# File 'lib/arch_obj_models/schema.rb', line 141

def value
  raise "Schema is not a single value" unless single_value?

  @schema_hash["const"]
end