Class: Bitsy

Inherits:
Object
  • Object
show all
Defined in:
lib/bitsy.rb,
lib/bitsy/mask.rb,
lib/bitsy/version.rb

Defined Under Namespace

Classes: InvalidFlagError, Mask

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val = 0) ⇒ Bitsy

Returns a new instance of Bitsy.



24
25
26
# File 'lib/bitsy.rb', line 24

def initialize(val = 0)
  self.value = val
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/bitsy.rb', line 38

def method_missing(name, *args, &block)
  super unless name.match(/^has_/)

  if name.match(/_or_/)
    some(*name.to_s.gsub(/^has_/, '').split(/_or_/))
  else
    every(*name.to_s.gsub(/^has_/, '').split(/_and_/))
  end
end

Class Method Details

.flags(*flags) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/bitsy.rb', line 7

def self.flags(*flags)
  if flags.empty?
    @flags
  else
    @flags = flags
  end
end

.masksObject



15
16
17
18
19
20
21
22
# File 'lib/bitsy.rb', line 15

def self.masks
  @masks ||= flags.inject({}) do |memo, flag|
    mask = Bitsy::Mask.with_index(flag, self.flags.index(flag))
    self.const_set(flag.upcase, mask)
    memo[flag] = mask
    memo
  end
end

Instance Method Details

#&(other) ⇒ Object



82
83
84
# File 'lib/bitsy.rb', line 82

def &(other)
  self.class.new(value & other.to_i)
end

#^(other) ⇒ Object



90
91
92
# File 'lib/bitsy.rb', line 90

def ^(other)
  self.class.new(value ^ other.to_i)
end

#every(*flags) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/bitsy.rb', line 48

def every(*flags)
  flags.inject(true) do |memo, flag|
    mask = self.class.masks.fetch(flag.to_sym, nil)
    raise InvalidFlagError.new(flag) unless mask
    memo && !(value & mask.value).zero?
  end
end

#set(*flags) ⇒ Object Also known as: <<



64
65
66
# File 'lib/bitsy.rb', line 64

def set(*flags)
  self.value = flags
end

#some(*flags) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/bitsy.rb', line 56

def some(*flags)
  flags.inject(false) do |memo, flag|
    mask = self.class.masks.fetch(flag.to_sym, nil)
    raise InvalidFlagError.new(flag) unless mask
    memo || !(value & mask.value).zero?
  end
end

#to_aObject



32
33
34
35
36
# File 'lib/bitsy.rb', line 32

def to_a
  self.class.masks.each_with_object([]) do |(_, mask), memo|
    memo << mask.flag unless (value & mask.value).zero?
  end
end

#to_iObject



28
29
30
# File 'lib/bitsy.rb', line 28

def to_i
  value
end

#toggle(*flags) ⇒ Object



76
77
78
79
80
# File 'lib/bitsy.rb', line 76

def toggle(*flags)
  masks_for_flags(flags) do |mask|
    self.value = value ^ mask.value
  end
end

#unset(*flags) ⇒ Object



70
71
72
73
74
# File 'lib/bitsy.rb', line 70

def unset(*flags)
  masks_for_flags(flags) do |mask|
    self.value = value & ~mask.value
  end
end

#|(other) ⇒ Object



86
87
88
# File 'lib/bitsy.rb', line 86

def |(other)
  self.class.new(value | other.to_i)
end