Module: TALibFFI::Doc

Defined in:
lib/ta_lib_ffi/doc.rb

Overview

Generates documentation for a TA-Lib function

This module provides methods to generate documentation for TA-Lib functions. It includes methods to collect input, optional input, and output information, and to generate documentation for each function.

Class Method Summary collapse

Class Method Details

.generateObject



38
39
40
41
42
43
44
# File 'lib/ta_lib_ffi/doc.rb', line 38

def generate
  docs = []
  TALibFFI.function_info_map.each_value do |h|
    docs << generate_function_documentation(h[:info], h[:inputs], h[:opt_inputs], h[:outputs])
  end
  docs.join("\n")
end

.generate_error_documentationObject



134
135
136
# File 'lib/ta_lib_ffi/doc.rb', line 134

def generate_error_documentation
  "    # @raise [TALibError]  If there is an error in function execution\n"
end

.generate_function_description(func_info, inputs, opt_inputs) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/AbcSize



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ta_lib_ffi/doc.rb', line 56

def generate_function_description(func_info, inputs, opt_inputs) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
  args = []
  inputs.map do |input|
    args << TALibFFI.normalize_parameter_name(input["paramName"].to_s)
  end

  opt_inputs.map do |opt_input|
    param_name = TALibFFI.normalize_parameter_name(opt_input["paramName"].to_s)
    args << "#{param_name}: #{opt_input["defaultValue"]}"
  end

  [
    "    # @!method #{func_info["name"].to_s.downcase}(#{args.join(", ")})",
    "    # #{func_info["hint"].to_s.strip}",
    "    #"
  ]
end

.generate_function_documentation(func_info, inputs, opt_inputs, outputs) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/ta_lib_ffi/doc.rb', line 46

def generate_function_documentation(func_info, inputs, opt_inputs, outputs)
  [
    generate_function_description(func_info, inputs, opt_inputs),
    generate_input_documentation(inputs),
    generate_optional_input_documentation(opt_inputs),
    generate_output_documentation(outputs),
    generate_error_documentation
  ].flatten.compact.join("\n")
end

.generate_input_documentation(inputs) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/AbcSize



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ta_lib_ffi/doc.rb', line 74

def generate_input_documentation(inputs) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
  inputs.map do |input| # rubocop:disable Metrics/BlockLength
    param_name = TALibFFI.normalize_parameter_name(input["paramName"].to_s)
    flags = TALibFFI.extract_flags(input["flags"], :TA_InputFlags)

    type = case input["type"]
           when TALibFFI::TA_PARAM_TYPE[:TA_Input_Price]
             array_types = Array.new(flags.length, "Array<Float>")
             "Array(#{array_types.join(", ")})"
           when TALibFFI::TA_PARAM_TYPE[:TA_Input_Real]
             "Array<Float>"
           when TALibFFI::TA_PARAM_TYPE[:TA_Input_Integer]
             "Array<Integer>"
           end

    description = if input["type"] == TALibFFI::TA_PARAM_TYPE[:TA_Input_Price]
                    arrays = flags.map do |flag|
                      {
                        TA_IN_PRICE_OPEN: "open",
                        TA_IN_PRICE_HIGH: "high",
                        TA_IN_PRICE_LOW: "low",
                        TA_IN_PRICE_CLOSE: "close",
                        TA_IN_PRICE_VOLUME: "volume",
                        TA_IN_PRICE_OPENINTEREST: "open interest",
                        TA_IN_PRICE_TIMESTAMP: "timestamp"
                      }[flag]
                    end
                    "Required price arrays: #{arrays.join(", ")}"
                  else
                    "Input values"
                  end

    "    # @param #{param_name} [#{type}]  #{description}"
  end
end

.generate_optional_input_documentation(opt_inputs) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/ta_lib_ffi/doc.rb', line 110

def generate_optional_input_documentation(opt_inputs)
  opt_inputs.map do |opt_input|
    param_name = TALibFFI.normalize_parameter_name(opt_input["paramName"].to_s)
    type = opt_input["type"] == TALibFFI::TA_PARAM_TYPE[:TA_OptInput_RealRange] ? "Float" : "Integer"
    "    # @param #{param_name} [#{type}]  #{opt_input["hint"]} (default: #{opt_input["defaultValue"]})"
  end
end

.generate_output_documentation(outputs) ⇒ Object

rubocop:disable Metrics/MethodLength



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ta_lib_ffi/doc.rb', line 118

def generate_output_documentation(outputs) # rubocop:disable Metrics/MethodLength
  if outputs.length == 1
    type = outputs.first["type"] == TALibFFI::TA_PARAM_TYPE[:TA_Output_Real] ? "Float" : "Integer"
    ["    # @return [Array<#{type}>]"]
  else
    [
      "    # @return [Hash]  Hash containing the following arrays:",
      *outputs.map do |output|
        param_name = TALibFFI.normalize_parameter_name(output["paramName"].to_s)
        type = output["type"] == TALibFFI::TA_PARAM_TYPE[:TA_Output_Real] ? "Float" : "Integer"
        "    # @option result [Array<#{type}>] :#{param_name}  Output values"
      end
    ]
  end
end

.insertObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ta_lib_ffi/doc.rb', line 12

def insert
  puts "Inserting documentation"
  file_path = File.expand_path("../ta_lib_ffi.rb", __dir__)
  content = File.read(file_path)

  new_content = content.sub(
    /### GENERATED DOCUMENTATION START ###.*### GENERATED DOCUMENTATION END ###/m,
    "### GENERATED DOCUMENTATION START ###\n#{generate}\n    ### GENERATED DOCUMENTATION END ###"
  )

  File.write(file_path, new_content)
end

.removeObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ta_lib_ffi/doc.rb', line 25

def remove
  puts "Removing documentation"
  file_path = File.expand_path("../ta_lib_ffi.rb", __dir__)
  content = File.read(file_path)

  new_content = content.sub(
    /### GENERATED DOCUMENTATION START ###.*### GENERATED DOCUMENTATION END ###/m,
    "### GENERATED DOCUMENTATION START ###\n    ### GENERATED DOCUMENTATION END ###"
  )

  File.write(file_path, new_content)
end