Class: RubyNext::Core::Patch

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-next/core.rb

Overview

Patch contains the extension implementation and meta information (e.g., Ruby version).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod = nil, method:, version:, name: nil, supported: nil, native: nil, location: nil, refineable: mod, core_ext: :patch, singleton: nil) ⇒ Patch

Create a new patch for module/class (mod) with the specified uniq name

‘core_ext` defines the strategy for core extensions:

- :patch — extend class directly
- :prepend — extend class by prepending a module (e.g., when needs `super`)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby-next/core.rb', line 21

def initialize(mod = nil, method:, version:, name: nil, supported: nil, native: nil, location: nil, refineable: mod, core_ext: :patch, singleton: nil)
  @mod = mod
  @method_name = method
  @version = version
  if method_name && mod
    @supported = supported.nil? ? mod.method_defined?(method_name) : supported
    # define whether running Ruby has a native implementation for this method
    # for that, we check the source_location (which is nil for C defined methods)
    @native = native.nil? ? (supported? && native_location?(mod.instance_method(method_name).source_location)) : native
  end
  @singleton = singleton
  @refineables = Array(refineable)
  @body = yield
  @core_ext = core_ext
  @location = location || build_location(caller_locations(1, 5))
  @name = name || build_module_name
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



13
14
15
# File 'lib/ruby-next/core.rb', line 13

def body
  @body
end

#core_extObject (readonly)

Returns the value of attribute core_ext.



13
14
15
# File 'lib/ruby-next/core.rb', line 13

def core_ext
  @core_ext
end

#locationObject (readonly)

Returns the value of attribute location.



13
14
15
# File 'lib/ruby-next/core.rb', line 13

def location
  @location
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



13
14
15
# File 'lib/ruby-next/core.rb', line 13

def method_name
  @method_name
end

#modObject (readonly)

Returns the value of attribute mod.



13
14
15
# File 'lib/ruby-next/core.rb', line 13

def mod
  @mod
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/ruby-next/core.rb', line 13

def name
  @name
end

#nativeObject (readonly) Also known as: native?

Returns the value of attribute native.



13
14
15
# File 'lib/ruby-next/core.rb', line 13

def native
  @native
end

#refineablesObject (readonly)

Returns the value of attribute refineables.



13
14
15
# File 'lib/ruby-next/core.rb', line 13

def refineables
  @refineables
end

#singletonObject (readonly) Also known as: singleton?

Returns the value of attribute singleton.



13
14
15
# File 'lib/ruby-next/core.rb', line 13

def singleton
  @singleton
end

#supportedObject (readonly) Also known as: supported?

Returns the value of attribute supported.



13
14
15
# File 'lib/ruby-next/core.rb', line 13

def supported
  @supported
end

#versionObject (readonly)

Returns the value of attribute version.



13
14
15
# File 'lib/ruby-next/core.rb', line 13

def version
  @version
end

Instance Method Details

#core_ext?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/ruby-next/core.rb', line 43

def core_ext?
  !mod.nil?
end

#prepend?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/ruby-next/core.rb', line 39

def prepend?
  core_ext == :prepend
end

#to_moduleObject



51
52
53
54
55
56
57
# File 'lib/ruby-next/core.rb', line 51

def to_module
  Module.new.tap do |ext|
    ext.module_eval(body, *location)

    RubyNext::Core.const_set(name, ext)
  end
end