Class: ActiveSupport::CodeGenerator::MethodSet

Inherits:
Object
  • Object
show all
Defined in:
lib/active_support/code_generator.rb

Constant Summary collapse

METHOD_CACHES =
Hash.new { |h, k| h[k] = Module.new }

Instance Method Summary collapse

Constructor Details

#initialize(namespace) ⇒ MethodSet

Returns a new instance of MethodSet.



8
9
10
11
12
13
# File 'lib/active_support/code_generator.rb', line 8

def initialize(namespace)
  @cache = METHOD_CACHES[namespace]
  @sources = []
  @methods = {}
  @canonical_methods = {}
end

Instance Method Details

#apply(owner, path, line) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/active_support/code_generator.rb', line 28

def apply(owner, path, line)
  unless @sources.empty?
    @cache.module_eval("# frozen_string_literal: true\n" + @sources.join(";"), path, line)
  end
  @canonical_methods.clear

  @methods.each do |as, canonical_name|
    owner.define_method(as, @cache.instance_method(canonical_name))
  end
end

#define_cached_method(canonical_name, as: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_support/code_generator.rb', line 15

def define_cached_method(canonical_name, as: nil)
  canonical_name = canonical_name.to_sym
  as = (as || canonical_name).to_sym

  @methods.fetch(as) do
    unless @cache.method_defined?(canonical_name) || @canonical_methods[canonical_name]
      yield @sources
    end
    @canonical_methods[canonical_name] = true
    @methods[as] = canonical_name
  end
end