Class: Tamper::Pack
- Inherits:
-
Object
- Object
- Tamper::Pack
- Defined in:
- lib/tamper/pack.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attr_name ⇒ Object
readonly
Returns the value of attribute attr_name.
-
#bit_window_width ⇒ Object
readonly
Returns the value of attribute bit_window_width.
-
#bitset ⇒ Object
readonly
Returns the value of attribute bitset.
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#item_window_width ⇒ Object
readonly
Returns the value of attribute item_window_width.
-
#max_choices ⇒ Object
readonly
Returns the value of attribute max_choices.
-
#max_guid ⇒ Object
readonly
Returns the value of attribute max_guid.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#possibilities ⇒ Object
readonly
Returns the value of attribute possibilities.
Class Method Summary collapse
Instance Method Summary collapse
-
#finalize_pack! ⇒ Object
Most packs do not implement this.
-
#initialize(attr_name, possibilities, max_choices) ⇒ Pack
constructor
A new instance of Pack.
- #to_h ⇒ Object
Constructor Details
#initialize(attr_name, possibilities, max_choices) ⇒ Pack
Returns a new instance of Pack.
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 = {} 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_name ⇒ Object (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_width ⇒ Object (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 |
#bitset ⇒ Object (readonly)
Returns the value of attribute bitset.
3 4 5 |
# File 'lib/tamper/pack.rb', line 3 def bitset @bitset end |
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
3 4 5 |
# File 'lib/tamper/pack.rb', line 3 def encoding @encoding end |
#item_window_width ⇒ Object (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_choices ⇒ Object (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_guid ⇒ Object (readonly)
Returns the value of attribute max_guid.
7 8 9 |
# File 'lib/tamper/pack.rb', line 7 def max_guid @max_guid end |
#meta ⇒ Object
Returns the value of attribute meta.
9 10 11 |
# File 'lib/tamper/pack.rb', line 9 def end |
#possibilities ⇒ Object (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_h ⇒ Object
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() end |