123456789_123456789_123456789_123456789_123456789_

Class: FFI::ConstGenerator::Constant

Relationships & Source Files
Inherits: Object
Defined in: lib/ffi/tools/const_generator.rb

Overview

This class hold constants for ::FFI::ConstGenerator

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(name, format, cast, ruby_name = nil, converter = nil) ⇒ Constant

Parameters:

  • name (#to_s)
  • format (String)

    a printf format string to print the value out

  • cast (String)

    a C cast for the value

  • ruby_name (defaults to: nil)

    alternate ruby name for #to_ruby

  • converter (#call) (defaults to: nil)

    convert the value from a string to the appropriate type for #to_ruby.

[ GitHub ]

  
# File 'lib/ffi/tools/const_generator.rb', line 199

def initialize(name, format, cast, ruby_name = nil, converter=nil)
  @name = name
  @format = format
  @cast = cast
  @ruby_name = ruby_name
  @converter = converter
  @value = nil
end

Instance Attribute Details

#cast (readonly)

[ GitHub ]

  
# File 'lib/ffi/tools/const_generator.rb', line 190

attr_reader :name, :format, :cast

#format (readonly)

[ GitHub ]

  
# File 'lib/ffi/tools/const_generator.rb', line 190

attr_reader :name, :format, :cast

#name (readonly)

[ GitHub ]

  
# File 'lib/ffi/tools/const_generator.rb', line 190

attr_reader :name, :format, :cast

#value (rw)

[ GitHub ]

  
# File 'lib/ffi/tools/const_generator.rb', line 191

attr_accessor :value

Instance Method Details

#converted_valueObject

Return constant value (converted if a converter was defined).

Returns:

  • constant value.

[ GitHub ]

  
# File 'lib/ffi/tools/const_generator.rb', line 210

def converted_value
  if @converter
    @converter.call(@value)
  else
    @value
  end
end

#ruby_nameString

get constant ruby name

[ GitHub ]

  
# File 'lib/ffi/tools/const_generator.rb', line 220

def ruby_name
  @ruby_name || @name
end

#to_rubyString

Get an evaluable string from constant.

[ GitHub ]

  
# File 'lib/ffi/tools/const_generator.rb', line 226

def to_ruby
  "#{ruby_name} = #{converted_value}"
end