lib/ffi/accessors.rb in ffi-libfuse-0.0.1.rctest12 vs lib/ffi/accessors.rb in ffi-libfuse-0.1.0.rc20220550

- old
+ new

@@ -18,12 +18,12 @@ end # # Define a struct attribute reader for members # @param [Array<Symbol>] attrs the attribute names - # @param [String] format - # A format string containing a single %s to convert attr symbol to struct member + # @param [Proc|String] format + # A Proc, or format string containing a single %s, to convert attr to struct member name # @param [Boolean] simple # Controls how writer methods are defined using block # @param [Proc] block # An optional block to the struct field(s) into something more useful # @@ -31,11 +31,11 @@ # and should use __method__ to get the attr name, and self.class.ffi_attr_readers[__method__] to get the member # name # @return [void] def ffi_attr_reader(*attrs, format: '%s', simple: true, &block) attrs.each do |attr| - member = (format % attr).to_sym + member = (format.respond_to?(:call) ? format.call(attr) : format % attr).to_sym ffi_attr_readers[attr] = member if !block define_method(attr) { self[member] } elsif simple define_method(attr) { block.call(self[member]) } @@ -45,19 +45,19 @@ end end # Define a struct attribute writer # @param [Array<Symbol>] attrs the attribute names - # @param [String] format + # @param [String|Proc] format # A format string containing a single %s to convert attr symbol to struct member # @param [Boolean] simple # Controls how writer methods are defined using block # @param [Proc] block # An optional block to set the input value into the struct field. # # If simple is true then the struct field is set to the result of calling block with the input value, - # otherwise the method is defined directly from the block. Use __method__[0..-2] to get the attribute name - # and self.class.ffi_attr_writers[__method__[0..-2]] to get the struct field name + # otherwise the method is defined directly from the block. Use __method__[0..-1] to get the attribute name + # and self.class.ffi_attr_writers[__method__[0..-1]] to get the struct member name # @return [void] def ffi_attr_writer(*attrs, format: '%s', simple: true, &block) attrs.each do |attr| member = (format % attr).to_sym ffi_attr_writers[attr.to_sym] = member