Module: FFI::DRY::ConstFlagsMap

Includes:
ConstMap
Defined in:
lib/ffi/dry.rb

Overview

Behaves just like ConstFlags, except that it returns a list of names for the flags. Name string lookups work same way as ConstFlags.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConstMap

#list, #slurp_constants

Class Method Details

.included(klass) ⇒ Object



350
351
352
# File 'lib/ffi/dry.rb', line 350

def self.included(klass)
  klass.extend(ConstFlagsMap)
end

Instance Method Details

#[](arg) ⇒ Object

A flexible lookup method for name to bit-flag mappings.

Parameters:

  • arg (String, Symbol, Integer)

    Symbol or String as a name to lookup a bit-flag value, or an Integer to lookup a corresponding names for the flags present in it.



359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/ffi/dry.rb', line 359

def [](arg)
  if arg.is_a? Integer
    ret = []
    if arg == 0
      n = list.invert[0]
      ret << n if n
    else
      list.invert.sort.each {|v,n| ret << n if v !=0 and (v & arg) == v }
    end
    return ret
  elsif arg.is_a? String or arg.is_a? Symbol
    list[arg.to_s.upcase]
  end
end