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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:53:52+00:00 2026-06-10T03:53:52+00:00

I’m trying to create a flexible dsl. I already have the DSL module, say

  • 0

I’m trying to create a flexible dsl. I already have the DSL module, say module DSL. The DSL user can create spin-offs of this as a class. The main point of the DSL is to allow the user to create Feature object with a custom render method. There was a lot of ugly and non-DRY code backing the Feature, hence the abstraction, but the user needs a lot of control on how that feature renders, and my meta-programming is not up to the task. Let me show you how it’s set up.

The DSL looks something like this:

module DSL
  module ClassMethods
      attr_accessor :features
      def column(name, *args)
        arguments = args.pop || {}
        self.features = [] if self.features.nil?
        self.features << Feature.new(name, arguments)
      end
    end
    def self.included(base)
      base.extend ClassMethods
    end
  end
end

An implementation of it would look something like this:

class DSLSpinOff
  include DSL

  feature :one
  feature :two, render_with: :predefined_render
  feature :three, render_with: :user_defined_render
  feature :four, render_with: lambda {
    puts "Go nuts, user!"
    puts "Do as you please!"
  }

  def user_defined_render
    #...
  end
end

And finally, the feature class itself lies within the DSL, like so:

module DSL
  #...
private
  class Feature
    attr_accessor :name, :render_with
    def initialize(name, *args)
      self.name = name
      attributes = args.pop || {}
      # somehow delegate attributes[:render_with] to the render function, handling defaults, lamdbas, function string names, etc
      self.render_with = attributes.fetch(:render_with, :default_render)
    end

  private
    def default_render
      #...
    end

    def predefined_render
      #...
    end
  end
end
  • 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-06-10T03:53:53+00:00Added an answer on June 10, 2026 at 3:53 am

    The magic I was looking for: define_singleton_method.

    module DSL
      #...
    private
      class Feature
        attr_accessor :name
        def initialize(name, args)
          #...
          define_singleton_method :render do
            if self.render_with.kind_of? Symbol
              content = self.send(self.render_with)
            else
              content = self.render_with.call
            end
            content.present? ? content.to_s.html_safe : '&ndash;'
          end
        end
      end
    end
    

    Now within the DSL I can iterate over all features and render them. It’ll send itself :default_render, or some other :predefined_render, or use the block provided instead. However, this does not let users define methods on within the DSLSpinOff and pass them in, since those methods would get delegated to the DSLSpinOff class instead of the DSLSpinOff::Column class.

    I suspect they would have to do something like:

    feature :three, render_with: self.method(:user_defined_render)
    def user_defined_render
      #...
    end
    

    Edit:

    I found a clean way to allow use of a default method, user-defined lambdas, pre-defined methods, and user-defined methods:

    module DSL
      #...
      class Feature
        attr_accessor :name, render_with
        def initialize(name, *args)
          self.name = name
          self.render_with = args.has_key?(:render_with) ? args[:render_with] : :default_render
          define_singleton_method :render do |object|
            render_method = self.render_with.is_a?(Proc) ? renderer : method(renderer) 
            render_method.call
          end
        end
    
      private
        def default_render
          #...
        end
    
        def predefined_render
          #...
        end
      end
    end
    

    This will grab lambdas or procs if the user passes those in, otherwise, it’ll use the method method to return a reference to the method defined on theFeature. The call method works on all three.

    To support user-defined render methods, just have the open the class in an initializer to make it findable by the method method:

    #initializers/custom_dsl_renders.rb
    class DSL::Feature
      def user_defined_render
        #...
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put

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.