Sha256: fee82371c3aa99d6e118bcb0a456af72ad1b03c3861e40359539ee1a050e0916

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

module Rid
  # Holds Application config
  #
  class App
    attr_reader :db_url, :db
    attr_accessor :root

    # Initialize app with an options object,
    # which behaves like an open struct.
    #
    # Options are:
    #   root:     specify app root directory
    #   database: set database name
    #
    def initialize(options = OpenStruct.new)
      @root = options.root
      @root ||= Dir.pwd
      @root = Pathname.new(@root)

      @db_url = options.database
      @db_url ||= read_db_url
    end

    # Returns an instance of Rid::Database,
    # configured with db_name.
    #
    def db
      return unless @db_url
      @db ||= Rid::Database.new(@db_url)
    end

    # List application documents.
    # Each entry is a filename.
    #
    def documents
      @documents ||= Dir[root.join("*")].
        select { |f| File.directory? f }.
        map { |f| Rid::Document.new :path => f }
    end

    private

    def read_db_url
      filename = root.join('_database')
      File.read(filename).strip
    rescue Errno::ENOENT
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
rid-core-1.1.0 lib/rid/app.rb
rid-core-1.0.3 lib/rid/app.rb
rid-1.0.3 lib/rid/app.rb
rid-1.0.2 lib/rid/app.rb