Sha256: 3da5374489fdcb02e26744da5b8ea2e82b3b53716c98e4205d9c54afa68dd0c9

Contents?: true

Size: 839 Bytes

Versions: 1

Compression:

Stored size: 839 Bytes

Contents

# frozen_string_literal: true

require_relative "spaced/version"

module Spaced
  def self.included(base)
    base.extend ClassMethods
  end

  class Base
    private

    attr_reader :parent
  end

  module ClassMethods
    def namespace(name, &)
      class_name = name.to_s.split("_").collect(&:capitalize).join
      klass = eval <<-RUBY, binding, __FILE__, __LINE__ + 1 # rubocop:disable Security/Eval
        #{self}::#{class_name} = Class.new(Base, &)  # Parent::Namespace = Class.new(Base, &)
      RUBY

      inst_name = :"@#{name}"
      define_method name do
        if instance_variable_defined?(inst_name)
          instance_variable_get inst_name
        else
          cls = klass.new
          cls.instance_variable_set :@parent, self
          instance_variable_set inst_name, cls
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spaced-0.1.1 lib/spaced.rb