Class: CabbageDoc::Collection

Inherits:
Object
  • Object
show all
Includes:
Singleton, Enumerable
Defined in:
lib/cabbage_doc/collection.rb

Constant Summary collapse

FILENAME =
"controllers.yml".freeze

Instance Method Summary collapse

Methods included from Singleton

included

Constructor Details

#initializeCollection

Returns a new instance of Collection.



10
11
12
# File 'lib/cabbage_doc/collection.rb', line 10

def initialize
  @_controllers = []
end

Instance Method Details

#<<(controller) ⇒ Object



14
15
16
# File 'lib/cabbage_doc/collection.rb', line 14

def <<(controller)
  @_controllers << controller
end

#clear!(tag = nil) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/cabbage_doc/collection.rb', line 49

def clear!(tag = nil)
  if tag && config.tags.size > 1
    @_controllers.reject! { |controller| tag == controller.tag }
  else
    @_controllers = []
  end
end

#eachObject



29
30
31
32
33
# File 'lib/cabbage_doc/collection.rb', line 29

def each
  @_controllers.each do |controller|
    yield controller
  end
end

#find_action(method, path) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/cabbage_doc/collection.rb', line 18

def find_action(method, path)
  action = nil

  @_controllers.each do |controller|
    action = controller.find_action(method, path)
    break if action
  end

  action
end

#load!Object



57
58
59
# File 'lib/cabbage_doc/collection.rb', line 57

def load!
  @_controllers = YAML.load(File.read(filename)) rescue [] unless @_controllers.any?
end

#parse!(filename, tag = TAG) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cabbage_doc/collection.rb', line 35

def parse!(filename, tag = TAG)
  text = File.read(filename) rescue nil
  return false unless text

  controller = Controller.parse(text, tag)
  return false unless controller

  controllers = controller.eval(text, tag)

  @_controllers.concat(controllers)

  controllers.any?
end

#save!Object



61
62
63
64
# File 'lib/cabbage_doc/collection.rb', line 61

def save!
  sort!
  open(filename, 'w') { |f| f.write(YAML.dump(@_controllers)) } rescue nil
end