Class: FlagSetMaker::FlagSet

Inherits:
Object
  • Object
show all
Defined in:
lib/flag_set_maker.rb

Instance Method Summary collapse

Constructor Details

#initialize(mod, names, zero = nil) ⇒ FlagSet

Returns a new instance of FlagSet.



9
10
11
12
13
14
15
16
17
# File 'lib/flag_set_maker.rb', line 9

def initialize(mod, names, zero= nil)
  @module= mod
  @flag_names= names.to_a()
  @zero_name= zero
  for i in 0...@flag_names.size
    mod.const_set(@flag_names[i], Flags.new(1 << i, self))
  end
  mod.const_set(@zero_name, Flags.new(0, self)) if @zero_name
end

Instance Method Details

#inspect(v = nil) ⇒ Object



31
32
33
# File 'lib/flag_set_maker.rb', line 31

def inspect(v= nil)
  return v ? format("%p::%s", @module, to_s(v)) : super()
end

#to_s(v) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/flag_set_maker.rb', line 19

def to_s(v)
  names= []
  @flag_names.each_with_index(){ |name, i| names.push(name) if v[i]==1 }
  if names.empty?()
    return (@zero_name.to_s() || "0")
  elsif names.size==1
    return names[0].to_s()
  else
    return "("+names.join("|")+")"
  end
end

#validate(v) ⇒ Object



35
36
37
# File 'lib/flag_set_maker.rb', line 35

def validate(v)
  return v&((1 << @flag_names.size)-1)
end