Skip to content

How can I include a module? #162

Description

@ejstembler

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>>>", ...

Activity

  1. dazuma commented on Jun 30, 2023

    @dazuma
    Contributor

    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
  2. dazuma commented on Jun 30, 2023

    @dazuma
    Contributor

    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.

  3. Spaceghost commented on Jul 26, 2023

    @Spaceghost
    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)?

  4. akerekes commented on Jan 3, 2025

    @akerekes
    Contributor

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestp3questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions