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 6865541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:04:01+00:00 2026-05-27T03:04:01+00:00

theres an excerpt of my code: module Configuracao extend self class Key include ActiveModel::Validations

  • 0

theres an excerpt of my code:

module Configuracao
  extend self

  class Key
    include ActiveModel::Validations
    attr_accessor :name, :type, :default, :validations, :group, :available_values

    def initialize(params)
      params.symbolize_keys!.assert_valid_keys(:name, :type, :default, :validations, :group, :available_values)
      @group = params[:group]
      @name = params[:name]
      @type = params[:type]
      @available_values = params[:available_values]
      @default = params[:default]
      @validations = params[:validations]

      #in this way each validation is being added for all keys
      Configuracao::Key.class_eval do
        validates :value, params[:validations]
      end
    end
  end
end

so for every instance key i will have a diferent validation passed in a hash, example:

Key.new( validations: { presence: true, numericality: true } )
Key.new( validations: { length: { maximum: 30 } } )

There’s a way to do it?

  • 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-27T03:04:02+00:00Added an answer on May 27, 2026 at 3:04 am

    well i found a solution, maybe not so elegant or best way to do, but it works

    def initialize(params)
      params.symbolize_keys!.assert_valid_keys(:name, :type, :default, :validations, :group, :available_values)
      @group = params[:group]
      @name = params[:name]
      @type = params[:type]
      @available_values = params[:available_values]
      @default = params[:default]
      @@current_validations = nil
      @@current_validations = @validations = params[:validations]
    
      class << self
        validates :value, @@current_validations unless @@current_validations.blank?
      end
    end
    

    now each time i instantiate a Key, the class will be modified only for that instance

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

Sidebar

Related Questions

UPDATED: SEE BELOW I've been porting the code for this assignment: http://www.stanford.edu/class/cs221/progAssignments/PA1/search.html (the entire
I find myself handling exceptions without specifying an exception type when I call code
I am writing a module, in which the email string is validated before it
In Ruby on Rails, where does one put the code from this snippet in
I'd like to inspect and manipulate code of arbitrary Perl procedures (got by coderefs)
I am using pynotify and this is the code I am trying to get
I'm trying to implement dynamic reloading objects in Python, that reflect code changes live.
What's the shortest way to import a module from a distant, relative directory? We've
When it comes to run time introspection and dynamic code generation I don't think
Quick background: writing a module. One of my objects has methods that may or

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.