Skip to content

Support for custom object evaluation #227

@bbugh

Description

@bbugh

Hi! 👋 This gem looks awesome, thanks for making it. It's a perfect fit for some needs we have to do simple user template evaluation to generate documents. Our use case includes object evaluation, though, and I couldn't see a way for it to work with dentaku. For example, "user.age >= 18, where user is a simple class like this:

class User
  attr_reader :age
end

I tried doing something like

calculator.store(user: ->{ User.last })
calculator.evaluate("user.age >= 15")
=> nil

We managed to make it work by adding a string representing the object chain...

calculator.store('user.age', ->{ User.last.age })
calculator.evaluate("user.age >= 15")
=> true

...but we have hundreds of variations and nestings we'd have to support (like user.profile.default_value) and it wouldn't be practical to generate them all.

Is there a mechanism that would allow for the dot chain evaluation that I missed, maybe some kind of custom evaluator? If not, do you think it would be feasible to add?

(We can also pre-evaluate the values into a string, and then use Dentaku to calculate the final results, but that would unfortunately require a large refactor of our parser and generator, so this seems like the simplest route to start).

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions