Class: Scopedog::Exporters::MarkdownExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/scopedog/exporters/markdown_exporter.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ MarkdownExporter

Returns a new instance of MarkdownExporter.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :dir (String)


5
6
7
# File 'lib/scopedog/exporters/markdown_exporter.rb', line 5

def initialize(opts = {})
  @dir = opts[:dir]
end

Instance Method Details

#export(record_class, destination = {}) ⇒ Object

Parameters:



13
14
15
16
17
18
19
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
# File 'lib/scopedog/exporters/markdown_exporter.rb', line 13

def export(record_class, destination = {})
  name = destination[:name] || record_class.name.to_s.underscore
  name = "#{name}.md" unless File.extname(name) == '.md'

  dir = @dir
  dir = File.join(dir, destination[:prefix]) if destination[:prefix]

  if name.include? '/'
    name, prefix = name.split('/').yield_self { |p| [p[-1], p[0..-2]] }
    dir = File.join(dir, prefix)
  end

  FileUtils.mkdir_p(dir) unless File.exists?(dir)

  File.open(File.join(dir, name), 'w') do |f|
    f.puts <<~MARKDOWN
      # #{record_class.name}
      #{record_class.docstring}

      ```sql
      -- default scope
      #{record_class.default_sql}
      ```
    MARKDOWN

    unless record_class.scopes.empty?
      f.puts
      f.puts "## Scopes"

      record_class.scopes.each do |s|
        f.puts
        f.puts <<~MARKDOWN
          ### `#{s.name}`
          #{s.docstring}

          ```sql
          #{s.sql}
          ```
        MARKDOWN
      end
    end
  end
end