Class: Map

Inherits:
Object
  • Object
show all
Defined in:
lib/models/map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr = {}) ⇒ Map

Returns a new instance of Map.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
# File 'lib/models/map.rb', line 8

def initialize(attr = {})
  # map name as String
  @name = attr[:name]

  # crc hex encoded string (8 characters / 4 bytes)
  @crc = attr[:crc]
  raise "Error: map crc invalid type: #{@crc.class}" unless @crc.instance_of?(String)

  unless @crc.match(/[a-fA-F0-9]{8}/) # str encoded hex
    raise "Error: map crc raw string expects size 8 but got #{@crc.size}"
  end

  @crc_str = @crc
  @crc_arr = str_bytes(@crc)
  @crc = @crc_arr.pack('C*')

  # size has to be a positive Integer
  @size = attr[:size]

  # sha256 can be:
  #   hex encoded string (64 characters / 32 bytes)
  #   '491af17a510214506270904f147a4c30ae0a85b91bb854395bef8c397fc078c3'
  #
  #   raw string (32 characters)
  #   array of integers representing the bytes (32 elements)
  @sha256 = attr[:sha256]

  if @sha256.instance_of?(String)
    if @sha256.match(/[a-fA-F0-9]{64}/) # str encoded hex
      @sha256_str = @sha256
      @sha256_arr = str_bytes(@sha256)
      @sha256 = @sha256_arr.pack('C*')
    elsif @sha256.length == 32 # raw byte string
      @sha256_arr = @sha256
      @sha256 = @sha256_arr.pack('C*')
      @sha256_str = str_hex(@sha256).gsub(' ', '')
    else
      raise "Error: map sha256 raw string expects size 64 but got #{@sha256.size}"
    end
  elsif @sha256.instance_of?(Array) # int byte array
    raise "Error: map sha256 array expects size 32 but got #{@sha256.size}" if @sha256.size != 32

    @sha256_arr = @sha256
    @sha256 = @sha256.pack('C*')
    @sha256_str = @sha256.map { |b| b.to_s(16).rjust(2, '0') }.join
  else
    raise "Error: map sha256 invalid type: #{@sha256.class}"
  end
end

Instance Attribute Details

#crcObject (readonly)

Returns the value of attribute crc.



6
7
8
# File 'lib/models/map.rb', line 6

def crc
  @crc
end

#crc_arrObject (readonly)

Returns the value of attribute crc_arr.



6
7
8
# File 'lib/models/map.rb', line 6

def crc_arr
  @crc_arr
end

#crc_strObject (readonly)

Returns the value of attribute crc_str.



6
7
8
# File 'lib/models/map.rb', line 6

def crc_str
  @crc_str
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/models/map.rb', line 6

def name
  @name
end

#sha256Object (readonly)

Returns the value of attribute sha256.



6
7
8
# File 'lib/models/map.rb', line 6

def sha256
  @sha256
end

#sha256_arrObject (readonly)

Returns the value of attribute sha256_arr.



6
7
8
# File 'lib/models/map.rb', line 6

def sha256_arr
  @sha256_arr
end

#sha256_strObject (readonly)

Returns the value of attribute sha256_str.



6
7
8
# File 'lib/models/map.rb', line 6

def sha256_str
  @sha256_str
end

#sizeObject (readonly)

Returns the value of attribute size.



6
7
8
# File 'lib/models/map.rb', line 6

def size
  @size
end