apotonick/representable — pull request #31 by tylerkahn (2013-01-17 17:33:22+00:00)
Title: give option to supply a Proc to a Definition that acts on the object

Give the option to supply a Proc to a Defintion that acts on the object
being represented.

class SensationalArticle
include Representable::JSON
attr_accessor :title

property :title, :proc => Proc.new {|article| article.title + "!"}

property :lede, :writeable => false, :proc => Proc.new { |article|
"Experts are warning that " + article.title.downcase + "!!"
}
end

Now let's use it.

a = SensationalArticle.new
a.title = "Bees Are Found To Make Honey"
a.to_json

Yields

{"title" : "Bees Are Found to make Honey!",
"lede" : "Experts are warning that bees are found to make honey!!}