Class: BitMagic::Bits
- Inherits:
-
Object
- Object
- BitMagic::Bits
- Defined in:
- lib/bit_magic/bits.rb
Overview
This is a wrapper class for objects that want bitfield functionality. It implements bit field read, write and attribute field read and updating.
This is usually used alongside Magician (an adapter helper class).
If you’re using this class directly, you can subclass it and set DEFAULT_OPTIONS on your subclass to change default options.
Constant Summary collapse
- BOOLEAN_CASTER =
This casts a given input value into a boolean.
lambda {|i| !(i == false or i == 0) }
- DEFAULT_OPTIONS =
Default options
The bool_caster is expected to be overwritten depending on your use-case. eg, form fields can send ‘0’ or ‘f’ to mean false.
{ :attribute_name => 'flags', :default => 0, :updater => Proc.new {|bits, new_value| bits.instance.send(:"#{bits.attribute_name}=", new_value) }, :bool_caster => BOOLEAN_CASTER }.freeze
Instance Attribute Summary collapse
-
#field_list ⇒ Hash
readonly
a hash of field name => field bits key-pairs.
-
#instance ⇒ Object
readonly
the instance we’re doing operations for.
-
#options ⇒ Hash
readonly
options for this instance.
Instance Method Summary collapse
-
#attribute_name ⇒ String
Get the attribute name option (a method on the instance that returns the current bit field value).
-
#disabled?(*fields) ⇒ Boolean
Check whether all the given field names or bits are disabled (false or 0) On fields with more than one bit, will return true only if the value of the field is 0 (no bits set).
-
#enabled?(*fields) ⇒ Boolean
Check whether all the given field name or bits are enabled (true or 1) On fields with more than one bit, will return true if any of the bits are enabled (value > 0).
-
#field ⇒ BitMagic::BitField
Get a BitField instance for the current value.
-
#initialize(instance, magician_or_field_list = {}, options = {}) ⇒ BitMagic::Bits
constructor
This class wraps around any arbitrary objects (instance) that respond to certain methods (a getter and setter for the flag field).
-
#inspect ⇒ String
Inspect output.
-
#read(name, field = nil) ⇒ Integer
(also: #[])
Read a field or bit from its bit index or name.
-
#update(new_value) ⇒ Object
Update the bit field value on the instance with a new value using the updater Proc given during initialization.
-
#value ⇒ Integer
Get the current value from the instance.
-
#write(name, target_value) ⇒ Object
(also: #[]=)
Write a field or bit from its field name or index.
Constructor Details
#initialize(instance, magician_or_field_list = {}, options = {}) ⇒ BitMagic::Bits
This class wraps around any arbitrary objects (instance) that respond to certain methods (a getter and setter for the flag field).
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/bit_magic/bits.rb', line 68 def initialize(instance, magician_or_field_list = {}, = {}) if defined?(BitMagic::Adapters::Magician) and magician_or_field_list.is_a?(BitMagic::Adapters::Magician) @magician = magician_or_field_list @field_list = @magician.field_list @options = @magician..merge() else @field_list = magician_or_field_list @options = self.class::DEFAULT_OPTIONS.merge() end @instance = instance end |
Instance Attribute Details
#field_list ⇒ Hash (readonly)
a hash of field name => field bits key-pairs
25 26 27 |
# File 'lib/bit_magic/bits.rb', line 25 def field_list @field_list end |
#instance ⇒ Object (readonly)
the instance we’re doing operations for
25 26 27 |
# File 'lib/bit_magic/bits.rb', line 25 def instance @instance end |
#options ⇒ Hash (readonly)
options for this instance
25 26 27 |
# File 'lib/bit_magic/bits.rb', line 25 def @options end |
Instance Method Details
#attribute_name ⇒ String
Get the attribute name option (a method on the instance that returns the current bit field value).
In the future, attribute name may be a Proc. This class could also possibly be subclassed and this method overwritten if advanced lookup is necessary.
87 88 89 |
# File 'lib/bit_magic/bits.rb', line 87 def attribute_name @options[:attribute_name] end |
#disabled?(*fields) ⇒ Boolean
Check whether all the given field names or bits are disabled (false or 0) On fields with more than one bit, will return true only if the value of the field is 0 (no bits set).
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/bit_magic/bits.rb', line 176 def disabled?(*fields) memo = true field = self.field fields.flatten.each do |name| break unless memo memo &&= (read(name, field) == 0) end memo end |
#enabled?(*fields) ⇒ Boolean
Check whether all the given field name or bits are enabled (true or 1) On fields with more than one bit, will return true if any of the bits are enabled (value > 0)
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/bit_magic/bits.rb', line 139 def enabled?(*fields) memo = true field = self.field fields.flatten.each do |name| break unless memo memo &&= (read(name, field) >= 1) end memo end |
#field ⇒ BitMagic::BitField
Get a BitField instance for the current value.
Note: Value changes are NOT tracked and updated into the instance, so call this method directly as needed.
194 195 196 |
# File 'lib/bit_magic/bits.rb', line 194 def field BitField.new(self.value) end |
#inspect ⇒ String
Inspect output.
278 279 280 281 282 283 |
# File 'lib/bit_magic/bits.rb', line 278 def inspect = {} [:default] = @options[:default] [:attribute_name] = @options[:attribute_name] "#<#{self.class.to_s} #{@magician ? "bit_magic=#{@magician.bit_magic_name.inspect} " : nil}value=#{self.value}> options=#{.inspect}" end |
#read(name, field = nil) ⇒ Integer Also known as: []
Read a field or bit from its bit index or name
219 220 221 222 223 224 225 226 227 |
# File 'lib/bit_magic/bits.rb', line 219 def read(name, field = nil) field ||= self.field if name.is_a?(Integer) field.read_field(name) elsif bits = @field_list[name] field.read_field(bits) end end |
#update(new_value) ⇒ Object
Update the bit field value on the instance with a new value using the updater Proc given during initialization.
108 109 110 111 112 |
# File 'lib/bit_magic/bits.rb', line 108 def update(new_value) if @options[:updater].respond_to?(:call) @options[:updater].call(self, new_value) end end |
#value ⇒ Integer
Get the current value from the instance. It should return an integer if the value exists, otherwise anything falsy will be set to the default value given during initialization.
96 97 98 99 100 |
# File 'lib/bit_magic/bits.rb', line 96 def value value = @instance.send(attribute_name) value ||= @options[:default] value end |
#write(name, target_value) ⇒ Object Also known as: []=
Write a field or bit from its field name or index
Note: only the total bits of the field is used from the given value, so any additional bits are ignored. eg: writing a field with one bit as value of 4 will set the bit to 0, writing 5 sets it to 1.
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/bit_magic/bits.rb', line 257 def write(name, target_value) if name.is_a?(Symbol) self.write(@field_list[name], target_value) elsif name.is_a?(Integer) self.update self.field.write_bits(name => @options[:bool_caster].call(target_value)) elsif name.respond_to?(:[]) and target_value.respond_to?(:[]) bits = {} name.each_with_index do |bit, i| bits[bit] = @options[:bool_caster].call(target_value[i]) end self.update self.field.write_bits bits end end |