Class: Magick::Documentation

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

Class Method Summary collapse

Class Method Details

.generate(format: :markdown) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/magick/documentation.rb', line 6

def generate(format: :markdown)
  features = Magick.features.values
  case format.to_sym
  when :markdown
    generate_markdown(features)
  when :html
    generate_html(features)
  when :json
    generate_json(features)
  else
    raise ArgumentError, "Unknown format: #{format}"
  end
end

.generate_html(features = nil) ⇒ Object



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/magick/documentation.rb', line 78

def generate_html(features = nil)
  features ||= Magick.features.values
  html = "    <!DOCTYPE html>\n    <html>\n    <head>\n      <title>Feature Flags Documentation</title>\n      <style>\n        body { font-family: Arial, sans-serif; margin: 20px; }\n        table { border-collapse: collapse; width: 100%; margin: 20px 0; }\n        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }\n        th { background-color: #f2f2f2; }\n        .status-active { color: green; }\n        .status-deprecated { color: orange; }\n        .status-inactive { color: red; }\n      </style>\n    </head>\n    <body>\n      <h1>Feature Flags Documentation</h1>\n      <p>Generated: \#{Time.now}</p>\n      <table>\n        <thead>\n          <tr>\n            <th>Name</th>\n            <th>Type</th>\n            <th>Status</th>\n            <th>Default Value</th>\n            <th>Description</th>\n          </tr>\n        </thead>\n        <tbody>\n  HTML\n\n  features.each do |feature|\n    html << <<~HTML\n      <tr>\n        <td><code>\#{feature.name}</code></td>\n        <td>\#{feature.type.to_s.capitalize}</td>\n        <td class=\"status-\#{feature.status}\">\#{feature.status.to_s.capitalize}</td>\n        <td><code>\#{feature.default_value.inspect}</code></td>\n        <td>\#{feature.description || 'No description'}</td>\n      </tr>\n    HTML\n  end\n\n  html << <<~HTML\n        </tbody>\n      </table>\n    </body>\n    </html>\n  HTML\n\n  html\nend\n"

.generate_json(features = nil) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/magick/documentation.rb', line 133

def generate_json(features = nil)
  features ||= Magick.features.values
  features_data = features.map do |feature|
    # Use to_h to get all feature data including targeting
    feature_hash = feature.to_h
    {
      name: feature_hash[:name],
      display_name: feature_hash[:display_name],
      type: feature_hash[:type].to_s,
      status: feature_hash[:status].to_s,
      default_value: feature_hash[:default_value],
      description: feature_hash[:description],
      targeting: feature_hash[:targeting] || {},
      dependencies: feature.dependencies
    }
  end
  JSON.pretty_generate(features_data)
end

.generate_markdown(features = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
# File 'lib/magick/documentation.rb', line 20

def generate_markdown(features = nil)
  features ||= Magick.features.values
  output = ["# Feature Flags Documentation\n", "Generated: #{Time.now}\n\n"]

  features.each do |feature|
    output << "## #{feature.display_name || feature.name}\n\n"
    output << "**Name:** `#{feature.name}`\n\n"
    output << "**Type:** #{feature.type.to_s.capitalize}\n\n"
    output << "**Status:** #{feature.status.to_s.capitalize}\n\n"
    output << "**Default Value:** `#{feature.default_value.inspect}`\n\n"
    output << "**Description:** #{feature.description || 'No description'}\n\n"

    # Access targeting via instance_variable_get since it's private
    targeting = feature.instance_variable_get(:@targeting) || {}
    if targeting.any?
      output << "### Targeting Rules\n\n"
      targeting.each do |key, value|
        case key.to_sym
        when :user
          user_list = value.is_a?(Array) ? value : [value]
          user_list.each do |user_id|
            output << "- **user_id:** #{user_id}\n"
          end
        when :group
          group_list = value.is_a?(Array) ? value : [value]
          group_list.each do |group|
            output << "- **group:** #{group}\n"
          end
        when :role
          role_list = value.is_a?(Array) ? value : [value]
          role_list.each do |role|
            output << "- **role:** #{role}\n"
          end
        when :percentage_users
          output << "- **percentage_users:** #{value}%\n"
        when :percentage_requests
          output << "- **percentage_requests:** #{value}%\n"
        else
          output << "- **#{key}:** #{value.inspect}\n"
        end
      end
      output << "\n"
    end

    if feature.dependencies.any?
      output << "### Dependencies\n\n"
      feature.dependencies.each do |dep|
        output << "- `#{dep}`\n"
      end
      output << "\n"
    end

    output << "---\n\n"
  end

  output.join
end