Class: MethodArgs::Processor

Inherits:
SexpProcessor
  • Object
show all
Defined in:
lib/method_args.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessor

Returns a new instance of Processor.



97
98
99
100
101
# File 'lib/method_args.rb', line 97

def initialize
  @method_maps = Hash.new{|h,k| h[k] = {}}
  @current_class = []
  super()
end

Instance Attribute Details

#method_mapsObject (readonly)

Returns the value of attribute method_maps.



95
96
97
# File 'lib/method_args.rb', line 95

def method_maps
  @method_maps
end

Instance Method Details

#add_methodsObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/method_args.rb', line 174

def add_methods
  unless current_class.method(:const_defined?).arity == -1 ? current_class.const_defined?(:ArgList, false) : current_class.const_defined?(:ArgList)
    current_class.send(:const_set, :ArgList, @method_maps[current_classname])
    current_class.module_eval("      alias_method :__method__, :method\n      \n      class << self\n        alias_method :__instance_method__, :instance_method unless method_defined?(:__instance_method__)\n      end\n\n      def self.instance_arg_list(method_name)\n        method = __instance_method__(method_name)\n        if method.owner == self\n          ArgList[method_name] or raise('i don\\\\'t know this method ' + method_name.inspect)\n        elsif method.owner.respond_to?(:instance_arg_list)\n          method.owner.instance_arg_list(method_name)\n        else\n          raise \\\"\\\#{method.owner} has not been loaded with method_args\\\"\n        end\n      end\n\n    HERE_DOC\n  end\nend\n", __FILE__, __LINE__)

#current_classObject



203
204
205
# File 'lib/method_args.rb', line 203

def current_class
  @current_class.inject(Module) {|c, m| c.const_get(m)}
end

#current_classnameObject



199
200
201
# File 'lib/method_args.rb', line 199

def current_classname
  @current_class.map{|c| c.to_s}.join('::')
end

#process_args(exp) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/method_args.rb', line 147

def process_args(exp)
  exp.shift
  arg_list = ArgList.new(current_class)
  while !exp.empty?
    t = exp.shift
    case t
    when Symbol
      arg_list << case t.to_s[0]
      when ?* then ArgList::Arg.new(t.to_s[1, t.to_s.size].to_sym, :splat)
      when ?& then ArgList::Arg.new(t.to_s[1, t.to_s.size].to_sym, :block)
      else         ArgList::Arg.new(t, :required)
      end
    when Sexp
      case t.shift
      when :block
        lasgn = t.shift
        lasgn.shift
        name = lasgn.shift
        new_arg = ArgList::Arg.new(name, :optional, @ruby2ruby.process(lasgn.last))
        arg_list.each_with_index{|arg, idx| arg_list[idx] = new_arg if arg.name == name}
      end
    end
  end
  @method_maps[current_classname][@current_method] = arg_list
  add_methods
end

#process_class(exp) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/method_args.rb', line 112

def process_class(exp)
  exp.shift
  current_class_size = @current_class.size
  case exp.first
  when Symbol
    @current_class << exp.first.to_sym
    process(exp)
  else
    if exp.first.first == :colon2
      exp.first.shift
      class_exp = exp.shift
      class_exp[0, class_exp.size - 1].each do |const|
        @current_class << const.last
      end
      @current_class << class_exp.last
    else
      raise
    end
    exp.shift
    process(exp.first)
  end
  @current_class.slice!(current_class_size, @current_class.size)
  exp.clear
  exp
end

#process_defn(exp) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/method_args.rb', line 138

def process_defn(exp)
  exp.shift
  @current_method = exp.shift
  @ruby2ruby = Ruby2Ruby.new
  process_args(exp.shift)
  scope = exp.shift
  exp
end

#process_module(exp) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/method_args.rb', line 103

def process_module(exp)
  exp.shift
  @current_class << exp.first.to_sym
  process(exp)
  @current_class.pop
  exp.clear
  exp
end