Class: BitMagic::Adapters::Magician
- Inherits:
-
Object
- Object
- BitMagic::Adapters::Magician
- Defined in:
- lib/bit_magic/adapters/magician.rb
Overview
A container for bit_magic settings and fields Meant to be used internally by the Base adapter (and by extension, all adapters).
Constant Summary collapse
- DEFAULT_ACTION_OPTIONS =
Bits::DEFAULT_OPTIONS.merge({ :query_by_value => true, # can be true, false, or a number of bits :helpers => true, # TODO: enable deeper access control over injected helper methods :bits_wrapper => Bits, :bits_generator => BitsGenerator, }).freeze
Instance Attribute Summary collapse
-
#action_options ⇒ Hash
readonly
bit_magic options that define settings.
-
#bit_magic_name ⇒ Symbol
readonly
the name given to bit_magic.
-
#bits_length ⇒ Integer
readonly
total number of bits defined in field_options.
-
#field_list ⇒ Hash
readonly
name => bits key pairs based off field_options.
-
#field_options ⇒ Hash
readonly
bit_magic options that define fields.
-
#max_bit ⇒ Integer
readonly
the largest bit defined in field_options.
Class Method Summary collapse
-
.field_key_value_check(check) ⇒ Integer+
Checks if the key is a a field key definition (Integer or Range, defining bit or bit ranges in question).
-
.options_splitter(options = {}) ⇒ Hash
Separate field and action options from the options passed to bit_magic.
Instance Method Summary collapse
-
#bits ⇒ Array<Integer>
List of bits defined in @field_options.
-
#bits_generator ⇒ BitsGenerator
Used by adapters to generate value lists for particular bit operations.
-
#bits_wrapper ⇒ Class
Used by the Base#bit_magic to create a Bits wrapper around instances.
-
#define_bit_magic_methods(klass) ⇒ Object
Define helper methods on the instance, namespaced to the name given in the bit_magic invocation.
-
#initialize(name, options = {}) ⇒ Object
constructor
Initialize a new Magician, a container for bit_magic settings and fields.
-
#inspect ⇒ Object
Inspect this object.
Constructor Details
#initialize(name, options = {}) ⇒ Object
Initialize a new Magician, a container for bit_magic settings and fields
98 99 100 101 102 |
# File 'lib/bit_magic/adapters/magician.rb', line 98 def initialize(name, = {}) @bit_magic_name = name @field_options, @action_options = self.class.() end |
Instance Attribute Details
#action_options ⇒ Hash (readonly)
bit_magic options that define settings
12 13 14 |
# File 'lib/bit_magic/adapters/magician.rb', line 12 def @action_options end |
#bit_magic_name ⇒ Symbol (readonly)
the name given to bit_magic
12 13 14 |
# File 'lib/bit_magic/adapters/magician.rb', line 12 def bit_magic_name @bit_magic_name end |
#bits_length ⇒ Integer (readonly)
total number of bits defined in field_options
12 13 14 |
# File 'lib/bit_magic/adapters/magician.rb', line 12 def bits_length @bits_length end |
#field_list ⇒ Hash (readonly)
name => bits key pairs based off field_options
12 13 14 |
# File 'lib/bit_magic/adapters/magician.rb', line 12 def field_list @field_list end |
#field_options ⇒ Hash (readonly)
bit_magic options that define fields
12 13 14 |
# File 'lib/bit_magic/adapters/magician.rb', line 12 def @field_options end |
#max_bit ⇒ Integer (readonly)
the largest bit defined in field_options
12 13 14 |
# File 'lib/bit_magic/adapters/magician.rb', line 12 def max_bit @max_bit end |
Class Method Details
.field_key_value_check(check) ⇒ Integer+
Checks if the key is a a field key definition (Integer or Range, defining bit or bit ranges in question).
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bit_magic/adapters/magician.rb', line 29 def self.field_key_value_check(check) if check.is_a?(Integer) check elsif check.is_a?(Range) list = check.to_a valid = list.reduce(true) {|m, i| m && i.is_a?(Integer) } list if valid and list.length > 0 elsif check.is_a?(Array) valid = true list = check.collect {|i| key = self.field_key_value_check(i) key.nil? ? (valid = false; break;) : key }.flatten list if valid and list.length > 0 end end |
.options_splitter(options = {}) ⇒ Hash
Separate field and action options from the options passed to bit_magic
Valid keys for field options are:
Integer - for a specific bit position
Range - for a range of bits
Array<Integer> - an array of bit positions
The value of the key-pair should be a Symbol.
Action options are everything else that’s not a field option
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/bit_magic/adapters/magician.rb', line 60 def self.( = {}) field_opts = {} action_opts = DEFAULT_ACTION_OPTIONS.dup .each_pair do |check_key, value| if key = self.field_key_value_check(check_key) field_opts[key] = value else if (![:allow_failed_fields]) and (check_key.is_a?(Integer) or check_key.is_a?(Range) or check_key.is_a?(Array)) raise BitMagic::FieldError.new("key-pair expected to be a valid field option, but it is not: #{check_key.inspect} => #{value.inspect}. If this is an action option, you can disable this error by passing ':allow_failed_fields => true' as an option") end action_opts[check_key] = value end end [field_opts.freeze, action_opts.freeze] end |
Instance Method Details
#bits ⇒ Array<Integer>
List of bits defined in @field_options
175 176 177 |
# File 'lib/bit_magic/adapters/magician.rb', line 175 def bits @field_list.values.flatten end |
#bits_generator ⇒ BitsGenerator
Used by adapters to generate value lists for particular bit operations
190 191 192 |
# File 'lib/bit_magic/adapters/magician.rb', line 190 def bits_generator @bits_generator ||= (self.[:bits_generator] || BitsGenerator).new self end |
#bits_wrapper ⇒ Class
Used by the Base#bit_magic to create a Bits wrapper around instances
182 183 184 |
# File 'lib/bit_magic/adapters/magician.rb', line 182 def bits_wrapper self.[:bits_wrapper] || Bits end |
#define_bit_magic_methods(klass) ⇒ Object
Define helper methods on the instance, namespaced to the name given in the bit_magic invocation.
This is an internal method, only meant to be used by the Base adapter to during its setup phase. Should not be used directly.
Methods that are defined is namespaced to the name during initialization. Referred to here as NAMESPACE. These methods are available on instances.
NAMESPACE - defined by Base adapter, returns a Bits wrapper
NAMESPACE_enabled?(*field_names) - checks if all the given field names
or bit indices are enabled (value > 0)
NAMESPACE_disabled?(*field_names) - checks if all the given field names
or bit indices are disabled (value == 0)
The following are helpers, defined based on field names during initialization Refered to here as ‘name’.
name - returns the value of the field
name=(new_value) - sets the value of the field to the new value
name? - available only if the field is a single bit, returns true if
the value of the bit is 1, or false if 0
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/bit_magic/adapters/magician.rb', line 131 def define_bit_magic_methods(klass) names = @field_list bit_magic_name = @bit_magic_name klass.instance_eval do define_method(:"#{bit_magic_name}_enabled?") do |*fields| self.send(bit_magic_name).enabled?(*fields) end define_method(:"#{bit_magic_name}_disabled?") do |*fields| self.send(bit_magic_name).disabled?(*fields) end end if @action_options[:helpers] klass.instance_eval do names.each_pair do |name, bits| define_method(:"#{name}") do self.send(bit_magic_name)[name] end define_method(:"#{name}=") do |val| self.send(bit_magic_name)[name] = val end if bits.is_a?(Integer) or bits.length == 1 define_method(:"#{name}?") do self.send(bit_magic_name)[name] == 1 end end end end end end |
#inspect ⇒ Object
Inspect this object. This is customized just to shorten the output to actually be readable.
196 197 198 |
# File 'lib/bit_magic/adapters/magician.rb', line 196 def inspect "#<#{self.class.to_s} name=#{@bit_magic_name} field_list=#{@field_list.inspect}>" end |