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.



32
33
34
35
36
37
# File 'lib/ffi_gen.rb', line 32

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

Instance Attribute Details

#constantsObject (readonly)

Returns the value of attribute constants.



30
31
32
# File 'lib/ffi_gen.rb', line 30

def constants
  @constants
end

Instance Method Details

#referenceObject



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

def reference
  ":#{ruby_name}"
end

#ruby_nameObject



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

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

#type_name(short) ⇒ Object



90
91
92
# File 'lib/ffi_gen.rb', line 90

def type_name(short)
  short ? @name : "Symbol from _enum_#{ruby_name}_"
end

#write(writer) ⇒ Object



39
40
41
42
43
44
45
46
47
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
# File 'lib/ffi_gen.rb', line 39

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