Class: TomDoc::Generator

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, scopes = {}) ⇒ Generator

Creates a Generator.

options - Optional Symbol-keyed Hash:

:validate - Whether or not to validate TomDoc.

scopes - Optional Symbol-keyed Hash.

Returns an instance of TomDoc::Generator



13
14
15
16
17
18
19
20
21
# File 'lib/tomdoc/generator.rb', line 13

def initialize(options = {}, scopes = {})
  @options = {
    :validate => true
  }
  @options.update(options)

  @scopes = {}
  @buffer = ''
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/tomdoc/generator.rb', line 3

def options
  @options
end

#scopesObject (readonly)

Returns the value of attribute scopes.



3
4
5
# File 'lib/tomdoc/generator.rb', line 3

def scopes
  @scopes
end

Class Method Details

.generate(text_or_sexp) ⇒ Object



23
24
25
# File 'lib/tomdoc/generator.rb', line 23

def self.generate(text_or_sexp)
  new.generate(text_or_sexp)
end

Instance Method Details

#constant?(const) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
# File 'lib/tomdoc/generator.rb', line 104

def constant?(const)
  const = const.split('::').first if const.include?('::')
  constant_names.include?(const.intern) || Object.const_defined?(const)
end

#constant_namesObject



109
110
111
112
# File 'lib/tomdoc/generator.rb', line 109

def constant_names
  name = @scopes.name if @scopes.respond_to?(:name)
  [ :Boolean, :Test, name ].compact + @scopes.keys
end

#generate(text_or_sexp) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/tomdoc/generator.rb', line 27

def generate(text_or_sexp)
  if text_or_sexp.is_a?(String)
    sexp = SourceParser.parse(text_or_sexp)
  else
    sexp = text_or_sexp
  end

  process(sexp)
end

#matches_pattern?(prefix, name) ⇒ Boolean

Returns:

  • (Boolean)


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

def matches_pattern?(prefix, name)
  if pattern = options[:pattern]
    # "-n hey" vs "-n /he.+y/"
    if pattern =~ /^\/.+\/$/
      pattern = pattern.sub(/^\//, '').sub(/\/$/, '')
      regexp = Regexp.new(pattern)
    else
      regexp = Regexp.new(Regexp.escape(pattern))
    end

    regexp =~ name.to_s || regexp =~ prefix.to_s
  else
    true
  end
end

#process(scopes = {}, prefix = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tomdoc/generator.rb', line 37

def process(scopes = {}, prefix = nil)
  old_scopes = @scopes
  @scopes = scopes
  scopes.each do |name, scope|
    write_scope(scope, prefix)
    process(scope, "#{name}::")
  end

  @buffer
ensure
  @scopes = old_scopes || {}
end

#pygments(text, *args) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tomdoc/generator.rb', line 92

def pygments(text, *args)
  out = ''

  Open3.popen3("pygmentize", *args) do |stdin, stdout, stderr|
    stdin.puts text
    stdin.close
    out = stdout.read.chomp
  end

  out
end

#valid?(object, prefix) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/tomdoc/generator.rb', line 114

def valid?(object, prefix)
  matches_pattern?(prefix, object.name) && valid_tomdoc?(object.tomdoc)
end

#valid_tomdoc?(comment) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/tomdoc/generator.rb', line 134

def valid_tomdoc?(comment)
  options[:validate] ? TomDoc.valid?(comment) : true
end

#write(*things) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/tomdoc/generator.rb', line 84

def write(*things)
  things.each do |thing|
    @buffer << "#{thing}\n"
  end

  nil
end

#write_class_methods(scope, prefix = nil) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/tomdoc/generator.rb', line 63

def write_class_methods(scope, prefix = nil)
  prefix ="#{prefix}#{scope.name}."

  scope.class_methods.map do |method|
    next if !valid?(method, prefix)
    write_method(method, prefix)
  end.compact
end

#write_instance_methods(scope, prefix = nil) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/tomdoc/generator.rb', line 72

def write_instance_methods(scope, prefix = nil)
  prefix = "#{prefix}#{scope.name}#"

  scope.instance_methods.map do |method|
    next if !valid?(method, prefix)
    write_method(method, prefix)
  end.compact
end

#write_method(method, prefix = '') ⇒ Object



81
82
# File 'lib/tomdoc/generator.rb', line 81

def write_method(method, prefix = '')
end

#write_scope(scope, prefix) ⇒ Object



50
51
52
53
54
55
# File 'lib/tomdoc/generator.rb', line 50

def write_scope(scope, prefix)
  write_scope_header(scope, prefix)
  write_class_methods(scope, prefix)
  write_instance_methods(scope, prefix)
  write_scope_footer(scope, prefix)
end


60
61
# File 'lib/tomdoc/generator.rb', line 60

def write_scope_footer(scope, prefix)
end

#write_scope_header(scope, prefix) ⇒ Object



57
58
# File 'lib/tomdoc/generator.rb', line 57

def write_scope_header(scope, prefix)
end