Sha256: 4447048df3cdc45b21ac7a16dffbf4d770779ee25f642d4597cf57da2eb7a61f

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

require 'concurrent'
require 'delegate'
require 'hanami/application'
require 'hanami/utils/class'
require 'hanami/utils/string'

module Hanami
  class Configuration
    class App < SimpleDelegator
      attr_reader :path_prefix

      def initialize(app, path_prefix)
        super(app)
        @path_prefix = path_prefix
      end
    end

    def initialize(&blk)
      @settings = Concurrent::Map.new
      instance_eval(&blk)
    end

    def mount(app, options)
      mounted[app] = App.new(app, options.fetch(:at))
    end

    def model(&blk)
      if block_given?
        settings.put_if_absent(:model, blk)
      else
        settings.fetch(:model)
      end
    end

    def mailer(&blk)
      settings.put_if_absent(:mailer, blk)
    end

    def mounted
      settings.fetch_or_store(:mounted, {})
    end

    def apps
      mounted.each_pair do |klass, app|
        yield(app) if klass.ancestors.include?(Hanami::Application)
      end
    end

    def logger(options = nil)
      if options.nil?
        settings.fetch(:logger, nil)
      else
        settings[:logger] = options
      end
    end

    def environment(name)
      yield if ENV['HANAMI_ENV'] == name.to_s
    end

    private

    attr_reader :settings
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hanami-1.0.0.beta2 lib/hanami/configuration.rb
hanami-1.0.0.beta1 lib/hanami/configuration.rb