Class: FFIGen::Enum

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generator, name, comment) ⇒ Enum

Returns a new instance of Enum.



41
42
43
44
45
46
# File 'lib/ffi_gen.rb', line 41

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

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



39
40
41
# File 'lib/ffi_gen.rb', line 39

def comment
  @comment
end

#constantsObject (readonly)

Returns the value of attribute constants.



39
40
41
# File 'lib/ffi_gen.rb', line 39

def constants
  @constants
end

Instance Method Details

#ruby_nameObject



95
96
97
# File 'lib/ffi_gen.rb', line 95

def ruby_name
  @ruby_name ||= @generator.to_ruby_lowercase @name
end

#write(writer) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ffi_gen.rb', line 48

def write(writer)
  prefix_length = 0
  suffix_length = 0
  
  unless @constants.size < 2
    search_pattern = @constants.all? { |constant| constant[:name].include? "_" } ? /(?<=_)/ : /[A-Z]/
    first_name = @constants.first[:name]
    
    loop do
      position = first_name.index(search_pattern, prefix_length + 1) or break
      prefix = first_name[0...position]
      break if not @constants.all? { |constant| constant[:name].start_with? prefix }
      prefix_length = position
    end
    
    loop do
      position = first_name.rindex(search_pattern, first_name.size - suffix_length - 1) or break
      prefix = first_name[position..-1]
      break if not @constants.all? { |constant| constant[:name].end_with? prefix }
      suffix_length = first_name.size - position
    end
  end
  
  @constants.each do |constant|
    constant[:symbol] = ":#{@generator.to_ruby_lowercase constant[:name][prefix_length..(-1 - suffix_length)]}"
  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] ? ", #{constant[:value]}" : ''}"
    end
  end
  writer.puts "]", ""
end