Sha256: 90537fb37c39a8f829516dbba432cba8b33478d3ddc9b5da5ec8c6d73c9ea0b5

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

module Mustang
  module V8
    class Object
      # We have to extend native <tt>.new</tt> method with full support for reflection
      # of ruby objects. 
      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) # :nodoc:
        !self[meth].undefined? or super
      end

      def method_missing(meth, *args, &block) # :nodoc:
        if respond_to?(meth)
          property = self[meth]
          if property.is_a?(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) # :nodoc:
        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
end # Mustang

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mustang-0.2.2 lib/mustang/v8/object.rb
mustang-0.2.1 lib/mustang/v8/object.rb
mustang-0.2.0 lib/mustang/v8/object.rb