class CabbageDoc::Controller

Attributes

actions[R]
klass[R]
label[R]
name[R]
path[R]
tag[R]
visibility[R]

Public Class Methods

new(tag = TAG) click to toggle source
# File lib/cabbage_doc/controller.rb, line 8
def initialize(tag = TAG)
  @actions = []
  @visibility = VISIBILITY.first
  @tag = tag
end

Public Instance Methods

eval(text, tag) click to toggle source
# File lib/cabbage_doc/controller.rb, line 35
def eval(text, tag)
  return [self] unless template?

  templates = {}

  templates.merge!(parse_templates(path, tag))
  templates.merge!(parse_templates(label, tag))

  yield_actions(text) do |action|
    templates.merge!(parse_templates(action, tag))
  end

  return [] unless templates.any?

  count = templates.values.first.count
  return [] if templates.values.any? { |v| v.count != count }

  count.times.map do |i|
    template_text = text.dup

    templates.each do |text, values|
      template_text.gsub!(text, values.shift.to_s)
    end

    self.class.parse(template_text, tag)
  end.compact
end
find_action(method, path) click to toggle source
# File lib/cabbage_doc/controller.rb, line 29
def find_action(method, path)
  @actions.detect do |action| 
    action.method == method && action.path == path
  end
end
parse(text, tag = TAG) click to toggle source
# File lib/cabbage_doc/controller.rb, line 14
def parse(text, tag = TAG)
  @label, @path, @klass, @visibility, @tag = parse_label_path_class_visibility_and_tag(text, tag)
  return false unless @label && @klass

  @name = compose_name(klass)

  @actions = parse_actions(text) unless template?

  valid?
end
valid?() click to toggle source
# File lib/cabbage_doc/controller.rb, line 25
def valid?
  @name && (actions? || template?)
end

Private Instance Methods

actions?() click to toggle source
# File lib/cabbage_doc/controller.rb, line 158
def actions?
  @actions.any?
end
compose_label(metadata, klass) click to toggle source
# File lib/cabbage_doc/controller.rb, line 65
def compose_label(metadata, klass)
  metadata[:label] || klass.sub(/Controller$/, '')
end
compose_name(klass) click to toggle source
# File lib/cabbage_doc/controller.rb, line 73
def compose_name(klass)
  compose_label({}, klass).downcase
end
compose_path(metadata, klass) click to toggle source
# File lib/cabbage_doc/controller.rb, line 69
def compose_path(metadata, klass)
  Path.join('/', Configuration.instance.path, metadata[:path] || compose_name(klass))
end
compose_tag(metadata, tag = TAG) click to toggle source
# File lib/cabbage_doc/controller.rb, line 81
def compose_tag(metadata, tag = TAG)
  metadata[:tag]&.to_sym || tag
end
compose_visbility(metadata) click to toggle source
# File lib/cabbage_doc/controller.rb, line 77
def compose_visbility(metadata)
  metadata[:visibility] || VISIBILITY.first
end
parse_action(text) click to toggle source
# File lib/cabbage_doc/controller.rb, line 110
def parse_action(text)
  new_text = text.strip

  if new_text.scan(/#\s*(#{VISIBILITY_REGEXP}):\s*/).size > 1
    new_text.sub(/#\s*(#{VISIBILITY_REGEXP}):.*?#\s*(#{VISIBILITY_REGEXP}):/m, '# \2:').strip
  else
    new_text
  end
end
parse_actions(text) click to toggle source
# File lib/cabbage_doc/controller.rb, line 100
def parse_actions(text)
  actions = []

  yield_actions(text) do |action|
    actions << Action.parse(parse_action(action))
  end

  actions.compact
end
parse_class(text) click to toggle source
# File lib/cabbage_doc/controller.rb, line 120
def parse_class(text)
  m = text.match(/class\s+(.*?)\s+?#\s+?#{MARKER}$/)
  m[1].strip.split('<').first.strip if m
end
parse_label_path_class_visibility_and_tag(text, tag = TAG) click to toggle source
# File lib/cabbage_doc/controller.rb, line 85
def parse_label_path_class_visibility_and_tag(text, tag = TAG)
  klass = parse_class(text)
  return unless klass

  metadata = parse_metadata(text)

  [
    compose_label(metadata, klass),
    compose_path(metadata, klass),
    klass,
    compose_visbility(metadata),
    compose_tag(metadata, tag)
  ]
end
parse_metadata(text) click to toggle source
# File lib/cabbage_doc/controller.rb, line 125
def parse_metadata(text)
  m = text.match(/(#\s*(#{VISIBILITY_REGEXP}):\s*.*?class\s+.*?\s*#\s*#{MARKER})/m)
  return {} unless m

  metadata = m[1].strip

  {}.tap do |hash|
    m = metadata.match(/#\s*(#{VISIBILITY_REGEXP}):(.*?)$/)
    if m
      hash[:visibility] = parse_visibility(m[1])
      hash[:label] = m[2].strip
    end

    m = metadata.match(/#\s*PATH:\s*\/(.*?)$/)
    hash[:path] = m[1].strip if m
  end
end
parse_visibility(text) click to toggle source
# File lib/cabbage_doc/controller.rb, line 143
def parse_visibility(text)
  visibility = text.to_s.strip.downcase
  if visibility.size > 0
    visibility.to_sym
  else
    VISIBILITY.first
  end
end
template?() click to toggle source
# File lib/cabbage_doc/controller.rb, line 162
def template?
  @path =~ /\/\{.*?\}/ || @label =~ /\{.*?\}/
end
yield_actions(text) { |str| ... } click to toggle source
# File lib/cabbage_doc/controller.rb, line 152
def yield_actions(text)
  text.scan(/(#\s*(#{VISIBILITY_REGEXP}):\s*.*?(#{Action::METHODS_REGEXP}):.*?def\s+.*?\s*#\s*#{MARKER})/m) do
    yield $1
  end 
end