Class: Method::DuckHunter

Inherits:
Object show all
Defined in:
lib/carat/duckhunter.rb

Overview

DuckHunter class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDuckHunter

Returns a new instance of DuckHunter.



76
77
78
# File 'lib/carat/duckhunter.rb', line 76

def initialize
  @ducks, @decoys = {}, {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(aSym, *args) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/carat/duckhunter.rb', line 84

def method_missing(aSym, *args)
  aSymStr = aSym.to_s

  # This will happen the first time
  @ducks[aSymStr] ||= [] #unless @ducks[aSymStr]
  @ducks[aSymStr] << args.collect { |a| "#{a.class}" }

  decoy = self.dup

  @decoys[aSymStr] ||= [] #unless @decoys[aSymStr]
  @decoys[aSymStr] << decoy

  # build proxy?
  #begin
  #  d = <<-HERE
  #    def self.#{aSymStr}(*args)
  #      # This will happen the subsequent times
  #      @ducks["#{aSymStr}"] << args.collect { |a| #{'"#{a.class}"'} }
  #      @ducks["#{aSymStr}"].uniq!
  #      decoy = self.dup
  #      @decoys["#{aSymStr}"] = [] unless @decoys["#{aSymStr}"]
  #      @decoys["#{aSymStr}"] << decoy
  #      decoy
  #    end
  #  HERE
  #  instance_eval d
  #rescue SyntaxError
  #  puts "This error may be avoidable by returning the failing duck type as the error message."
  #  raise
  #end

  decoy
end

Instance Attribute Details

#decoysObject (readonly)

Returns the value of attribute decoys.



74
75
76
# File 'lib/carat/duckhunter.rb', line 74

def decoys
  @decoys
end

#ducksObject (readonly)

Returns the value of attribute ducks.



74
75
76
# File 'lib/carat/duckhunter.rb', line 74

def ducks
  @ducks
end

Class Method Details

.duckcallObject



65
66
67
68
69
70
71
72
# File 'lib/carat/duckhunter.rb', line 65

def DuckHunter.duckcall
  begin
    yield
  rescue TypeError => e
    self.send(e.message)
    retry
  end
end

Instance Method Details

#initialize_copy(from) ⇒ Object



80
81
82
# File 'lib/carat/duckhunter.rb', line 80

def initialize_copy(from)
  initialize
end