Sha256: 040d10bef633a3a5bf39d8e1b2806af054e611057d62ee223c4e02edb953b14b

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require 'forwardable'
require 'singleton'

require 'mwc/project'
require 'mwc/mruby'
require 'mwc'

module Mwc
  # The compile preferences
  class Config
    class << self
      extend Forwardable

      delegate %i[
        exist?
        mruby
      ] => :instance
    end

    include Singleton

    attr_reader :project, :mruby

    # :nodoc:
    def initialize
      @path = Mwc.root.join('.mwcrc')
      @project = Project.new
      @mruby = MRuby.new

      load_config if exist?
    end

    # TODO: Move to DSL module
    # Set name
    #
    # @param name [String|NilClass] the name
    #
    # @since 0.1.0
    # @api private
    def name(name = nil)
      return @name if name.nil?

      @name = name.to_s
    end

    # Check config file exists
    #
    # @return [TrueClass,FalseClass] exist or not
    #
    # @since 0.1.0
    # @api private
    def exist?
      @path.exist?
    end

    # Reload config
    #
    # @since 0.1.0
    # @api private
    def reload!
      # TODO: Update path when root changed
      @path = Mwc.root.join('.mwcrc')
      return unless exist?

      load_config
    end

    private

    # Laod .mwcrc config
    #
    # @since 0.1.0
    # @api private
    def load_config
      # TODO: Improve config DSL
      instance_eval(@path.read)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mwc-0.2.0 lib/mwc/config.rb