Skip to content

Instantly share code, notes, and snippets.

@ngauthier
Created November 17, 2010 18:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ngauthier/703747 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'active_support'
require 'active_support/core_ext/object/to_json.rb'
class Widget
attr_accessor :id
attr_accessor :name
attr_accessor :gadgets
end
class Gadget
attr_accessor :id
attr_accessor :name
end
class GadgetPresenter
attr_reader :id
attr_reader :name
def initialize(gadget)
@id = %{gadget-#{gadget.id}}
@name = gadget.name
end
end
class WidgetPresenter
attr_reader :id
attr_reader :name
attr_reader :children
def initialize(widget)
@id = %{widget-#{widget.id}}
@name = widget.name
@children = widget.gadgets.collect{|g| GadgetPresenter.new(g)}
end
end
w = Widget.new
w.id = 47
w.name = 'name of widget'
g = Gadget.new
g.id = 63
g.name = 'name of gadget'
w.gadgets = [g]
p WidgetPresenter.new(w).to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment