Sha256: 5231fbca0db81c2b0eb532d9021a50ce765d155d62101bd606d9528604851d94

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

module CabbageDoc
  class Configuration
    include Singleton

    DEFAULTS = {
      path: 'api/v1',
      title: 'Cabbage Doc',
      scheme: 'https',
      verbose: false,
      dev: false,
      visibility: [VISIBILITY.first],
      cache: Cache.new,
      request: proc { |request| request.perform },
      theme: 'github'
    }.freeze

    OPTIONAL_ATTRIBUTES = %i(welcome path scheme title verbose authentication dev request cache theme visibility).freeze
    REQUIRED_ATTRIBUTES = %i(domain controllers root).freeze
    ATTRIBUTES          = (OPTIONAL_ATTRIBUTES + REQUIRED_ATTRIBUTES).freeze
    CALLABLE_ATTRIBUTES = %i(controllers authentication request).freeze

    attr_accessor *ATTRIBUTES 

    def initialize
      DEFAULTS.each do |attr, value|
        send(:"#{attr}=", value)
      end
    end

    def validate!
      validate_required!
      validate_callable!
      validate_root!
      validate_visibility!
    end

    private

    def validate_required!
      REQUIRED_ATTRIBUTES.each do |attr|
        raise ArgumentError, "#{attr} is required" unless send(attr)
      end
    end

    def validate_callable!
      CALLABLE_ATTRIBUTES.each do |attr|
        if (value = send(attr)) && !value.respond_to?(:call)
          raise ArgumentError, "#{attr} is not callable"
        end
      end
    end

    def validate_root!
      raise ArgumentError, "#{root} directory doesn't exist" unless Dir.exists?(root)
    end

    def validate_visibility!
      self.visibility = Array(visibility)
      self.visibility.each do |v|
        raise ArgumentError, "#{v} invalid visibility" unless VISIBILITY.include?(v)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cabbage_doc-0.0.6 lib/cabbage_doc/configuration.rb
cabbage_doc-0.0.5 lib/cabbage_doc/configuration.rb
cabbage_doc-0.0.4 lib/cabbage_doc/configuration.rb