Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6782735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:46:44+00:00 2026-05-26T16:46:44+00:00

In my app I am having to interface with some 3rd party software I

  • 0

In my app I am having to interface with some 3rd party software I hope one day to replace. So, rather than keeping all the code that maps between my models’ data and the form it needs to be in for the 3rd party software in the models themselves, I’ve created a mapper module for each model, isolating the code somewhere that’s easy to delete when the time comes.

So I have something like the following:

app/
  models/
    people.rb
  mappers/
    people_mapper.rb

Ideally, I’d like to automatically include the modules in the model class with the matching name, the same way that helpers are automatically included in views of the same name. How/where are the helpers automatically included, and is this also the best place for me to add my own code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:46:44+00:00Added an answer on May 26, 2026 at 4:46 pm

    you can try something like this :

    module Mapper::Core
    
      def self.included( base )
        base.extend( ClassMethods )
      end
    
      module ClassMethods
    
        # model class method to include matching module
        # this will throw an error if matching class constant name does not exist
        def has_mapping
          @mapper_class = Kernel.const_get( "Mapper::#{self}Mapper" )
          include @mapper_class
        end
    
        # an accessor to the matching class mapper may come in handy
        def mapper_class
          @mapper_class
        end
    
      end
    end     
    

    then then require and include the module in ActiveRecord::Base in an initializer (make sure that your Mapper module requires all the files in your ‘mappers’ folder, or use config.autoload_paths).

    If you don’t want to use the has_mapping class method at all, you can try to override ActiveRecord::Base‘s self.inherited callback, but it might become dangerous:

      def self.included( base )
        base.extend( ClassMethods )
        base.instance_eval <<-EOF
          alias :old_inherited :inherited
          def self.inherited( subclass )
            subclass.has_mapping
            old_inherited( subclass )
          end
        EOF
      end
    

    I did not try any of this, so proceed with caution.

    EDIT :

    i was tired when i wrote this. there is a much simpler way to autoinclude the matching module :

    module Mapper::Core
      def self.included( base )
        begin
          mapper_class = Kernel.const_get( "Mapper::#{base.name}Mapper" )
          base.instance_eval( "include #{mapper_class}" ) 
        rescue 
          Logger.info "No matching Mapper Class found for #{base.name}"
        end
      end
    end
    

    initialize this with :

    ActiveRecord::base.instance_eval( 'include Mapper::Core' )
    

    all inheriting class will now include Mapper::Core, which will trigger inclusion of matching class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble keeping my app responsive to user actions. Therefore, I'd like to
I'm having a problem running a VS 2005 app on some machines and not
I am internationalizing my app, and after having translated the interface with the yml
I'm having some trouble searching a SQLite database in my Android app. I use
I'm having some problems with a Navigation controller in my app. Moving from the
I'm having some problems with proguard when optimizing my android app. It seems that
I'm having an issue where some of my views look incorrect on App load
I am creating an app that is having a UIWebView which contains an advert.
I have a huge web app that is having issues with memory leak in
I just created an iPhone app and am having friends try it out, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.