Sha256: 015360747fe1d510c277e040eb095b20b77e4f6b6c8b43bd818c1c77d2837e3c

Contents?: true

Size: 1.79 KB

Versions: 28

Compression:

Stored size: 1.79 KB

Contents

class Class

  # get singleton methods to target class without super class methods
  def class_methods
    self.methods - Object.methods
  end

  # bind a singleton method to a class object
  def create_class_method(method,&block)
    self.class_eval do
      define_singleton_method method do |*args|
        block.call *args
      end
    end
  end

  # create an instance method
  def create_instance_method(method,&block)
    self.class_eval do
      define_method method do |*args|
        block.call *args
      end
    end
  end

  # Iterates over all subclasses (direct and indirect)
  def each_subclass
    ObjectSpace.each_object(Class) { | candidate |
      begin
        yield candidate if candidate < self
      rescue ArgumentError
        # comparison of Class with Class failed (ArgumentError)
      end
    }
  end

  # Returns an Array of subclasses (direct and indirect)
  def subclasses_all
    ret = []
    each_subclass {|c| ret.push c}
    ret
  end

  alias :all_subclasses :subclasses_all

  # Returns an Array of direct subclasses
  def subclasses
    ret = []
    each_subclass {|c| ret.push(c) if c.superclass == self }
    ret
  end
  alias :subclass :subclasses

  # create singleton attribute
  def class_attr_accessor(name)

    ### GET
    begin
      define_method name do
        class_variable_get "@@#{name}"
      end
    end

    ### SET
    begin
      define_method "#{name}=" do |new_val|
        class_variable_set "@@#{name}", new_val
      end
    end

  end

  # create class instance attribute
  def instance_attr_accessor(name)

    ### GET
    begin
      define_method name do
        instance_variable_get "@#{name}"
      end
    end

    ### SET
    begin
      define_method "#{name}=" do |new_val|
        instance_variable_set "@#{name}", new_val
      end
    end

  end

end

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
mpatch-1.3.0 lib/mpatch/class.rb
mpatch-1.2.0 lib/mpatch/class.rb
mpatch-1.1.0 lib/mpatch/class.rb
mpatch-1.0.2 lib/mpatch/class.rb
mpatch-1.0.1 lib/mpatch/class.rb
mpatch-1.0.0 lib/mpatch/class.rb
procemon-0.8.1 lib/procemon/mpatch/class.rb
procemon-0.8.0 lib/procemon/mpatch/class.rb
procemon-0.7.0 lib/procemon/mpatch/class.rb
procemon-0.6.3 lib/procemon/mpatch/class.rb
procemon-0.6.2 lib/procemon/mpatch/class.rb
procemon-0.6.1 lib/procemon/mpatch/class.rb
procemon-0.5.0 lib/procemon/mpatch/class.rb
procemon-0.4.5 lib/procemon/mpatch/class.rb
procemon-0.4.4 lib/procemon/mpatch/class.rb
procemon-0.4.3 lib/procemon/mpatch/class.rb
procemon-0.4.2 lib/procemon/mpatch/class.rb
procemon-0.4.1 lib/procemon/mpatch/class.rb
procemon-0.4.0 lib/procemon/mpatch/class.rb
procemon-0.3.4 lib/procemon/mpatch/class.rb