daily pastebin goal
64%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'script/environment.rb'
  2. $KCODE = 'u'
  3. clear!
  4.  
  5. Movie = Meta.new(:credits => %w[ Автор Актер Режиссер Исполнитель Год ]) do
  6.   has_many :comments
  7.   on_new_document do |doc|
  8.     doc[:credits] ||= []
  9.   end
  10.  
  11.   # :editor slot keeps track on who edited this particular version
  12.   before_save do |doc|
  13.     doc[:editor] ||= doc[:owner]
  14.   end
  15.  
  16.   # returns ordered credits in a form of:
  17.   # movie.credits == [ ["Artist", "Mylène Farmer"], ["Director", "Laurent Boutonnat"] ]
  18.   def credits
  19.     meta.credits.map do |s|
  20.       self[s] ? [s, self[s]] : nil
  21.     end.compact
  22.   end
  23. end
  24.  
  25. Comment = Meta.new
  26.  
  27. User = Meta.new do
  28.   on_new_document do |u|
  29.     u[:playlists] ||= []
  30.   end
  31. end
  32.  
  33. Playlist = Meta.new do
  34.   on_new_document do |pl|
  35.     pl[:items] ||= []
  36.   end
  37. end
  38.  
  39. oleg   = User.create!(:name => "Олег Андреев")
  40. yrashk = User.create!(:name => "Юрій Рашковський")
  41.  
  42. melancholie = Movie.create!(:owner        => oleg,
  43.                             :title        => "Je t'aime mélancolie")
  44.  
  45. desenchantee = Movie.create!(:owner       => oleg,
  46.                             :title        => "Désenchantée")
  47.  
  48. sanscontrefacon = Movie.create!(:owner    => yrashk,
  49.                             :title        => "Sans Contrefaçon")
  50.  
  51. # Oleg creates a favorites playlist
  52. my_favorites = Playlist.new(:owner => oleg,
  53.                             :title => "Favorite MF music videos")
  54.  
  55. my_favorites.items += [ melancholie, desenchantee ]
  56. my_favorites.save!
  57.  
  58. # Yura adds credits to movies
  59. melancholie["Исполнитель"]     = "Mylène Farmer"
  60. desenchantee["Исполнитель"]    = "Mylène Farmer"
  61. sanscontrefacon["Исполнитель"] = "Mylène Farmer"
  62. melancholie["Год"]     = 1991
  63. desenchantee["Год"]    = 1991
  64. sanscontrefacon["Год"] = 1987
  65. melancholie.save!
  66. desenchantee.save!
  67. sanscontrefacon.save!
  68.  
  69. # Oleg adds a comment to "Sans Contrefaçon"
  70.  
  71. Comment.create!(:text => "Обалдеть!!!111", :owner => oleg, :movie => sanscontrefacon)
  72.  
  73. p sanscontrefacon.comments.map{|c|c.text}
  74. # => ["Обалдеть!!!111"]
  75.  
  76.  
  77. # Find all Movies
  78. p Movie.find.map { |d|
  79.   "#{d.uuid[0,4]}: #{d.title}"
  80. }
  81. # => ["fd59: Je t'aime mélancolie", "66d0: Sans Contrefaçon", "9c4c: Désenchantée"]
  82.  
  83.  
  84. # Find all Oleg's documents
  85. p StrokeDB.default_store.search(:owner => oleg).map { |d|
  86.   "#{d.meta.name}:#{d.uuid[0,4]}"
  87. }
  88. # => ["Movie:fd59", "Movie:9c4c", "Playlist:8acc", "Comment:8306"]
  89.  
  90. p sanscontrefacon.credits
  91. # => [["Исполнитель", "Mylène Farmer"], ["Год", 1987]]
  92.  
  93. # Find all 1991 year movies
  94. p StrokeDB.default_store.search("Год" => 1991).map { |d| d.title }
  95. # => ["Désenchantée", "Je t'aime mélancolie"]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top