Open tra38 opened 7 years ago
The syntax limitation is by design, but I appreciate the feedback on this.
I’ll provide some recommendations once #21 is fixed.
In case anyone was curious, this was my final solution to this problem. If I get time, I'm going to look at Inflecto to see whether I can incorporate that library into this solution in case I ever need to deal with pluralization again.
module Macros
def generate_pluralization_method(singular:, plural:)
method_suffix = plural.gsub(" ","_")
define_method :"pluralize_#{method_suffix}" do |argument|
if (argument.to_f == 1)
"#{argument} #{singular}"
else
"#{argument} #{plural}"
end
end
end
end
module CatWatchHelpers
extend Macros
generate_pluralization_method(singular: "cat", plural: "cats")
end
class CatWatch < Calyx::Grammar
modifier CatWatchHelpers
start "Today, I saw {number.pluralize_cats} on the way to work."
end
reporter = CatWatch.new
reporter.generate(number: 1)
#=>Today, I saw 1 cat on the way to work.
The good news is that I am right now using Calyx in a real-world project. The bad news is that doing so means you quickly encounter edge cases...
Sometimes, your program has to generate sentences without knowing in advance if they'd be plural or not.
I thought about doing some monkey-patching to handle this sort of pluralization, with plans to eventually refactor to a "best practice" solution using modifiers...
However, this monkey-patched solution doesn't work in Calyx.
Refactoring to "best practice" doesn't work either.
Even more oddly, if I do a slight change to the start method...it is no longer gets interpreted as part of the string...even though I still get incorrect output.
Now, since I'm on a tough deadline, I wind up doing this solution instead to handle pluralization.
...which "works", but is pretty hacky. There has to be a better solution (one that hopefully doesn't involve using meta programming to generate a bunch of unique methods for handling pluralization). It'll do for now though.
I decided to follow best practice by moving my
pluralize_cats
method over to a module, but I then encountered a different issue with modifiers (which I reported in #21).