Sha256: 2396e5f025b2c936d3a4d9986b3afd8fb1533c895c374600e6d0ed7454f4f421

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

# Require files in your internal folder
require 'ga_example_gem/client'

# This is our GaExampleGem namespace
# for our gem

module GaExampleGem
  # This is weird. 
  # These are now class methods.
  # So you no longer have to call def self.method_missing
	class << self  
		# Alias for GaExampleGem::Client.new
		def new
      @client ||= GaExampleGem::Client.new
    end

    # Delegate to GaExampleGem::Client
    # This is a weird trick that allows for calling 
    # on methods without calling .new first
    def method_missing(method, *args, &block)
      # Normally this raises an error
      return super unless new.respond_to?(method)
      new.send(method, *args, &block)
    end


    # This is used for the method_missing to see
    # if a new client would respond to a method
    def respond_to?(method, include_private=false)
      new.respond_to?(method, include_private) || super(method, include_private)
    end

    # The logic for above
    # 1) Call a method, GaExampleGem.configure
    # 2) Run .method_missing, because we don't have that method
    # 3) Name of method is 'configure'
    # 4) Does a new instance of the client respond to configure?
    # 5) If yes, create a new client and run the method 'configure'
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ga_example_gem-0.0.4 lib/ga_example_gem.rb
ga_example_gem-0.0.3 lib/ga_example_gem.rb