Class: EacRubyUtils::Bit

Inherits:
Object show all
Defined in:
lib/eac_ruby_utils/bit.rb

Constant Summary collapse

VALID_VALUES =
[0, 1].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Bit

Returns a new instance of Bit.

Parameters:

  • value (Integer)


33
34
35
# File 'lib/eac_ruby_utils/bit.rb', line 33

def initialize(value)
  @value = self.class.validate_integer(value)
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



27
28
29
# File 'lib/eac_ruby_utils/bit.rb', line 27

def value
  @value
end

Class Method Details

.assert(obj) ⇒ Object



10
11
12
13
14
# File 'lib/eac_ruby_utils/bit.rb', line 10

def assert(obj)
  return obj if obj.is_a?(self)

  new(obj.to_i)
end

.valid_integer?(value) ⇒ Boolean

Returns:



16
17
18
# File 'lib/eac_ruby_utils/bit.rb', line 16

def valid_integer?(value)
  value.is_a?(::Integer) && VALID_VALUES.include?(value)
end

.validate_integer(value) ⇒ Object

Raises:

  • (::ArgumentError)


20
21
22
23
24
# File 'lib/eac_ruby_utils/bit.rb', line 20

def validate_integer(value)
  return value if valid_integer?(value)

  raise(::ArgumentError, "Invalid bit value: #{value} (Valid: #{VALID_VALUES})")
end

Instance Method Details

#one?Boolean

Returns:



38
39
40
# File 'lib/eac_ruby_utils/bit.rb', line 38

def one?
  !zero?
end

#to_iInteger

Returns:

  • (Integer)


43
44
45
# File 'lib/eac_ruby_utils/bit.rb', line 43

def to_i
  value
end