Module: FFI::Accessors
- Extended by:
- ClassMethods
- Included in:
- Flock, Libfuse::FuseArgs, Libfuse::FuseBufVec, Libfuse::FuseConfig, Libfuse::FuseConnInfo, Libfuse::FuseContext, Libfuse::FuseLoopConfig, StatVfs, StructWrapper
- Defined in:
- lib/ffi/accessors.rb
Overview
Syntax sugar for FFI::Struct
Modules that include Accessors are automatically extended by ClassMethods which provides for defining reader and writer methods over struct field members.
Although designed around needs of FFI::Struct, eg the ability to map natural ruby names to struct field names,
this module can be used over anything that stores attributes in a Hash like structure.
It provides equivalent method definitions to Module#attr_(reader|writer|accessor) except using the index methods
:[
Additionally it supports boolean attributes with '?' aliases for reader methods, and keeps track of attribute definitions to support #fill,#to_h etc.
Standard instance variable based attributes defined through #attr_(reader|writer|accessor) also get these features.
Defined Under Namespace
Modules: ClassMethods
Private Accessor helpers collapse
-
#ffi_attr_reader_member(attr_method, *default) ⇒ Array<Symbol,Symbol>
(private) Takes
__method__
and returns the corresponding attr and struct member names. -
#ffi_attr_writer_member(attr_method, *default) ⇒ Array<Symbol,Symbol>
(private) Takes
__method__
and returns the corresponding attr and struct member names.
Instance Method Summary collapse
-
#ffi_attr_fill(from, writers: self.class.ffi_attr_writers, **args) ⇒ Object
(private) Fill struct from another object or list of properties.
-
#fill(from = nil, **args) ⇒ self
Fill struct from another object or list of properties.
-
#inspect(readers: self.class.ffi_public_attr_readers) ⇒ String
Inspect attributes.
-
#to_h(readers: self.class.ffi_public_attr_readers) ⇒ Hash<Symbol,Object>
Convert struct to hash.
Methods included from ClassMethods
attr_accessor, attr_reader, attr_writer, ffi_attr_accessor, ffi_attr_reader, ffi_attr_reader_method, ffi_attr_readers, ffi_attr_writer, ffi_attr_writer_method, ffi_attr_writers, ffi_bitflag_accessor, ffi_bitflag_reader, ffi_bitflag_writer, ffi_public_attr_readers, ffi_public_attr_writers
Instance Method Details
#ffi_attr_fill(from, writers: self.class.ffi_attr_writers, **args) ⇒ Object
This private method allows an including classes' instance method to fill attributes through any writer method (vs ##fill which only sets attributes with public writers)
(private) Fill struct from another object or list of properties
426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/ffi/accessors.rb', line 426 def ffi_attr_fill(from, writers: self.class.ffi_attr_writers, **args) if from.is_a?(Hash) args.merge!(from) else writers.each do |w| r = w[0..-2] # strip trailing = send(w, from.public_send(r)) if from.respond_to?(r) end end args.transform_keys! { |k| :"#{k}=" } args.each_pair { |k, v| send(k, v) } self end |
#ffi_attr_reader_member(attr_method, *default) ⇒ Array<Symbol,Symbol>
(private) Takes __method__
and returns the corresponding attr and struct member names
453 454 455 456 |
# File 'lib/ffi/accessors.rb', line 453 def ffi_attr_reader_member(attr_method, *default) attr = ffi_attr(attr_method) [attr, self.class.ffi_attr_readers_map.fetch(attr, *default)] end |
#ffi_attr_writer_member(attr_method, *default) ⇒ Array<Symbol,Symbol>
(private) Takes __method__
and returns the corresponding attr and struct member names
464 465 466 467 |
# File 'lib/ffi/accessors.rb', line 464 def ffi_attr_writer_member(attr_method, *default) attr = ffi_attr(attr_method) [attr, self.class.ffi_attr_writers_map.fetch(attr, *default)] end |
#fill(from = nil, **args) ⇒ self
Fill struct from another object or list of properties
398 399 400 |
# File 'lib/ffi/accessors.rb', line 398 def fill(from = nil, **args) ffi_attr_fill(from, writers: self.class.ffi_public_attr_writers, **args) end |
#inspect(readers: self.class.ffi_public_attr_readers) ⇒ String
Inspect attributes
405 406 407 |
# File 'lib/ffi/accessors.rb', line 405 def inspect(readers: self.class.ffi_public_attr_readers) "#{self.class.name} {#{readers.map { |r| "#{r}: #{send(r)} " }.join(',')}" end |
#to_h(readers: self.class.ffi_public_attr_readers) ⇒ Hash<Symbol,Object>
Convert struct to hash
412 413 414 |
# File 'lib/ffi/accessors.rb', line 412 def to_h(readers: self.class.ffi_public_attr_readers) readers.to_h { |r| [r, send(r)] } end |