gavmor / Dunmanifestin

A verisimilitude generator.
3 stars 4 forks source link

a army of elfs with bowl made of woods #2

Closed benchristel closed 11 years ago

benchristel commented 11 years ago

We should come up with a more general solution for phrase templating that will let us handle plurals and agreement.

I think the best way to handle this is for phrases to be objects. A phrase's to_s method will interpolate some other to_sed phrases into a string, like:

class Phrase
  def method_missing meth, *args, &block
    instance_variable_get "@#{meth}" or
    instance_variable_set "@#{meth}", meth.capitalize.constantize.new(*args)
  end

  def initialize with_string
    @__string__ = with_string
  end

  def to_s
    @__string__.to_s
  end
end

class Denizen < Phrase
  def to_s
    "#{name}, a #{race} with a bunch of #{item.pluralize}"
  end
end

A phrase can have other methods like pluralize that may delegate to subphrases.

class Item < Phrase
  def to_s
    "#{adjective} #{baseItem} #{description}"
  end

  def pluralize
    baseItem.pluralize
  end
end

The default pluralization is just to to_s yourself and add "s"

class Phrase
  def pluralize
    Phrase.new "#{self}s"
  end

  def article
    if self.to_s =~ /^[aeiou]/i
      Phrase.new "an #{self}"
    else
      Phrase.new "a #{self}"
    end
  end
end
gavmor commented 11 years ago

Awkward agreements abound, but accessible phrase syntax has priority over their abolishment.

As long as our top-level DSL remains simplistic, I'm excited to ensure legal output! On Sep 3, 2013 9:31 PM, "benchristel" notifications@github.com wrote:

We should come up with a more general solution for phrase templating that will let us handle plurals and agreement.

I think the best way to handle this is for phrases to be objects. A phrase's to_s method will interpolate some other to_sed phrases into a string, like:

class Phrase def method_missing meth, _args, &block instance_variable_get "@#{meth}" or instance_variable_set "@#{meth}", meth.capitalize.constantize.new(_args) end

def initialize with_string @string = with_string end

def to_s @string.to_s endend class Denizen < Phrase def to_s "#{name}, a #{race} with a bunch of #{item.pluralize!}" endend

A phrase can have other methods like pluralize! that may delegate to subphrases.

class Item < Phrase def to_s "#{adjective} #{baseItem} #{description}" end

def pluralize! baseItem.pluralize! endend

The default pluralization is just to to_s yourself and add "s"

class Phrase def pluralize! Phrase.new "#{self}s" end

def article! if self.to_s =~ /^[aeiou]/i Phrase.new "an #{self}" else Phrase.new "a #{self}" end endend

— Reply to this email directly or view it on GitHubhttps://github.com/quavmo/Dunmanifestin/issues/2 .

benchristel commented 11 years ago

Now it's totally possible to ensure "An army of elves with bowls made of wood". Closing!