Class: Tamper::Pack

Inherits:
Object
  • Object
show all
Defined in:
lib/tamper/pack.rb

Direct Known Subclasses

BitmapPack, ExistencePack, IntegerPack

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr_name, possibilities, max_choices) ⇒ Pack

Returns a new instance of Pack.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
# File 'lib/tamper/pack.rb', line 11

def initialize(attr_name, possibilities, max_choices)
  @attr_name, @possibilities, @max_choices = attr_name, possibilities, max_choices
  @meta = {}

  raise ArgumentError, "Possibilities are empty for #{attr_name}!" if possibilities.nil? || possibilities.empty?
  @possibilities.map!(&:to_s) # tamper values/possibilities should always be strings.
end

Instance Attribute Details

#attr_nameObject (readonly)

Returns the value of attribute attr_name.



3
4
5
# File 'lib/tamper/pack.rb', line 3

def attr_name
  @attr_name
end

#bit_window_widthObject (readonly)

Returns the value of attribute bit_window_width.



5
6
7
# File 'lib/tamper/pack.rb', line 5

def bit_window_width
  @bit_window_width
end

#bitsetObject (readonly)

Returns the value of attribute bitset.



3
4
5
# File 'lib/tamper/pack.rb', line 3

def bitset
  @bitset
end

#encodingObject (readonly)

Returns the value of attribute encoding.



3
4
5
# File 'lib/tamper/pack.rb', line 3

def encoding
  @encoding
end

#item_window_widthObject (readonly)

Returns the value of attribute item_window_width.



5
6
7
# File 'lib/tamper/pack.rb', line 5

def item_window_width
  @item_window_width
end

#max_choicesObject (readonly)

Returns the value of attribute max_choices.



3
4
5
# File 'lib/tamper/pack.rb', line 3

def max_choices
  @max_choices
end

#max_guidObject (readonly)

Returns the value of attribute max_guid.



7
8
9
# File 'lib/tamper/pack.rb', line 7

def max_guid
  @max_guid
end

#metaObject

Returns the value of attribute meta.



9
10
11
# File 'lib/tamper/pack.rb', line 9

def meta
  @meta
end

#possibilitiesObject (readonly)

Returns the value of attribute possibilities.



3
4
5
# File 'lib/tamper/pack.rb', line 3

def possibilities
  @possibilities
end

Class Method Details

.build(attr_name, possibilities, max_choices) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/tamper/pack.rb', line 19

def self.build(attr_name, possibilities, max_choices)
  if (max_choices * Math.log2(possibilities.length)) < possibilities.length
    pack = IntegerPack
  else
    pack = BitmapPack
  end

  pack.new(attr_name, possibilities, max_choices)
end

Instance Method Details

#finalize_pack!Object

Most packs do not implement this.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tamper/pack.rb', line 41

def finalize_pack!
  data = @bitset.to_s
  byte_length = data.length / 8
  remaining_bits = data.length % 8

  output  = byte_length.to_s(2).rjust(32)
  output += remaining_bits.to_s(2).rjust(8)
  output += data

  @bitset = Bitset.from_s(output)
end

#to_hObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/tamper/pack.rb', line 29

def to_h
  output = { encoding: encoding,
            attr_name: attr_name,
            possibilities: possibilities,
            pack: encoded_bitset,
            item_window_width: item_window_width,
            bit_window_width: bit_window_width,
            max_choices: max_choices }
  output.merge(meta)
end