Sha256: 934e28162fed86e99de90750e4c18a150b045e7d72d8bf43d21cf47b5730b185

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require "active_support/core_ext/class/attribute"
require "active_support/concern"
require "new_class/version" unless defined?(NewClass::VERSION)

module NewClass

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

  module ClassMethods
    def new_class(variables = {}, name = nil)
      Class.new(self).tap do |klass|
        klass.send :include, Concerns, MethodMissing
        klass.extend MethodMissing
        klass._name = name || self.name
        klass._variables = variables.inject({}){|h, (k, v)| h.merge k.to_sym => v}
        klass.defined if klass.respond_to?(:defined)
      end
    end

    module Concerns
      extend ActiveSupport::Concern
      included do
        class_attribute :_name, :_variables
      end
      module ClassMethods
        def name
          _name || super
        end
        alias :to_s :name
      end

      def _variables
        self.class._variables
      end
    end

    module MethodMissing
      def method_missing(method, *args)
        _variables.include?(key = method.to_sym) ? _variables[key] : super
      end
      def respond_to?(symbol, include_private = false)
        _variables.include?(symbol.to_sym) || super
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
new_class-0.1.2 lib/new_class.rb