Sha256: 1ed79cb16889e47843f1bfc822e6aaddcd37186bb7343758dce2babc7c03fe93

Contents?: true

Size: 1.33 KB

Versions: 10

Compression:

Stored size: 1.33 KB

Contents

# TITLE:
#   Prototype
#
# SUMMARY:
#   Prototype-base Object-Oriented programming.
#
# COPYRIGHT:
#   Copyright (c) 2006, 2007 Thomas Sawyer


# = Prototype class

class Prototype

  # New prototype object.

  def initialize(&block)
    @traits = []

    instance_eval(&block)

    h = {}
    iv = instance_variables
    iv.each { |k| h[k[1..-1].to_sym] = instance_eval{ instance_variable_get(k) } }
    meta.class_eval do
      h.each do |k,v|
        case v
        when Proc
          #define_method(k){ |*args| v[*args] }
          attr_reader k
        else
          attr_accessor k
        end
      end
    end
  end

  def fn(&blk)
    proc(&blk)
  end

  def new(o=nil)
    return o.clone if o
    return clone
  end

  def meta
   (class << self; self; end)
  end

  def traits
    @traits
  end

  def trait(obj)
    traits << obj.new
  end

  def method_missing(s, *a, &b)
    if trait = traits.find{ |t| t.method_defined?(s) }
      trait.send(s,*a,&b)
    else
      super
    end
  end

end


module Kernel

  def prototype(&block)
    Prototype.new(&block)
  end

  #private

  # Synonymous with #clone, this is an interesting
  # method in that it promotes prototype-based Ruby.
  # Now Classes aren't the only things that respond to #new.
  #
  #   "ABC".new  => "ABC"
  #
  def new(o=nil)
    return o.clone if o
    return clone
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.0.5 lib/more/facets/prototype.rb
facets-2.1.2 lib/more/facets/prototype.rb
facets-2.0.3 lib/more/facets/prototype.rb
facets-2.0.4 lib/more/facets/prototype.rb
facets-2.1.0 lib/more/facets/prototype.rb
facets-2.1.1 lib/more/facets/prototype.rb
facets-2.1.3 lib/more/facets/prototype.rb
facets-2.2.0 lib/more/facets/prototype.rb
facets-2.2.1 lib/more/facets/prototype.rb
facets-2.3.0 lib/more/facets/prototype.rb