Class: GodObject::BitSet::Configuration
- Inherits:
-
Object
- Object
- GodObject::BitSet::Configuration
- Defined in:
- lib/god_object/bit_set/configuration.rb
Overview
A Configuration defines the digits of a BitSet. Additionally it can hold information on how to represent the digits in a String representation.
Constant Summary collapse
- UNNAMED_ENABLED =
Returns the default String representation for enabled digits.
'1'.freeze
- UNNAMED_DISABLED =
Returns the default String representation for disabled digits.
'0'.freeze
- NAMED_DISABLED =
Returns the default String representation for disabled digits which have a custom enabled representation.
'-'.freeze
Instance Attribute Summary collapse
-
#digits ⇒ Array<Symbol>
readonly
An ordered list of all configured digit names.
-
#valid_range ⇒ Range<Integer>
readonly
The Range in which an Integer representation of a BitSet with this Configuration can be.
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Answers if another object is equal.
-
#binary_position(digit) ⇒ Integer
The Integer representation of a BitSet where only the given digit is enabled.
-
#disabled_character(digit) ⇒ String
The String representation for the given digit when it is disabled.
-
#enabled_character(digit) ⇒ String
The String representation for the given digit when it is enabled.
-
#eql?(other) ⇒ true, false
Answers if another object is equal and of the same type family.
-
#find_digit(index_or_digit) ⇒ Symbol
The digit's name.
-
#hash ⇒ see Hash#hash
Identity hash for hash table usage.
-
#initialize(configuration) ⇒ Configuration
constructor
Initializes a new BitSet::Configuration.
-
#new(*state) ⇒ GodObject::BitSet
A new BitSet object with the current configuration.
-
#unique_characters? ⇒ true, false
Answers if all digits have unique enabled representations.
Constructor Details
#initialize(digits) ⇒ Configuration #initialize(enabled_representations_by_digits) ⇒ Configuration #initialize(representations_by_digits) ⇒ Configuration
Initializes a new BitSet::Configuration
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/god_object/bit_set/configuration.rb', line 89 def initialize(configuration) @digits = {} @enabled = {} @disabled = {} configuration.each do |digit, display| digit = digit.to_sym @digits[digit] = nil case when display.respond_to?(:all?) && display.all?{|s| s.respond_to?(:to_str) && s.to_str.length == 1} @enabled[digit] = display.first.to_str.dup.freeze @disabled[digit] = display.last.to_str.dup.freeze when display.respond_to?(:to_str) && display.to_str.length == 1 @enabled[digit] = display.to_str.dup.freeze @disabled[digit] = NAMED_DISABLED when display.nil? @enabled[digit] = UNNAMED_ENABLED @disabled[digit] = UNNAMED_DISABLED else raise ArgumentError, 'Invalid configuration' end end raise ArgumentError, 'At least one digit must be configured' if digits.count < 1 @digits.keys.reverse.each_with_index{|digit, index| @digits[digit] = 2 ** index } @valid_range = Range.new(0, (@digits.values.first * 2) - 1).freeze @unique_characters = !@enabled.values.dup.uniq! end |
Instance Attribute Details
#digits ⇒ Array<Symbol> (readonly)
Returns an ordered list of all configured digit names.
133 134 135 |
# File 'lib/god_object/bit_set/configuration.rb', line 133 def digits @digits.keys end |
#valid_range ⇒ Range<Integer> (readonly)
Returns the Range in which an Integer representation of a BitSet with this Configuration can be.
74 75 76 |
# File 'lib/god_object/bit_set/configuration.rb', line 74 def valid_range @valid_range end |
Class Method Details
.build(configuration) ⇒ GodObject::BitSet::Configuration .build(digits) ⇒ GodObject::PosixMode::Mode .build(enabled_representations_by_digits) ⇒ GodObject::PosixMode::Mode .build(representations_by_digits) ⇒ GodObject::PosixMode::Mode
62 63 64 65 66 67 68 |
# File 'lib/god_object/bit_set/configuration.rb', line 62 def build(configuration) if configuration.is_a?(Configuration) configuration else new(configuration) end end |
Instance Method Details
#==(other) ⇒ true, false
Answers if another object is equal.
Equality is defined as having the same ordered list of digits.
189 190 191 192 193 |
# File 'lib/god_object/bit_set/configuration.rb', line 189 def ==(other) digits == other.digits rescue NoMethodError false end |
#binary_position(digit) ⇒ Integer
Returns the Integer representation of a BitSet where only the given digit is enabled.
147 148 149 |
# File 'lib/god_object/bit_set/configuration.rb', line 147 def binary_position(digit) @digits[find_digit(digit)] end |
#disabled_character(digit) ⇒ String
Returns the String representation for the given digit when it is disabled.
154 155 156 |
# File 'lib/god_object/bit_set/configuration.rb', line 154 def disabled_character(digit) @disabled[find_digit(digit)] end |
#enabled_character(digit) ⇒ String
Returns the String representation for the given digit when it is enabled.
161 162 163 |
# File 'lib/god_object/bit_set/configuration.rb', line 161 def enabled_character(digit) @enabled[find_digit(digit)] end |
#eql?(other) ⇒ true, false
Answers if another object is equal and of the same type family.
201 202 203 |
# File 'lib/god_object/bit_set/configuration.rb', line 201 def eql?(other) self == other && other.kind_of?(self.class) end |
#find_digit(index_or_digit) ⇒ Symbol
Returns the digit's name.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/god_object/bit_set/configuration.rb', line 167 def find_digit(index_or_digit) case when index_or_digit.respond_to?(:to_sym) digit = index_or_digit.to_sym raise ArgumentError, "Invalid digit name (#{index_or_digit})" unless @digits.keys.include?(digit) else digit = @digits.keys[index_or_digit.to_int] end raise ArgumentError, "Invalid index or digit (#{index_or_digit})" unless digit digit end |
#hash ⇒ see Hash#hash
Returns identity hash for hash table usage.
206 207 208 |
# File 'lib/god_object/bit_set/configuration.rb', line 206 def hash @digits.hash end |
#new(*state) ⇒ GodObject::BitSet
Returns a new BitSet object with the current configuration.
141 142 143 |
# File 'lib/god_object/bit_set/configuration.rb', line 141 def new(*state) BitSet.new(*state, self) end |
#unique_characters? ⇒ true, false
Answers if all digits have unique enabled representations.
127 128 129 |
# File 'lib/god_object/bit_set/configuration.rb', line 127 def unique_characters? @unique_characters end |