Class: Johnson::RubyLandProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/johnson/ruby_land_proxy.rb

Direct Known Subclasses

SpiderMonkey::RubyLandProxy

Defined Under Namespace

Modules: Callable, ProxyHelper

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/johnson/ruby_land_proxy.rb', line 92

def method_missing(sym, *args, &block)
  args << block if block_given?

  name = sym.to_s
  assignment = "=" == name[-1, 1]

  # default behavior if the slot's not there
  return super unless assignment || respond_to?(sym)

  unless function_property?(name)
    # for arity 0, treat it as a get
    return self[name] if args.empty?

    # arity 1 and quacking like an assignment, treat it as a set
    return self[name[0..-2]] = args[0] if assignment && 1 == args.size
  end

  # okay, must really be a function
  call_function_property(name, *args)
end

Class Method Details

.add_conversion(conversion, &test) ⇒ Object



37
38
39
# File 'lib/johnson/ruby_land_proxy.rb', line 37

def add_conversion(conversion, &test)
  conversions << [conversion, test]
end

.add_wrapper(wrapper, &test) ⇒ Object



18
19
20
# File 'lib/johnson/ruby_land_proxy.rb', line 18

def add_wrapper(wrapper, &test)
  wrappers.push [wrapper, test]
end

.apply_conversions(proxy) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/johnson/ruby_land_proxy.rb', line 28

def apply_conversions(proxy)
  conversions.each do |(conversion, test)|
    if test.call(proxy)
      converted = conversion.call(proxy)
      return converted unless converted.eql? proxy
    end
  end
  proxy
end

.apply_wrappers(proxy) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/johnson/ruby_land_proxy.rb', line 4

def apply_wrappers(proxy)
  wrappers.each do |(wrapper, test)|
    next if test && !test.call(proxy)
    next if wrapper.respond_to?(:test?) && !wrapper.test?(proxy)

    if wrapper.respond_to?(:call)
      proxy = wrapper.call(proxy)
      break unless Johnson::RubyLandProxy === proxy
    else
      proxy.send :extend, wrapper
    end
  end
  proxy
end

.conversionsObject



40
41
42
# File 'lib/johnson/ruby_land_proxy.rb', line 40

def conversions
  @conversions ||= []
end

.insert_wrapper(wrapper, &test) ⇒ Object



21
22
23
# File 'lib/johnson/ruby_land_proxy.rb', line 21

def insert_wrapper(wrapper, &test)
  wrappers.unshift [wrapper, test]
end

.wrappersObject



24
25
26
# File 'lib/johnson/ruby_land_proxy.rb', line 24

def wrappers
  @wrappers ||= []
end

Instance Method Details

#inspectObject



88
89
90
# File 'lib/johnson/ruby_land_proxy.rb', line 88

def inspect
  toString
end

#sizeObject

FIXME: need to revisit array vs non-array proxy, to_a/to_ary semantics, etc.



80
# File 'lib/johnson/ruby_land_proxy.rb', line 80

def size; length; end

#to_aryObject



81
# File 'lib/johnson/ruby_land_proxy.rb', line 81

def to_ary; to_a; end