Class: FlagSet::Base
- Inherits:
-
Object
- Object
- FlagSet::Base
- Defined in:
- lib/flagset.rb
Overview
The class FlagSet.define creates derives from Base
Class Method Summary collapse
- .[](*args) ⇒ Object
- .all_flags_and_ints ⇒ Hash<Symbol,Integer>
- .all_flags_mask ⇒ Integer
- .define_flags_class_methods ⇒ Object
- .define_flags_instance_methods ⇒ Object
-
.elementary_flag_names ⇒ Array<Symbol>
elementary flags are flags that are not aliases.
- .flagset_builder ⇒ Object
- .int_to_names(int) ⇒ Object
- .name_to_int(name) ⇒ Object
Instance Method Summary collapse
-
#&(*args) ⇒ Object
(also: #intersect)
set manipulation methods ###.
- #-(*args) ⇒ Object (also: #difference)
- #^(*args) ⇒ Object
- #all? ⇒ Boolean
- #any? ⇒ Boolean
-
#initialize(*args) ⇒ Base
constructor
A new instance of Base.
- #intersect?(*args) ⇒ Boolean (also: #has_any_of?, #===)
- #none? ⇒ Boolean
- #proper_subset?(*args) ⇒ Boolean
- #proper_superset?(*args) ⇒ Boolean
- #subset?(*args) ⇒ Boolean
- #superset?(*args) ⇒ Boolean (also: #has_all_of?)
- #to_i ⇒ Object
- #to_names ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- #|(*args) ⇒ Object (also: #union, #+)
- #~@ ⇒ Object
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_ints ⇒ 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_mask ⇒ Integer
219 220 221 |
# File 'lib/flagset.rb', line 219 def all_flags_mask flagset_builder.current_all_mask end |
.define_flags_class_methods ⇒ Object
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_methods ⇒ Object
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_names ⇒ Array<Symbol>
elementary flags are flags that are not aliases
209 210 211 |
# File 'lib/flagset.rb', line 209 def elementary_flag_names flagset_builder.elementary_flag_names end |
.flagset_builder ⇒ Object
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
297 298 299 |
# File 'lib/flagset.rb', line 297 def all? @int_value == self.class.all_flags_mask end |
#any? ⇒ 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?, ===
291 292 293 |
# File 'lib/flagset.rb', line 291 def intersect?(*args) @int_value & args_to_int_value(*args) != 0 end |
#none? ⇒ Boolean
305 306 307 |
# File 'lib/flagset.rb', line 305 def none? @int_value == 0 end |
#proper_subset?(*args) ⇒ 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
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
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?
281 282 283 |
# File 'lib/flagset.rb', line 281 def superset?(*args) ~@int_value & args_to_int_value(*args) == 0 end |
#to_i ⇒ Object
125 126 127 |
# File 'lib/flagset.rb', line 125 def to_i @int_value end |
#to_names ⇒ Object
137 138 139 |
# File 'lib/flagset.rb', line 137 def to_names self.class.int_to_names(@int_value) end |
#to_s ⇒ Object 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 |