Successfully reported this slideshow.

One project, 3 gems

0

Share

Upcoming SlideShare
do i believe in god?
do i believe in god?
Loading in …3
×
1 of 27
1 of 27

More Related Content

One project, 3 gems

  1. 1. One project, three gems Mike Williams Cogent Consulting in association with
  2. 2. Architecture “Atlas” <stuff/>
  3. 3. arboreal Growing Trees (restaurant) (hotel) Melbourne Sydney Geelong Victoria NSW (sight) WA Australia New Zealand Pacific Europe Asia WORLD Americas Africa
  4. 4. arboreal how do I select this? Melbourne Sydney Geelong Victoria NSW WA Australia New Zealand Pacific Europe Asia WORLD Americas Africa
  5. 5. arboreal Path Enumeration @victoria = Place.find_by_name(“Victoria”) @victoria.ancestry_string #=> “-4-14-15-” Melbourne Sydney Geelong Victoria NSW Australia 15 WA New Zealand Pacific 14 Europe WORLD 4 Asia Americas Africa
  6. 6. arboreal @victoria.ancestors SELECT * FROM places WHERE (id in (4,14,15)) Melbourne Sydney Geelong Victoria NSW Australia 15 WA New Zealand Pacific 14 Europe WORLD 4 Asia Americas Africa
  7. 7. arboreal @australia = Place.find_by_name(“Australia”) @australia.children SELECT * FROM places WHERE (places.parent_id = 15) Melbourne Sydney Geelong Victoria NSW Australia 15 WA New Zealand Pacific Europe Asia WORLD Americas Africa
  8. 8. arboreal @australia.descendants SELECT * FROM places WHERE (places.ancestry_string like ‘-4-14-15-%’) “-4-14-15-22-” Melbourne Sydney “-4-14-15-16-” Geelong Victoria NSW “-4-14-15-” WA Australia New Zealand Pacific Europe Asia WORLD Americas Africa
  9. 9. arboreal @australia.subtree SELECT * FROM places WHERE (places.id = 15 OR places.ancestry_string like ‘-4-14-15-%’) “-4-14-15-22-” Melbourne Sydney “-4-14-15-16-” Geelong Victoria NSW “-4-14-15-” Australia 15 WA New Zealand Pacific Europe Asia WORLD Americas Africa
  10. 10. arboreal @australia.subtree.scope(:find, :conditions) #=> [“places.id = ? OR places.ancestry_string like ?”, 15, “-4-14-15-%”] class Place def contained_pois Poi.scoped(:include => :place, :conditions => subtree.scope(:find, :conditions)) end end @australia.contained_pois SELECT ... FROM pois LEFT OUTER JOIN places ON places.id = pois.place_id WHERE (places.id = 15 OR places.ancestry_string like ‘-4-14-15-%’)
  11. 11. arboreal @australia.contained_pois SELECT ... FROM pois LEFT OUTER JOIN places ON places.id = pois.place_id WHERE (places.id = 15 OR places.ancestry_string like ‘-4-14-15-%’) Melbourne Sydney Geelong Victoria NSW WA Australia New Zealand Pacific Europe Asia WORLD Americas Africa
  12. 12. GET http://stuff.local/places/15/pois “Atlas” <pois/>
  13. 13. <pois type="array"> <poi> <created-at type="datetime">2010-08-25T23:35:59Z</created-at> <email nil="true"></email> <id type="integer">6</id> <latitude type="decimal">-37.8002389992226</latitude> <longitude type="decimal">144.901535511017</longitude> <name>Footscray Market</name> <place-id type="integer">26</place-id> <review>A frenetic covered food market.</review> <type>See</type> <updated-at type="datetime">2010-08-25T23:35:59Z</updated-at> <url nil="true"></url> </poi> <poi> <created-at type="datetime">2010-08-25T23:35:59Z</created-at> <email nil="true"></email> <id type="integer">7</id> <latitude type="decimal">-37.8020224141169</latitude> <longitude type="decimal">144.983611106873</longitude> <name>Panama Dining Room</name> <place-id type="integer">26</place-id> <review>Franco-Fitzroy pub grub and ersatz Manhattan views.</review> <type>Eat</type> <updated-at type="datetime">2010-08-25T23:35:59Z</updated-at> <url nil="true"></url> </poi> ...
  14. 14. <pois type="array"> <poi href="http://stuff.local/pois/6"> <name>Footscray Market</name> <type>See</type> <review>A frenetic covered food market.</review> <place href="http://stuff.local/places/26"> <name>Melbourne</name> </place> </poi> <poi href="http://stuff.local/pois/7"> <name>Panama Dining Room</name> <type>Eat</type> <review>Franco-Fitzroy pub grub and ersatz Manhattan views.</review> <place href="http://stuff.local/places/26"> <name>Melbourne</name> </place> </poi> <poi href="http://stuff.local/pois/8"> <name>Sydney Opera House</name> <type>See</type> <review/> <place href="http://stuff.local/places/17"> <name>Sydney</name> </place> </poi> </pois>
  15. 15. # BEFORE ... xml.pois :type => “array” do @pois.each do |poi| xml.poi :href => poi_url(poi) do xml.name(poi.name) xml.type(poi.type) xml.review(poi.review) xml.place :href => place_url(poi.place) do xml.name poi.place.name end end end end
  16. 16. # BEFORE ... representative xml.pois :type => “array” do @pois.each do |poi| xml.poi :href => poi_url(poi) do xml.name(poi.name) xml.type(poi.type) xml.review(poi.review) xml.place :href => place_url(poi.place) do xml.name poi.place.name end end end end # AFTER ... Representative::Xml.new(xml) do |r| r.list_of :pois, @pois, :item_attributes => {:href => method(:poi_url)} do r.element :name r.element :type r.element :review r.element :place, :href => method(:place_url) do r.element :name end end end
  17. 17. # XML ... representative Representative::Xml.new(xml) do |r| r.list_of :pois, @pois, :item_attributes => {:href => method(:poi_url)} do r.element :name r.element :type r.element :review r.element :place, :href => method(:place_url) do r.element :name end end end # JSON ... Representative::Json.new do |r| r.list_of :pois, @pois, :item_attributes => {:href => method(:poi_url)} do r.element :name r.element :type r.element :review r.element :place, :href => method(:place_url) do r.element :name end end end.to_s
  18. 18. <pois type="array"> <poi href="http://stuff.local/pois/6"> <name>Footscray Market</name> <type>See</type> <review>A frenetic covered food market.</review> <place href="http://stuff.local/places/26"> <name>Melbourne</name> </place> </poi> <poi href="http://stuff.local/pois/7"> <name>Panama Dining Room</name> <type>Eat</type> <review>Franco-Fitzroy pub grub and ersatz Manhattan views.</review> <place href="http://stuff.local/places/26"> <name>Melbourne</name> </place> </poi> <poi href="http://stuff.local/pois/8"> <name>Sydney Opera House</name> <type>See</type> <review/> <place href="http://stuff.local/places/17"> <name>Sydney</name> </place> </poi> </pois>
  19. 19. representative [ { "name": "Footscray Market", "type": "See", "review": "A frenetic covered food market.", "place": { "name": "Melbourne" } }, { "name": "Panama Dining Room", "type": "Eat", "review": "Franco-Fitzroy pub grub and ersatz Manhattan views.", "place": { "name": "Melbourne" } }, { "name": "Sydney Opera House", "type": "See", "review": null, "place": { "name": "Sydney" } } ]
  20. 20. “Atlas” <pois/>
  21. 21. <pois/> something that wants POI data
  22. 22. <pois/> something fake that wants Atlas POI data
  23. 23. sham rack require ‘fakeweb’ FakeWeb.register_uri(:get, “http://www.example.com/hello”, :body => “Hello World!”) require ‘webmock’ stub_request(:get, “www.example.com/hello”).to_return(:body => “Hello, World!”) require ‘sham_rack’ ShamRack.at(“www.example.com”).stub.register_resource(“/hello”, “Hello, World!”)
  24. 24. sham rack It’s just Rack! client Net::HTTP Sham Rack Rack app
  25. 25. sham rack It’s just Rack! ShamRack.at("www.example.com”) do |env| [ "200 OK", { "Content-type" => "text/plain" }, "Hello, world!" ] end ShamRack.at("www.example.com").rackup do use Some::Middleware use Some::Other::Middleware run MyApp.new end
  26. 26. sham rack It’s just Rack! ShamRack.at("www.example.com").sinatra do get "/hello/:subject" do "Hello, #{params[:subject]}" end end
  27. 27. http://github.com/mdub/arboreal http://github.com/mdub/representative http://github.com/mdub/sham_rack

×