Class: FlagSet::Base

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

Overview

The class FlagSet.define creates derives from Base

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



121
122
123
# File 'lib/flagset.rb', line 121

def initialize(*args)
  @int_value = args_to_int_value(*args)
end

Class Method Details

.[](*args) ⇒ Object



223
224
225
# File 'lib/flagset.rb', line 223

def [](*args)
  new(*args)
end

.all_flags_and_intsHash<Symbol,Integer>

Returns:

  • (Hash<Symbol,Integer>)


214
215
216
# File 'lib/flagset.rb', line 214

def all_flags_and_ints
  flagset_builder.all_flags_and_ints
end

.all_flags_maskInteger

Returns:

  • (Integer)


219
220
221
# File 'lib/flagset.rb', line 219

def all_flags_mask
  flagset_builder.current_all_mask
end

.define_flags_class_methodsObject



227
228
229
230
231
232
233
# File 'lib/flagset.rb', line 227

def define_flags_class_methods
  all_flags_and_ints.each{| k,v |
    define_singleton_method(k){
      new(v)
    }
  }
end

.define_flags_instance_methodsObject



235
236
237
238
239
240
241
242
# File 'lib/flagset.rb', line 235

def define_flags_instance_methods
  all_flags_and_ints.each{| k,v |
    next if [:all, :none].include?(k)
    define_method("#{k}?"){
      has_any_of?(v)
    }
  }
end

.elementary_flag_namesArray<Symbol>

elementary flags are flags that are not aliases

Returns:

  • (Array<Symbol>)


209
210
211
# File 'lib/flagset.rb', line 209

def elementary_flag_names
  flagset_builder.elementary_flag_names
end

.flagset_builderObject



202
203
204
# File 'lib/flagset.rb', line 202

def flagset_builder
  # abstract
end

.int_to_names(int) ⇒ Object



196
197
198
199
200
# File 'lib/flagset.rb', line 196

def int_to_names(int)
  elementary_flag_names.select{| name |
    (int & (all_flags_and_ints[name])) != 0
  }
end

.name_to_int(name) ⇒ Object



192
193
194
# File 'lib/flagset.rb', line 192

def name_to_int(name)
  all_flags_and_ints[name] or raise NameError, "unknown flag name for #{self.class}: #{name}"
end

Instance Method Details

#&(*args) ⇒ Object Also known as: intersect

set manipulation methods ###



248
249
250
# File 'lib/flagset.rb', line 248

def &(*args)
  self.class.new(@int_value & args_to_int_value(*args))
end

#-(*args) ⇒ Object Also known as: difference



259
260
261
# File 'lib/flagset.rb', line 259

def -(*args)
  self.class.new(@int_value & ~args_to_int_value(*args))
end

#^(*args) ⇒ Object



264
265
266
# File 'lib/flagset.rb', line 264

def ^(*args)
  self.class.new(@int_value ^ args_to_int_value(*args))
end

#all?Boolean

Returns:

  • (Boolean)


297
298
299
# File 'lib/flagset.rb', line 297

def all?
  @int_value == self.class.all_flags_mask
end

#any?Boolean

Returns:

  • (Boolean)


301
302
303
# File 'lib/flagset.rb', line 301

def any?
  @int_value != 0
end

#intersect?(*args) ⇒ Boolean Also known as: has_any_of?, ===

Returns:

  • (Boolean)


291
292
293
# File 'lib/flagset.rb', line 291

def intersect?(*args)
  @int_value & args_to_int_value(*args) != 0
end

#none?Boolean

Returns:

  • (Boolean)


305
306
307
# File 'lib/flagset.rb', line 305

def none?
  @int_value == 0
end

#proper_subset?(*args) ⇒ Boolean

Returns:

  • (Boolean)


276
277
278
279
# File 'lib/flagset.rb', line 276

def proper_subset?(*args)
  v = args_to_int_value(*args)
  @int_value & ~v == 0 and @int_value != v
end

#proper_superset?(*args) ⇒ Boolean

Returns:

  • (Boolean)


286
287
288
289
# File 'lib/flagset.rb', line 286

def proper_superset?(*args)
  v = args_to_int_value(*args)
  ~@int_value & v == 0 and @int_value != v
end

#subset?(*args) ⇒ Boolean

Returns:

  • (Boolean)


272
273
274
# File 'lib/flagset.rb', line 272

def subset?(*args)
  @int_value & ~args_to_int_value(*args) == 0
end

#superset?(*args) ⇒ Boolean Also known as: has_all_of?

Returns:

  • (Boolean)


281
282
283
# File 'lib/flagset.rb', line 281

def superset?(*args)
  ~@int_value & args_to_int_value(*args) == 0
end

#to_iObject



125
126
127
# File 'lib/flagset.rb', line 125

def to_i
  @int_value
end

#to_namesObject



137
138
139
# File 'lib/flagset.rb', line 137

def to_names
  self.class.int_to_names(@int_value)
end

#to_sObject Also known as: inspect



129
130
131
132
133
134
# File 'lib/flagset.rb', line 129

def to_s
  '#<%s: [%s]>' % [
    self.class,
    to_names.map(&:inspect).join(',')
  ]
end

#|(*args) ⇒ Object Also known as: union, +



253
254
255
# File 'lib/flagset.rb', line 253

def |(*args)
  self.class.new(@int_value | args_to_int_value(*args))
end

#~@Object



268
269
270
# File 'lib/flagset.rb', line 268

def ~@
  self.class.new(self.class.all_flags_mask & ~@int_value)
end