Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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