Module: CountryIdMemoization

Extended by:
ActiveSupport::Concern
Included in:
Netcore::Payload
Defined in:
app/models/concerns/country_id_memoization.rb

Overview

Note:

The memoization is at the instance level, so each instance of the including class will have its own memoized values. This is appropriate for service objects, controllers, and other short-lived objects.

Note:

The underlying Country.find_* methods already use Rails cache, so this memoization provides an additional layer of optimization by avoiding even the cache lookup overhead within a single instance.

CountryIdMemoization provides instance-level memoization for country IDs to avoid repeated cache lookups across multiple method calls.

This concern is useful in classes that frequently need to access country IDs (Thailand, Singapore, Malaysia) within the same request/operation lifecycle.

Examples:

Include in a service class

class MyService
  include CountryIdMemoization

  def process_user(user)
    user.update(country_id: thailand_id)
    # Subsequent calls to thailand_id will use the memoized value
  end
end

Include in a module

module MyModule
  include CountryIdMemoization

  def user_data(user)
    {
      thai_points: user.get_credits(thailand_id),
      sg_points: user.get_credits(singapore_id),
      my_points: user.get_credits(malaysia_id)
    }
  end
end

Instance Method Summary collapse

Instance Method Details

#malaysia_idInteger

Returns the ID of Malaysia country with instance-level memoization

Examples:

malaysia_id # => 127

Returns:

  • (Integer)

    The ID of Malaysia country record



66
67
68
# File 'app/models/concerns/country_id_memoization.rb', line 66

def malaysia_id
  @malaysia_id ||= Country.find_malaysia.id
end

#singapore_idInteger

Returns the ID of Singapore country with instance-level memoization

Examples:

singapore_id # => 198

Returns:

  • (Integer)

    The ID of Singapore country record



57
58
59
# File 'app/models/concerns/country_id_memoization.rb', line 57

def singapore_id
  @singapore_id ||= Country.find_singapore.id
end

#thailand_idInteger

Returns the ID of Thailand country with instance-level memoization

Examples:

thailand_id # => 218

Returns:

  • (Integer)

    The ID of Thailand country record



48
49
50
# File 'app/models/concerns/country_id_memoization.rb', line 48

def thailand_id
  @thailand_id ||= Country.find_thailand.id
end