Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I include a module? #162

Open
ejstembler opened this issue May 17, 2023 · 4 comments
Open

How can I include a module? #162

ejstembler opened this issue May 17, 2023 · 4 comments
Labels
enhancement New feature or request p3 question Further information is requested

Comments

@ejstembler
Copy link

Normally one would add module includes inside the class. How can this be done with FunctionsFramework?

For example, this doesn't work:

FunctionsFramework.http 'my_function' do |request|
  include GoogleCloudLoggable

  # ...
end
NoMethodError: undefined method `include' for #<#<Class:0x000029e70e0ca9f8>:0x000029e70f56a6b0 @__globals={:function_name=>\"my_function\", :function_type=>:http}, @__logger=#<Logger:0x000029e70e0b3398 @level=1, @progname=nil, @default_formatter=#<Logger::Formatter:0x000029e70e0b3258 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x000029e70e0b30a0 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDERR>>, @binmode=false, @mon_data=#<Monitor:0x000029e70e0b2fd8>, @mon_data_owner_object_id=2460>>>", ...
@dazuma
Copy link
Member

dazuma commented Jun 30, 2023

Hi @ejstembler, apologies for not noticing this sooner.

Conceptually, the block you provide to FunctionsFramework.http is a "function" (i.e. a method), not a class, so you can't call Module#include from it. But you can call Object#extend. The following should work:

FunctionsFramework.http 'my_function' do |request|
  extend GoogleCloudLoggable

  # ...
end

@dazuma
Copy link
Member

dazuma commented Jun 30, 2023

That said, let's leave this issue open. You bring up a good use case, and I think it would be good to provide a way to include modules in the underlying class rather than extending every instance individually.

@Spaceghost
Copy link
Contributor

@dazuma Do you already know what you'd like to happen in the block to make include work normally or would a PR for this be acceptable from an outside contributor (me)?

@HKWinterhalter HKWinterhalter added the question Further information is requested label Sep 25, 2023
@kenneth-rosario kenneth-rosario added enhancement New feature or request p3 labels Sep 25, 2023
@akerekes
Copy link
Contributor

akerekes commented Jan 3, 2025

@Spaceghost if you're still up to it, a PR would be welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request p3 question Further information is requested
Projects
None yet
Development

No branches or pull requests

6 participants