Module: RecordInvocation::Record

Defined in:
lib/record_invocation/record_invocation.rb

Instance Method Summary collapse

Instance Method Details

#prepend_record_moduleObject



119
120
121
122
123
# File 'lib/record_invocation/record_invocation.rb', line 119

def prepend_record_module
  mod = Module.new
  prepend mod
  mod
end

#record_macro(method_name, &blk) ⇒ Object Also known as: record



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/record_invocation/record_invocation.rb', line 125

def record_macro(method_name, &blk)
  record_module.define_method(method_name) do |*args, **kwargs, &block|
    parameters = method(method_name).super_method.parameters

    positional_arguments = args.dup
    keyword_arguments = kwargs.dup

    arguments = {}

    parameters.each do |type, name|
      case type
      when :req, :opt
        if positional_arguments.any?
          arguments[name] = positional_arguments.shift
        end
      when :rest
        if positional_arguments.any?
          arguments[name] = positional_arguments
        end
      when :key, :keyreq
        if keyword_arguments.key?(name)
          arguments[name] = keyword_arguments.delete(name)
        end
      when :keyrest
        arguments[name] = keyword_arguments
      when :block
        if not block.nil?
          arguments[name] = block
        end
      end
    end

    invocation = Invocation.new(method_name, arguments)
    __record(invocation)

    super(*args, **kwargs, &block)
  end
end

#record_moduleObject



115
116
117
# File 'lib/record_invocation/record_invocation.rb', line 115

def record_module
  @record_module ||= prepend_record_module
end