Class: FFIGen::Enum

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi_gen.rb,
lib/ffi_gen/java_output.rb,
lib/ffi_gen/ruby_output.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generator, name, comment) ⇒ Enum

Returns a new instance of Enum.



79
80
81
82
83
84
# File 'lib/ffi_gen.rb', line 79

def initialize(generator, name, comment)
  @generator = generator
  @name = name
  @comment = comment
  @constants = []
end

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



77
78
79
# File 'lib/ffi_gen.rb', line 77

def comment
  @comment
end

#constantsObject (readonly)

Returns the value of attribute constants.



77
78
79
# File 'lib/ffi_gen.rb', line 77

def constants
  @constants
end

#nameObject

Returns the value of attribute name.



76
77
78
# File 'lib/ffi_gen.rb', line 76

def name
  @name
end

Instance Method Details

#java_nameObject



145
146
147
# File 'lib/ffi_gen/java_output.rb', line 145

def java_name
  @java_name ||= @name.to_java_classname
end

#ruby_nameObject



136
137
138
# File 'lib/ffi_gen/ruby_output.rb', line 136

def ruby_name
  @ruby_name ||= @name.to_ruby_downcase
end

#shorten_namesObject



86
87
88
89
90
91
# File 'lib/ffi_gen.rb', line 86

def shorten_names
  return if @constants.size < 2
  names = @constants.map { |constant| constant[:name].parts }
  names.each(&:shift) while names.map(&:first).uniq.size == 1 and @name.parts.map(&:downcase).include? names.first.first.downcase
  names.each(&:pop) while names.map(&:last).uniq.size == 1 and @name.parts.map(&:downcase).include? names.first.last.downcase
end

#write_java(writer) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ffi_gen/java_output.rb', line 121

def write_java(writer)
  shorten_names
  
  @constants.each do |constant|
    constant[:symbol] = ":#{constant[:name].to_ruby_downcase}"
  end
  
  writer.comment do
    writer.write_description @comment
    # TODO constant comments
  end
  
  writer.puts "public enum #{java_name} implements NativeEnum {"
  writer.indent do
    writer.write_array @constants, "," do |constant|
      "#{constant[:name].to_java_constant}(#{constant[:value]})"
    end
    writer.puts ";"
    
    writer.puts "", "private int nativeInt;", "", "private #{java_name}(int nativeInt) {", "    this.nativeInt = nativeInt;", "}", "", "@Override", "public int toNativeInt() {", "    return nativeInt;", "}"
  end
  writer.puts "}", ""
end

#write_ruby(writer) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ffi_gen/ruby_output.rb', line 109

def write_ruby(writer)
  shorten_names
  
  @constants.each do |constant|
    constant[:symbol] = ":#{constant[:name].to_ruby_downcase}"
  end
  
  writer.comment do
    writer.write_description @comment
    writer.puts "", "<em>This entry is only for documentation and no real method. The FFI::Enum can be accessed via #enum_type(:#{ruby_name}).</em>"
    writer.puts "", "=== Options:"
    @constants.each do |constant|
      writer.puts "#{constant[:symbol]} ::"
      writer.write_description constant[:comment], false, "  ", "  "
    end
    writer.puts "", "@method _enum_#{ruby_name}_", "@return [Symbol]", "@scope class"
  end
  
  writer.puts "enum :#{ruby_name}, ["
  writer.indent do
    writer.write_array @constants, "," do |constant|
      "#{constant[:symbol]}, #{constant[:value]}"
    end
  end
  writer.puts "]", ""
end