Sha256: 3c4562a4d917c77d728efd28e3bfa58752a9c88a8f673b0e624e1de18df065de

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

require 'support/delegated'

module V8
  class Object
    class << self
      alias_method :native_new, :new
      
      def new(*args, &block)
        res = native_new(*args)
        res.send(:reflect_from, args.first)        
      end
    end
    
    def respond_to?(meth)
      !self[meth].undefined? or super
    end

    def method_missing(meth, *args, &block)
      if respond_to?(meth)
        property = self[meth]

        if property.is_a?(V8::Function)
          return property.call_on(self, *args, &block)
        else
          return property
        end
      end

      super
    end

    include Comparable
    include Enumerable
    include Delegated

    def to_hash
      Hash[*keys.map {|key| [key.to_s, get(key)] }.flatten(1)]
    end

    def <=>(other)
      to_hash <=> other
    end

    def each(*args, &block)
      to_hash.each(*args, &block)
    end

    def delegate
      to_hash
    end

    private

    def reflect_from(obj)
      if !obj.nil? and !obj.v8? and !obj.is_a?(Hash)
        obj.class.declared_methods.each { |meth|
          if func_name = meth.to_sym.to_js_func_name
            func = set(func_name, obj.method(meth))
            func.bind(obj)
          end
        }
      end
      self
    end
  end # Object
end # V8

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mustang-0.1.1 lib/v8/object.rb
mustang-0.1.0 lib/v8/object.rb