Module: FFI::Accessors::ClassMethods
- Included in:
- FFI::Accessors, StructWrapper
- Defined in:
- lib/ffi/accessors.rb
Overview
Class methods for defining struct member accessors
Accessor Definition collapse
-
#ffi_attr_accessor(*attrs, **attrs_map) ⇒ Array<Symbol] list of methods defined
Define both reader and writer.
-
#ffi_attr_reader(*attrs, **attrs_map, &block) ⇒ Array<Symbol>
Define reader methods for the given attributes.
-
#ffi_attr_reader_method(*attrs, **attrs_map, &block) ⇒ Array<Symbol>
Define reader methods directly from a block.
-
#ffi_attr_writer(*attrs, **attrs_map, &block) ⇒ Array<Symbol>
Define struct attribute writers for the given attributes.
-
#ffi_attr_writer_method(*attrs, **attrs_map, &block) ⇒ Array<Symbol>
Define writer methods directly from a block.
-
#ffi_bitflag_accessor(member, *flags) ⇒ Array<Symbol>
Define individual flag accessors over a bitmask field.
-
#ffi_bitflag_reader(member, *flags) ⇒ Array<Symbol>
Define individual flag readers over a bitmask field.
-
#ffi_bitflag_writer(member, *flags) ⇒ Array<Symbol>
Define individual flag writers over a bitmask field.
Accessor Information collapse
-
#ffi_attr_readers ⇒ Array<Symbol>
List of accessor reader methods defined.
-
#ffi_attr_writers ⇒ Array<Symbol>
List of accessor writer methods (ie ending in '=').
-
#ffi_public_attr_readers ⇒ Array<Symbol>
List of public attr accessor reader methods.
-
#ffi_public_attr_writers ⇒ Array<Symbol>
List of public accessor writer methods (ie ending in '=').
Instance Method Summary collapse
-
#attr_accessor(*args) ⇒ Object
Override instance variable based accessor to build our enhanced readers and writers.
-
#attr_reader(*args) ⇒ Array<Symbol]
Standard instance variable based reader with support for boolean and integration with to_h, inspect etc..
-
#attr_writer(*args) ⇒ Object
Standard instance variable based writer with support for booleans and integration with fill etc..
Instance Method Details
#attr_accessor(*args) ⇒ Object
Override instance variable based accessor to build our enhanced readers and writers
155 156 157 |
# File 'lib/ffi/accessors.rb', line 155 def attr_accessor(*args) attr_reader(*args) + attr_writer(*args) end |
#attr_reader(*args) ⇒ Array<Symbol]
Standard instance variable based reader with support for boolean and integration with to_h, inspect etc..
The member registered for each attribute will be its instance variable symbol (ie with a leading '@')
141 142 143 144 |
# File 'lib/ffi/accessors.rb', line 141 def attr_reader(*args) super(*args.map { |a| a[-1] == '?' ? a[0..-2] : a }) ffi_attr_reader_method(**args.to_h { |a| [a, :"@#{a[-1] == '?' ? a[0..-2] : a}"] }) end |
#attr_writer(*args) ⇒ Object
Standard instance variable based writer with support for booleans and integration with fill etc..
The member registered for each attribute will be its instance variable symbol (ie with a leading '@')
149 150 151 152 |
# File 'lib/ffi/accessors.rb', line 149 def attr_writer(*args) super(*args.map { |a| a[-1] == '?' ? a[0..-2] : a }) ffi_attr_writer_method(**args.to_h { |a| [a, :"@#{a[-1] == '?' ? a[0..-2] : a}"] }) end |
#ffi_attr_accessor(*attrs, **attrs_map) ⇒ Array<Symbol] list of methods defined
Define both reader and writer
163 164 165 |
# File 'lib/ffi/accessors.rb', line 163 def ffi_attr_accessor(*attrs, **attrs_map) ffi_attr_reader(*attrs, **attrs_map) + ffi_attr_writer(*attrs, **attrs_map) end |
#ffi_attr_reader(*attrs, **attrs_map, &block) ⇒ Array<Symbol>
Define reader methods for the given attributes
187 188 189 190 191 192 193 |
# File 'lib/ffi/accessors.rb', line 187 def ffi_attr_reader(*attrs, **attrs_map, &block) ffi_attr_reader_method(*attrs, **attrs_map) do _attr, member = ffi_attr_reader_member(__method__) val = self[member] block ? instance_exec(val, &block) : val end end |
#ffi_attr_reader_method(*attrs, **attrs_map, &block) ⇒ Array<Symbol>
Define reader methods directly from a block
229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/ffi/accessors.rb', line 229 def ffi_attr_reader_method(*attrs, **attrs_map, &block) attr_methods = map_attributes(attrs, attrs_map).flat_map do |attr, member, bool| ffi_attr_readers_map[attr] = member define_method(attr, &block) if block next attr unless bool bool_alias = :"#{attr}?" alias_method(bool_alias, attr) [attr, bool_alias] end send(default_visibility, *attr_methods) attr_methods end |
#ffi_attr_readers ⇒ Array<Symbol>
Returns list of accessor reader methods defined. (excludes boolean aliases).
327 328 329 |
# File 'lib/ffi/accessors.rb', line 327 def ffi_attr_readers ffi_attr_readers_map.keys end |
#ffi_attr_writer(*attrs, **attrs_map, &block) ⇒ Array<Symbol>
Define struct attribute writers for the given attributes
252 253 254 255 256 257 |
# File 'lib/ffi/accessors.rb', line 252 def ffi_attr_writer(*attrs, **attrs_map, &block) ffi_attr_writer_method(*attrs, **attrs_map) do |val| _attr, member = ffi_attr_writer_member(__method__) self[member] = block ? instance_exec(val, &block) : val end end |
#ffi_attr_writer_method(*attrs, **attrs_map, &block) ⇒ Array<Symbol>
Define writer methods directly from a block
275 276 277 278 279 280 281 282 |
# File 'lib/ffi/accessors.rb', line 275 def ffi_attr_writer_method(*attrs, **attrs_map, &block) writer_methods = map_attributes(attrs, attrs_map) do |attr, member, _bool| ffi_attr_writers_map[attr] = member block ? define_method("#{attr}=", &block) : attr end send(default_visibility, *writer_methods) writer_methods end |
#ffi_attr_writers ⇒ Array<Symbol>
Returns list of accessor writer methods (ie ending in '=').
333 334 335 |
# File 'lib/ffi/accessors.rb', line 333 def ffi_attr_writers ffi_attr_writers_map.keys.map { |a| :"#{a}=" } end |
#ffi_bitflag_accessor(member, *flags) ⇒ Array<Symbol>
Define individual flag accessors over a bitmask field
286 287 288 289 |
# File 'lib/ffi/accessors.rb', line 286 def ffi_bitflag_accessor(member, *flags) ffi_bitflag_reader(member, *flags) ffi_bitflag_writer(member, *flags) end |
#ffi_bitflag_reader(member, *flags) ⇒ Array<Symbol>
Define individual flag readers over a bitmask field
295 296 297 298 299 300 301 |
# File 'lib/ffi/accessors.rb', line 295 def ffi_bitflag_reader(member, *flags) bool_attrs = flags.to_h { |f| [:"#{f}?", member] } ffi_attr_reader_method(**bool_attrs) do flag_attr, member = ffi_attr_reader_member(__method__) self[member].include?(flag_attr) end end |
#ffi_bitflag_writer(member, *flags) ⇒ Array<Symbol>
Define individual flag writers over a bitmask field
307 308 309 310 311 312 313 314 |
# File 'lib/ffi/accessors.rb', line 307 def ffi_bitflag_writer(member, *flags) writers = flags.to_h { |f| [f, member] } ffi_attr_writer_method(**writers) do |v| flag_attr, member = ffi_attr_writer_member(__method__) v ? self[member] += [flag_attr] : self[member] -= flag v end end |
#ffi_public_attr_readers ⇒ Array<Symbol>
Returns list of public attr accessor reader methods.
321 322 323 |
# File 'lib/ffi/accessors.rb', line 321 def ffi_public_attr_readers ffi_attr_readers & public_instance_methods end |
#ffi_public_attr_writers ⇒ Array<Symbol>
Returns list of public accessor writer methods (ie ending in '=').
339 340 341 |
# File 'lib/ffi/accessors.rb', line 339 def ffi_public_attr_writers ffi_attr_writers & public_instance_methods end |