Module: V8::Access::Names

Included in:
V8::Access
Defined in:
lib/v8/access/names.rb

Instance Method Summary collapse

Instance Method Details

#accessible_names(obj, special_methods = false) ⇒ Object


47
48
49
50
51
52
53
54
55
56
# File 'lib/v8/access/names.rb', line 47

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

#delete(obj, name, &dontintercept) ⇒ Object


43
44
45
# File 'lib/v8/access/names.rb', line 43

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

#get(obj, name, &dontintercept) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/v8/access/names.rb', line 8

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

#names(obj) ⇒ Object


4
5
6
# File 'lib/v8/access/names.rb', line 4

def names(obj)
  accessible_names(obj)
end

#query(obj, name, attributes, &dontintercept) ⇒ Object


32
33
34
35
36
37
38
39
40
41
# File 'lib/v8/access/names.rb', line 32

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

#set(obj, name, value, &dontintercept) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/v8/access/names.rb', line 20

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