Sha256: 4709363ae02704bb18599c882755d66b150350f8e4a9007920cbb4ddf294655c

Contents?: true

Size: 1.64 KB

Versions: 37

Compression:

Stored size: 1.64 KB

Contents

require 'set'
class V8::Access
  module Names
    def names(obj)
      accessible_names(obj)
    end

    def get(obj, name, &dontintercept)
      methods = accessible_names(obj)
      if methods.include?(name)
        method = obj.method(name)
        method.arity == 0 ? method.call : method.unbind
      elsif obj.respond_to?(:[]) && !special?(name)
        obj.send(:[], name, &dontintercept)
      else
        yield
      end
    end

    def set(obj, name, value, &dontintercept)
      setter = name + "="
      methods = accessible_names(obj, true)
      if methods.include?(setter)
        obj.send(setter, value)
      elsif obj.respond_to?(:[]=) && !special?(name)
        obj.send(:[]=, name, value, &dontintercept)
      else
        yield
      end
    end

    def query(obj, name, attributes, &dontintercept)
      if obj.respond_to?(name)
        attributes.dont_delete
        unless obj.respond_to?(name + "=")
          attributes.read_only
        end
      else
        yield
      end
    end

    def delete(obj, name, &dontintercept)
      yield
    end

    def accessible_names(obj, special_methods = false)
      obj.public_methods(false).map {|m| m.to_s}.to_set.tap do |methods|
        ancestors = obj.class.ancestors.dup
        while ancestor = ancestors.shift
          break if ancestor == ::Object
          methods.merge(ancestor.public_instance_methods(false).map {|m| m.to_s})
        end
        methods.reject!(&special?) unless special_methods
      end
    end

    private

    def special?(name = nil)
      @special ||= lambda {|m| m == "[]" || m == "[]=" || m =~ /=$/}
      name.nil? ? @special : @special[name]
    end
  end
end

Version data entries

37 entries across 37 versions & 8 rubygems

Version Path
arcabouco-0.2.13 vendor/bundle/gems/therubyracer-0.12.2/lib/v8/access/names.rb
therubyracer-0.12.3 lib/v8/access/names.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/therubyracer-0.11.4/lib/v8/access/names.rb
therubyracer-xcode-0.12.3 lib/v8/access/names.rb
therubyracer-xcode-0.12.2 lib/v8/access/names.rb
therubyracer-tmpfork-0.12.2 lib/v8/access/names.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/therubyracer-0.12.2/lib/v8/access/names.rb
therubyracer-0.12.2 lib/v8/access/names.rb
therubyracer-0.12.1 lib/v8/access/names.rb
therubyracer-discourse-0.12.0 lib/v8/access/names.rb
therubyracer-0.12.0 lib/v8/access/names.rb
therubyracer-0.11.4 lib/v8/access/names.rb
therubyracer-0.11.3 lib/v8/access/names.rb
therubyracer-0.11.2 lib/v8/access/names.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/therubyracer-0.11.0/lib/v8/access/names.rb
therubyracer-0.11.1 lib/v8/access/names.rb
therubyracer-0.11.1beta1 lib/v8/access/names.rb
therubyracer-0.11.0 lib/v8/access/names.rb
therubyracer-0.11.0beta8-x86-freebsd-9 lib/v8/access/names.rb
therubyracer-0.11.0beta8-x86_64-linux lib/v8/access/names.rb