Sha256: 0e8d217f63d89e10b20c53f275252bcb4db6caf09d40d7496f148a14db9e1ec9

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

module ViteRb
  class Configuration
    attr_accessor :config_path
    attr_accessor :config_file
    attr_accessor :babel_config_file
    attr_accessor :postcss_config_file
    attr_accessor :build_dir
    attr_accessor :mount_path
    attr_accessor :manifest_file
    attr_accessor :output_dir
    attr_accessor :port, :hostname

    def initialize
      yield(self) if block_given?
    end


    # Allows dynamic definition of getters and setters
    # The setter must be the value used first.
    # @example
    #   ViteRb.config.fake_attr # => Raises an error
    #   ViteRb.config.fake_attr = "stuff"
    #   ViteRb.config.fake_attr # => "stuff"
    def method_missing(method_name, *args, &block)
      # Check if the method missing an "attr=" method
      raise unless method_name.to_s.end_with?("=")

      setter = method_name
      getter = method_name.to_s.slice(0...-1).to_sym
      instance_var = "@#{getter}".to_sym

      # attr_writer
      define_singleton_method(setter) do |new_val|
        instance_variable_set(instance_var, new_val)
      end

      # attr_reader
      define_singleton_method(getter) { instance_variable_get(instance_var) }

      # Ignores all arguments but the first one
      value = args[0]

      # Actually sets the value on the instance variable
      send(setter, value)
    rescue MethodMissing
      # Raise error as normal, nothing to see here
      super(method_name, *args, &block)
    end

    # rubocop:enable Style/MethodMissingSuper Metrics/MethodLength

    def respond_to_missing?(method_name, include_private = false)
      method_name.to_s.end_with?("=") || super
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vite_rb-0.0.1.alpha1 lib/vite_rb/configuration.rb