Class: Obfuskey::ObfusbitSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/obfuskey/obfusbit_schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_schema) ⇒ ObfusbitSchema

Returns a new instance of ObfusbitSchema.



5
6
7
8
9
10
11
12
13
# File 'lib/obfuskey/obfusbit_schema.rb', line 5

def initialize(raw_schema)
  validate_schema!(raw_schema)

  @definition = raw_schema
  @total_bits = raw_schema.sum { |item| item[:bits] || item["bits"] }
  @max_bits = (1 << @total_bits) - 1
  @field_info = calculate_field_info(raw_schema)
  @field_names = raw_schema.map { |item| (item[:name] || item["name"]).to_s }.to_set
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



3
4
5
# File 'lib/obfuskey/obfusbit_schema.rb', line 3

def definition
  @definition
end

#field_namesObject (readonly)

Returns the value of attribute field_names.



3
4
5
# File 'lib/obfuskey/obfusbit_schema.rb', line 3

def field_names
  @field_names
end

#max_bitsObject (readonly)

Returns the value of attribute max_bits.



3
4
5
# File 'lib/obfuskey/obfusbit_schema.rb', line 3

def max_bits
  @max_bits
end

#total_bitsObject (readonly)

Returns the value of attribute total_bits.



3
4
5
# File 'lib/obfuskey/obfusbit_schema.rb', line 3

def total_bits
  @total_bits
end

Instance Method Details

#field_infoObject



15
16
17
18
# File 'lib/obfuskey/obfusbit_schema.rb', line 15

def field_info
  # Return a deep copy so callers can't mutate internal state.
  @field_info.each_with_object({}) { |(k, v), h| h[k] = v.dup }
end

#get_field_info(name) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
# File 'lib/obfuskey/obfusbit_schema.rb', line 20

def get_field_info(name)
  info = @field_info[name.to_s]
  raise ArgumentError, "Field '#{name}' not found in schema." unless info
  info
end

#to_sObject Also known as: inspect



52
53
54
# File 'lib/obfuskey/obfusbit_schema.rb', line 52

def to_s
  "ObfusbitSchema(total_bits=#{@total_bits}, fields=#{@definition.length})"
end

#validate_values!(values) ⇒ Object



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
# File 'lib/obfuskey/obfusbit_schema.rb', line 26

def validate_values!(values)
  input_names = values.keys.map(&:to_s).to_set

  missing = @field_names - input_names
  unless missing.empty?
    raise ArgumentError,
      "Required values for the following fields are missing: #{missing.sort.join(', ')}."
  end

  extra = input_names - @field_names
  unless extra.empty?
    raise ArgumentError,
      "Unexpected fields provided in input values: #{extra.sort.join(', ')}."
  end

  values.each do |name, value|
    info = get_field_info(name)
    bits = info[:bits]
    unless value.is_a?(Integer) && value >= 0 && value < (1 << bits)
      raise BitOverflowError,
        "Value '#{name}' (#{value}) exceeds its allocated #{bits} bits " \
        "(maximum allowed: #{(1 << bits) - 1})."
    end
  end
end