Class: GodObject::BitSet::Configuration

Inherits:
Object
  • Object
show all
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.

Returns:

  • (String)

    the default String representation for enabled digits

'1'.freeze
UNNAMED_DISABLED =

Returns the default String representation for disabled digits.

Returns:

  • (String)

    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.

Returns:

  • (String)

    the default String representation for disabled digits which have a custom enabled representation

'-'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(digits) ⇒ Configuration #initialize(enabled_representations_by_digits) ⇒ Configuration #initialize(representations_by_digits) ⇒ Configuration

Initializes a new BitSet::Configuration

Overloads:

  • #initialize(digits) ⇒ Configuration

    Parameters:

    • digits (Array<Symbol>)

      a list of digit names

  • #initialize(enabled_representations_by_digits) ⇒ Configuration

    Parameters:

    • enabled_representations_by_digits (Hash<Symbol => String>)

      digit names mapped to their enabled character representations

  • #initialize(representations_by_digits) ⇒ Configuration

    Parameters:

    • representations_by_digits (Hash<Symbol => Array<String>>)

      digit names mapped to their enabled and disabled character representations

Raises:

  • (ArgumentError)


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

#digitsArray<Symbol> (readonly)

Returns an ordered list of all configured digit names.

Returns:

  • (Array<Symbol>)

    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_rangeRange<Integer> (readonly)

Returns the Range in which an Integer representation of a BitSet with this Configuration can be.

Returns:

  • (Range<Integer>)

    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

Overloads:

  • .build(configuration) ⇒ GodObject::BitSet::Configuration

    Returns an existing instance of GodObject::BitSet::Configuration.

    Parameters:

    Returns:

  • .build(digits) ⇒ GodObject::PosixMode::Mode

    Returns a new Configuration object with given attributes.

    Parameters:

    • digits (Array<Symbol>)

      a list of digit names

    Returns:

    • (GodObject::PosixMode::Mode)

      a new Configuration object

  • .build(enabled_representations_by_digits) ⇒ GodObject::PosixMode::Mode

    Returns a new Configuration object with given attributes.

    Parameters:

    • enabled_representations_by_digits (Hash<Symbol => String>)

      digit names mapped to their enabled character representations

    Returns:

    • (GodObject::PosixMode::Mode)

      a new Configuration object

  • .build(representations_by_digits) ⇒ GodObject::PosixMode::Mode

    Returns a new Configuration object with given attributes.

    Parameters:

    • representations_by_digits (Hash<Symbol => Array<String>>)

      digit names mapped to their enabled and disabled character representations

    Returns:

    • (GodObject::PosixMode::Mode)

      a new Configuration object



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.

Parameters:

  • other (Object)

    an object to be checked for equality

Returns:

  • (true, false)

    true if the object is considered equal, false otherwise



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.

Returns:

  • (Integer)

    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.

Parameters:

  • digit (Symbol, Integer)

    the name or index of the digit

Returns:

  • (String)

    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.

Parameters:

  • digit (Symbol, Integer)

    the name or index of the digit

Returns:

  • (String)

    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.

Parameters:

  • other (Object)

    an object to be checked for equality

Returns:

  • (true, false)

    true if the object is considered equal and of the same type familiy, false otherwise

See Also:

  • Configuration#==


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.

Parameters:

  • index_or_digit (Symbol, Integer)

    the name or index of the digit

Returns:

  • (Symbol)

    the digit's name

Raises:

  • (ArgumentError)


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

#hashsee Hash#hash

Returns identity hash for hash table usage.

Returns:

  • (see Hash#hash)

    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.

Parameters:

  • state (Integer, Array<Symbol>)

    either the octal state of the BitSet or a list of enabled digits

Returns:



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.

Returns:

  • (true, false)

    true if all digits have unique enabled representations, false otherwise



127
128
129
# File 'lib/god_object/bit_set/configuration.rb', line 127

def unique_characters?
  @unique_characters
end