Sha256: 9db9f912c846a7a6bbd326e0191215c9fcd2c73416c14f8de72ba1bd5bd915c1

Contents?: true

Size: 1.21 KB

Versions: 7

Compression:

Stored size: 1.21 KB

Contents

require 'yaml'

module CabbageDoc
  class Collection
    include Singleton
    include Enumerable

    FILENAME = "controllers.yml".freeze

    def initialize
      @_controllers = []
    end

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

    def find_action(method, path)
      action = nil

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

      action
    end

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

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

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

      controllers = controller.eval(text)

      @_controllers.concat(controllers)

      controllers.any?
    end

    def clear!
      @_controllers = []
    end

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

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

    private

    def filename
      @_filename ||= Path.join(Configuration.instance.root, FILENAME)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
cabbage_doc-0.0.7 lib/cabbage_doc/collection.rb
cabbage_doc-0.0.6 lib/cabbage_doc/collection.rb
cabbage_doc-0.0.5 lib/cabbage_doc/collection.rb
cabbage_doc-0.0.4 lib/cabbage_doc/collection.rb
cabbage_doc-0.0.3 lib/cabbage_doc/collection.rb
cabbage_doc-0.0.2 lib/cabbage_doc/collection.rb
cabbage_doc-0.0.1 lib/cabbage_doc/collection.rb