Sha256: dcd8d6abcd9c177bc5463c8f571f9a5448f8da985f1aa122a43d4d77ccb58238

Contents?: true

Size: 2 KB

Versions: 11

Compression:

Stored size: 2 KB

Contents

# <%= app_name.camelize %>
module <%= app_name.camelize %>
  class << self
    # Configure
    #
    # @param _block [Proc] the configuration options
    # @raise [NoMethodError] when new options are inside configuration block
    # @return [<%= app_name.camelize %>::Configuration] the configuration
    def configure(&_block)
      yield @config ||= <%= app_name.camelize %>::Configuration.new
    rescue NoMethodError => exc
      add_new_attribute_from_exception exc
    end

    # Configuration object
    #
    # @return [<%= app_name.camelize %>::Configuration] the configuration object
    def config
      @config ||= <%= app_name.camelize %>::Configuration.new
    end

    # Add new attribute in configuration block
    #
    # @param exc [NoMethodError] the exception
    # @return [void]
    def add_new_attribute_from_exception(exc)
      add_new_attribute(exc, exc.name.to_s.gsub(/=$/, ''))
    end

    # Implementation for adding new attribute in configuration block
    #
    # @param exc [NoMethodError] the exception
    # @param method [String] the writter method name
    # @return [void]
    def add_new_attribute(exc, method)
      @config.extend Virtus.model
      @config.attribute method.to_sym, String, default: exc.args.first,
                                               lazy: true
      @config.send(exc.name, exc.args.first)
    end
    private :add_new_attribute
  end

  # Configuration
  class Configuration
    include Virtus.model

    attribute :active,   Boolean,                default: true
    attribute :log_file, String
    attribute :options,  Hash[Symbol => String], default: {}

    # Set custom default values
    #
    # @param args [Array] the attributes
    # @return [void]
    def initialize(*args)
      super
      # Bug - Update some default values
      @log_file = <%= app_name.camelize %>.env + '.log'
    end

    # Configuration
    #
    # @return [<%= app_name.camelize %>::Configuration] the configuration
    def config
      <%= app_name.camelize %>.config
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rgentpl-2.0.0 src/lib/%app_name%/core/configuration.rb.tt
rgentpl-1.2.6 src/lib/%app_name%/core/configuration.rb.tt
rgentpl-1.2.5 src/lib/%app_name%/core/configuration.rb.tt
rgentpl-1.2.4.3 src/lib/%app_name%/core/configuration.rb.tt
rgentpl-1.2.4.2 src/lib/%app_name%/core/configuration.rb.tt
rgentpl-1.2.4.1 src/lib/%app_name%/core/configuration.rb.tt
rgentpl-1.2.4 src/lib/%app_name%/core/configuration.rb.tt
rgentpl-1.2.3 src/lib/%app_name%/core/configuration.rb.tt
rgentpl-1.2.2 src/lib/%app_name%/core/configuration.rb.tt
rgentpl-1.2.1 src/lib/%app_name%/core/configuration.rb.tt
rgentpl-1.2.0 src/lib/%app_name%/core/configuration.rb.tt