Class: Johnson::RubyLandProxy

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

Defined Under Namespace

Modules: Callable

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/johnson/ruby_land_proxy.rb', line 60

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_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_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

.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



56
57
58
# File 'lib/johnson/ruby_land_proxy.rb', line 56

def inspect
  toString
end

#sizeObject

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



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

def size; length; end

#to_aryObject



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

def to_ary; to_a; end