2

In one of my views, I rendered a table with a helper method, so the view (haml) looked like this:

= table do
  - action "Add"
  - column :id
  - column :name

After I changed the helper and used the ViewComponent lib instead, I need to call it the following way:

= table do |t|
  - t.action "Add"
  - t.column :id
  - t.column :name

I wondered if it's possible to convert the block in example 1 to the block in example 1 in a helper method, so I don't need to rewrite every view that uses a table.

The helper method would look like:

def table(*args, **kwargs, &block)
  # ...
  render TableComponent.new(*args, **kwargs, &new_block)
end
andi2.2
  • 86
  • 1
  • 11
  • Do you really have that many views so that you want to spend time on a fools errand instead of fixing the original problem? Use a regex in your editor to find the offenses and fix them. – max Mar 10 '22 at 10:03
  • @max changing the views is not the problem, but I this would keep the usage of this helper consistent with the project – andi2.2 Mar 10 '22 at 11:44
  • 2
    In Ruby, a block is an anonymous function. What you are proposing would be code that could take a function, modify that function in place based on the unknown contents of the function and then forward the new block on. While Ruby does have meta-programming, I don't see how this could be done. I agree with @max that it seems that it would be easier to use regex or write code that reads your source code and updates it than to try and write this helper. – clarked Apr 04 '22 at 22:17
  • you are asking for a DSL, e.g. some `instance_eval` probably – Dorian Apr 09 '22 at 20:35

1 Answers1

1

Let's simplify your example to this:

class Table
  def initialize
    @rows = []
  end

  def action(name)
    @rows << "Action #{name}"
  end

  def column(name)
    @rows << "Column #{name}"
  end

  def to_s
    "Table:\n======\n#{@rows.join("\n")}"
  end
end

def table(&block)
  t = Table.new
  block.call(t)
  t
end

puts(
  table do |t|
    t.action "Add"
    t.column :id
    t.column :name
  end
)

That gives:

Table:
======
Action Add
Column id
Column name

Now you want to do:

  table do
    action "Add"
    column :id
    column :name
  end

So you want to have the body of the block be in the same context as the instance (e.g. like being in a Table instance), so:

def table(&block)
  t = Table.new
  t.instance_eval(&block)
  t
end

(that's how most Domain Specific Languages are made :) )

Dorian
  • 7,749
  • 4
  • 38
  • 57