Guest User

Untitled

a guest
Mar 14th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. ## Rails ActiveRecord
  2.  
  3. class Playlist
  4. serialize :items_ids
  5. def videos(offset = nil, limit = nil)
  6. ids = item_ids || []
  7. vs = Video.find(:all, :conditions => ["id in (?)", ids],
  8. :limit => limit, :offset => offset)
  9. hvs = Hash[*(vs.map{|v| [v.id.to_i, v] }.flatten)]
  10. ids.map{|i| hvs[i.to_i] }
  11. end
  12. def add_item(v)
  13. self.items_id ||= []
  14. items_id.unshift(v.id)
  15. v.playlist_id = self.id
  16. v.save!
  17. end
  18. end
  19.  
  20. list = Playlist.new
  21. list.add_item(some_video)
  22. list.videos(0, 2) #=> [ #<Video ...> ]
  23.  
  24. ## StrokeDB
  25.  
  26. Playlist = Meta.new do
  27. on_initialize do |doc|
  28. doc["videos"] ||= []
  29. end
  30. end
  31.  
  32. list = Playlist.new
  33. list.videos << some_video # store reference
  34. list.videos[0, 2] #=> [ #<Video ...> ] (auto dereferencing)
Add Comment
Please, Sign In to add comment