Class: CabbageDoc::Controller

Inherits:
Object
  • Object
show all
Includes:
Cloneable, Parser
Defined in:
lib/cabbage_doc/controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cloneable

#clone

Methods included from Parser

included, #parse_option, #parse_templates

Constructor Details

#initialize(tag = TAG) ⇒ Controller

Returns a new instance of Controller.



8
9
10
11
12
# File 'lib/cabbage_doc/controller.rb', line 8

def initialize(tag = TAG)
  @actions = []
  @visibility = VISIBILITY.first
  @tag = tag
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



6
7
8
# File 'lib/cabbage_doc/controller.rb', line 6

def actions
  @actions
end

#klassObject (readonly)

Returns the value of attribute klass.



6
7
8
# File 'lib/cabbage_doc/controller.rb', line 6

def klass
  @klass
end

#labelObject (readonly)

Returns the value of attribute label.



6
7
8
# File 'lib/cabbage_doc/controller.rb', line 6

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/cabbage_doc/controller.rb', line 6

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/cabbage_doc/controller.rb', line 6

def path
  @path
end

#tagObject (readonly)

Returns the value of attribute tag.



6
7
8
# File 'lib/cabbage_doc/controller.rb', line 6

def tag
  @tag
end

#visibilityObject (readonly)

Returns the value of attribute visibility.



6
7
8
# File 'lib/cabbage_doc/controller.rb', line 6

def visibility
  @visibility
end

Instance Method Details

#eval(text, tag) ⇒ Object



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
# 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) ⇒ Object



29
30
31
32
33
# 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) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# 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?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/cabbage_doc/controller.rb', line 25

def valid?
  @name && (actions? || template?)
end