From 2f611742ba60d120909f4a426251d7dcf0fdde07 Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Thu, 7 Apr 2011 00:59:50 +0400 Subject: [PATCH] Rails BeatDB draft --- .../app/controllers/application_controller.rb | 1 + rails/app/helpers/application_helper.rb | 13 ++++++++ rails/app/models/beatdb.rb | 33 +++++++++++++++++++ rails/app/views/artist/view.rhtml | 6 ++-- rails/config/application.rb | 2 ++ rails/config/boot.rb | 2 ++ 6 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 rails/app/models/beatdb.rb diff --git a/rails/app/controllers/application_controller.rb b/rails/app/controllers/application_controller.rb index 5b25c37..f711163 100644 --- a/rails/app/controllers/application_controller.rb +++ b/rails/app/controllers/application_controller.rb @@ -1,5 +1,6 @@ class ApplicationController < ActionController::Base protect_from_forgery + require 'beatdb' require 'pp' #require 'cobravsmongoose' end diff --git a/rails/app/helpers/application_helper.rb b/rails/app/helpers/application_helper.rb index de6be79..a5d2739 100644 --- a/rails/app/helpers/application_helper.rb +++ b/rails/app/helpers/application_helper.rb @@ -1,2 +1,15 @@ module ApplicationHelper end + +class Numeric + def toSeconds + (self / 1000).round + end + + def toTime + s = self.toSeconds + m = (s / 60).floor + s -= m * 60 + m.to_s << ':' << (s < 10 ? '0' : '') << s.to_s + end +end \ No newline at end of file diff --git a/rails/app/models/beatdb.rb b/rails/app/models/beatdb.rb new file mode 100644 index 0000000..94102ce --- /dev/null +++ b/rails/app/models/beatdb.rb @@ -0,0 +1,33 @@ +class BeatDB + @@db_root = '/www/beatdb' + + def self.get key + unless self.exists(key) || File.readable?(self.filePathByKey(key)) + return false + end + h = File.open(self.filePathByKey(key), 'r') + JSON.decode(h.readline) + end + + def self.set key, data + unless self.exists(key) || File.writable?(self.filePathByKey(key)) + return false + end + h = File.open(self.filePathByKey(key), 'w') + h.puts(JSON.encode(data)) + end + + def self.delete key + + end + + def self.exists key + File.exists?(self.filePathByKey(key)) + end + + def self.filePathByKey key + path = '' + key.to_s.each_char {|c| path << '/' + c} + @@db_root + path[0..(path.length-2)] << key.to_s + '.json' + end +end \ No newline at end of file diff --git a/rails/app/views/artist/view.rhtml b/rails/app/views/artist/view.rhtml index 7468de4..231b9d0 100644 --- a/rails/app/views/artist/view.rhtml +++ b/rails/app/views/artist/view.rhtml @@ -1,9 +1,9 @@

<%= @artist.name %>

<% @artist.albums.each do |album| %> - <% if album.releases.length > 0 %> + <% unless album.releases.empty? %>
-

<%= album.name %> (<%= album.year %>)

+

<%= album.name %> <%= (album.year ? album.year : '') %>

<% releases = album.releases first_release = releases.first @@ -13,7 +13,7 @@
  • <%= track.name %> - <%= track.length %> + <%= track.length.toTime %>
  • <% end %> diff --git a/rails/config/application.rb b/rails/config/application.rb index b716dd1..07f1035 100644 --- a/rails/config/application.rb +++ b/rails/config/application.rb @@ -38,5 +38,7 @@ module Beathaven # Configure sensitive parameters which will be filtered from the log file. config.filter_parameters += [:password] + + #config.autoload_paths += %W(#{config.root}/lib/db) end end diff --git a/rails/config/boot.rb b/rails/config/boot.rb index 4489e58..ca05e0f 100644 --- a/rails/config/boot.rb +++ b/rails/config/boot.rb @@ -1,6 +1,8 @@ +#utf-8 require 'rubygems' # Set up gems listed in the Gemfile. ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) +require 'json' \ No newline at end of file