Class: Obfuskey::ObfusbitSchema
- Inherits:
-
Object
- Object
- Obfuskey::ObfusbitSchema
- Defined in:
- lib/obfuskey/obfusbit_schema.rb
Instance Attribute Summary collapse
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
-
#field_names ⇒ Object
readonly
Returns the value of attribute field_names.
-
#max_bits ⇒ Object
readonly
Returns the value of attribute max_bits.
-
#total_bits ⇒ Object
readonly
Returns the value of attribute total_bits.
Instance Method Summary collapse
- #field_info ⇒ Object
- #get_field_info(name) ⇒ Object
-
#initialize(raw_schema) ⇒ ObfusbitSchema
constructor
A new instance of ObfusbitSchema.
- #to_s ⇒ Object (also: #inspect)
- #validate_values!(values) ⇒ Object
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
#definition ⇒ Object (readonly)
Returns the value of attribute definition.
3 4 5 |
# File 'lib/obfuskey/obfusbit_schema.rb', line 3 def definition @definition end |
#field_names ⇒ Object (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_bits ⇒ Object (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_bits ⇒ Object (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_info ⇒ Object
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
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_s ⇒ Object 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 |