Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate getter, setter and attribute value #36

Open
schmijos opened this issue Nov 15, 2024 · 0 comments
Open

Separate getter, setter and attribute value #36

schmijos opened this issue Nov 15, 2024 · 0 comments
Assignees

Comments

@schmijos
Copy link
Member

I tried something like this:

CleanShot.2024-11-15.at.12.40.27.mp4
class Row < ApplicationRecord
  [*"A".."Z"].each do |letter|
    define_method(letter) do
      evaluate(self[letter])
    end

    define_method("#{letter}=") do |value|
      self[letter] = value
    end
  end

  def evaluate(value)
    return value unless value.is_a?(String) && value.start_with?('=')

    begin
      formula = value[1..] # Strip the '=' prefix
      formula.gsub!(/\$([A-Z])\$([0-9]+)/) do |match|
        col = Regexp.last_match(1)
        row = Regexp.last_match(2).to_i
        referenced_row = self.class.find(row)
        referenced_row.send(col)
      end

      eval(formula)
    rescue StandardError => e
      "Error: #{e.message}"
    end
  end
end

This use case would need a separation of attribute=, attribute and self[:attribute].
I'd like to see the getter value, but I'd like to edit the value in the database.

@simon-isler simon-isler self-assigned this Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants