Parent

Class Index [+]

Quicksearch

Flt::Support::Flags

This class stores a set of flags. It can be assign a FlagValues object (using values= or passing to the constructor) so that the flags can be store in an integer (bits).

Public Class Methods

new(*flags) click to toggle source

When a Flag object is created, the initial flags to be set can be passed, and also a FlagValues. If a FlagValues is passed an integer can be used to define the flags.

   Flags.new(:flag1, :flag3, FlagValues.new(:flag1,:flag2,:flag3))
   Flags.new(5, FlagValues.new(:flag1,:flag2,:flag3))
     # File lib/flt/support.rb, line 76
 76:       def initialize(*flags)
 77:         @values = nil
 78:         @flags = {}
 79: 
 80:         v = 0
 81: 
 82:         flags.flatten!
 83: 
 84:         flags.each do |flag|
 85:           case flag
 86:             when FlagValues
 87:               @values = flag
 88:             when Symbol, Class
 89:               @flags[flag] = true
 90:             when Integer
 91:               v |= flag
 92:             when Flags
 93:               @values = flag.values
 94:               @flags = flag.to_h.dup
 95:             else
 96:               raise InvalidFlagTypeError, "Invalid flag type for: #{flag.inspect}"
 97:           end
 98:         end
 99: 
100:         if v!=0
101:           raise InvalidFlagTypeError, "Integer flag values need flag bit values to be defined" if @values.nil?
102:           self.bits = v
103:         end
104: 
105:         if @values
106:           # check flags
107:           @flags.each_key{|flag| check flag}
108:         end
109: 
110:       end

Public Instance Methods

<<(flags) click to toggle source

Sets (makes true) one or more flags (passes as an array)

     # File lib/flt/support.rb, line 233
233:       def << (flags)
234:         if flags.kind_of?(Array)
235:           set(*flags)
236:         else
237:           set(flags)
238:         end
239:       end
==(other) click to toggle source
     # File lib/flt/support.rb, line 293
293:       def ==(other)
294:         if @values && other.values && compatible_values?(other.values)
295:           bits == other.bits
296:         else
297:           to_a.map{|s| s.to_s}.sort == other.to_a.map{|s| s.to_s}.sort
298:         end
299:       end
[](flag) click to toggle source

Retrieve the setting (true/false) of a flag

     # File lib/flt/support.rb, line 178
178:       def [](flag)
179:         check flag
180:         @flags[flag]
181:       end
[]=(flag,value) click to toggle source

Modifies the setting (true/false) of a flag.

     # File lib/flt/support.rb, line 184
184:       def []=(flag,value)
185:         check flag
186:         case value
187:           when true,1
188:             value = true
189:           when false,0,nil
190:             value = false
191:           else
192:             raise InvalidFlagValueError, "Invalid value: #{value.inspect}"
193:         end
194:         @flags[flag] = value
195:         value
196:       end
any?() click to toggle source

returns true if any flag is set

     # File lib/flt/support.rb, line 267
267:       def any?
268:         if @values
269:           bits != 0
270:         else
271:           to_a.size>0
272:         end
273:       end
bits() click to toggle source

Retrieves the flags as a bit-vector integer. Values must have been assigned.

     # File lib/flt/support.rb, line 141
141:       def bits
142:         if @values
143:           i = 0
144:           @flags.each do |f,v|
145:             bit_val = @values[f]
146:             i |= bit_val if v && bit_val
147:           end
148:           i
149:         else
150:           raise Error,"No flag values defined"
151:         end
152:       end
bits=(i) click to toggle source

Sets the flags as a bit-vector integer. Values must have been assigned.

     # File lib/flt/support.rb, line 155
155:       def bits=(i)
156:         if @values
157:           raise Error, "Invalid bits value #{i}" if i<0 || i>@values.all_flags_value
158:           clear!
159:           @values.each do |f,v|
160:             @flags[f]=true if (i & v)!=0
161:           end
162:         else
163:           raise Error,"No flag values defined"
164:         end
165:       end
clear(*flags) click to toggle source

Clears (makes false) one or more flags

     # File lib/flt/support.rb, line 216
216:       def clear(*flags)
217:         flags = flags.first if flags.size==1 && flags.first.instance_of?(Array)
218:         flags.each do |flag|
219:           if flag.kind_of?(Flags)
220:             #if @values && other.values && compatible_values(other_values)
221:             #  self.bits &= ~other.bits
222:             #else
223:               flags.concat other.to_a
224:             #end
225:           else
226:             check flag
227:             @flags[flag] = false
228:           end
229:         end
230:       end
clear!() click to toggle source

Clears all flags

     # File lib/flt/support.rb, line 117
117:       def clear!
118:         @flags = {}
119:       end
dup() click to toggle source
     # File lib/flt/support.rb, line 112
112:       def dup
113:         Flags.new(self)
114:       end
each(&blk) click to toggle source

Iterate on each flag/setting pair.

     # File lib/flt/support.rb, line 242
242:       def each(&blk)
243:         if @values
244:           @values.each do |f,v|
245:             blk.call(f,@flags[f])
246:           end
247:         else
248:           @flags.each(&blk)
249:         end
250:       end
each_clear() click to toggle source

Iterate on each cleared flag

     # File lib/flt/support.rb, line 260
260:       def each_clear
261:         each do |f,v|
262:           yield f if !v
263:         end
264:       end
each_set() click to toggle source

Iterate on each set flag

     # File lib/flt/support.rb, line 253
253:       def each_set
254:         each do |f,v|
255:           yield f if v
256:         end
257:       end
inspect() click to toggle source
     # File lib/flt/support.rb, line 286
286:       def inspect
287:         txt = "#{self.class.to_s}#{to_s}"
288:         txt << " (0x#{bits.to_s(16)})" if @values
289:         txt
290:       end
set(*flags) click to toggle source

Sets (makes true) one or more flags

     # File lib/flt/support.rb, line 199
199:       def set(*flags)
200:         flags = flags.first if flags.size==1 && flags.first.instance_of?(Array)
201:         flags.each do |flag|
202:           if flag.kind_of?(Flags)
203:             #if @values && other.values && compatible_values(other_values)
204:             #  self.bits |= other.bits
205:             #else
206:               flags.concat other.to_a
207:             #end
208:           else
209:             check flag
210:             @flags[flag] = true
211:           end
212:         end
213:       end
set!() click to toggle source

Sets all flags

     # File lib/flt/support.rb, line 122
122:       def set!
123:         if @values
124:           self.bits = @values.all_flags_value
125:         else
126:           raise Error,"No flag values defined"
127:         end
128:       end
to_a() click to toggle source

Returns the true flags as an array

     # File lib/flt/support.rb, line 276
276:       def to_a
277:         a = []
278:         each_set{|f| a << f}
279:         a
280:       end
to_h() click to toggle source

Retrieves the flags as a hash.

     # File lib/flt/support.rb, line 168
168:       def to_h
169:         @flags
170:       end
to_i() click to toggle source

Same as bits

     # File lib/flt/support.rb, line 173
173:       def to_i
174:         bits
175:       end
to_s() click to toggle source
     # File lib/flt/support.rb, line 282
282:       def to_s
283:         "[#{to_a.map{|f| f.to_s.split('::').last}.join(', ')}]"
284:       end
values() click to toggle source

Retrieves the flag bit values

     # File lib/flt/support.rb, line 136
136:       def values
137:         @values
138:       end
values=(fv) click to toggle source

Assign the flag bit values

     # File lib/flt/support.rb, line 131
131:       def values=(fv)
132:         @values = fv
133:       end

Private Instance Methods

check(flag) click to toggle source
     # File lib/flt/support.rb, line 304
304:       def check(flag)
305:         raise InvalidFlagType,"Flags must be defined as symbols or classes; invalid flag: #{flag.inspect}" unless flag.kind_of?(Symbol) || flag.instance_of?(Class)
306: 
307:         @values[flag] if @values # raises an invalid flag error if flag is invalid
308:         true
309:       end
compatible_values?(v) click to toggle source
     # File lib/flt/support.rb, line 311
311:       def compatible_values?(v)
312:         #@values.object_id==v.object_id
313:         @values == v
314:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.