Module: Spark::CoreExtension::Module

Defined in:
lib/spark/ext/module.rb

Instance Method Summary collapse

Instance Method Details

#patch_unless_exist(target, suffix) ⇒ Object

Patch method to class unless already exist

Example:

class Hash
  def a
    1
  end
end

module HashExtension
  module InstanceMethods
    def a_with_spark
      2
    end

    def b_with_spark
      1
    end
  end

  def self.included(base)
    base.send(:include, InstanceMethods)
    base.class_eval do
      patch_unless_exist :a, :spark
      patch_unless_exist :b, :spark
    end
  end
end

Hash.include(HashExtension)

Hash.new.a # => 1
Hash.new.b # => 1


40
41
42
43
44
45
46
# File 'lib/spark/ext/module.rb', line 40

def patch_unless_exist(target, suffix)
  unless method_defined?(target)
    aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1

    alias_method target, "#{aliased_target}_with_#{suffix}#{punctuation}"
  end
end

#path_const_unless_exist(target, suffix) ⇒ Object



48
49
50
51
52
# File 'lib/spark/ext/module.rb', line 48

def path_const_unless_exist(target, suffix)
  unless const_defined?(target)
    const_set(target, const_get("#{target}_WITH_#{suffix}"))
  end
end