Sha256: 9aa64ab31d7a7063d48de3cfc8a97a35336f8cbe0a798c8b5438fd6b847230ab

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module Brief
  class Repository
    attr_reader :briefcase, :options

    include Enumerable

    # should compare vs yield
    def each(*args, &block)
      documents.send(:each, *args, &block)
    end

    def initialize(briefcase, options={})
      @briefcase = briefcase
      @options = options

      load_documents
    end

    def documents
      return @documents if @documents
      load_documents
    end

    def where(*args)
      Brief::DocumentMapper::Query.new(self).send(:where, *args)
    end

    def order_by(*args)
      Brief::DocumentMapper::Query.new(self).send(:order_by, *args)
    end

    def root
      briefcase.root
    end

    def load_documents
      @documents = document_paths.map do |path|
        Brief::Document.new(path)
      end
    end

    def document_paths
      Dir[root.join("**/*.md").to_s]
    end

    def self.define_document_finder_methods
      # Create a finder method on the repository
      # which lets us find instances of models by their class name
      Brief::Model.table.keys.each do |type|
        define_method(type.to_s.pluralize) do
          Brief::Model.for_type(type).models.to_a
        end
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
brief-1.1.0 lib/brief/repository.rb
brief-1.0.0 lib/brief/repository.rb