simpleconsoleã§1è¡ã¡ã¢å¸³
http://d.hatena.ne.jp/shokai/20081126/1227721485 ã®ç¶ããSimpleConsoleã§æ軽ã«ä½¿ãã1è¡ã¡ã¢å¸³ãä½ã£ã
ã³ãã³ãå¼æ°ããControllerãé¸ãã§Viewã«æµãããã¿ã¯SimpleConsoleãä½ã£ã¦ãããã®ã§ãmodelãActiveRecordã§å®è£
ãã¦ã¿ãã
ã§ãããã®â ã¨ããããèªã¿æ¸ãåé¤ã¯ã§ããããã®ãã¡ã¡ã¼ã«ã«åãåºãæ©è½ãªã©ãä»ããã
å¼æ°ç¡ãã§ååèµ·åæã« ~/.mem/db ãä½ããã
sho@shomb% mem initialize db.. == CreatePosts: migrating ==================================================== -- create_table(:posts) -> 0.0585s == CreatePosts: migrated (0.0589s) =========================================== success! usage: mem post -m "ã¡ã¢ã¡ã¢" mem log :postã®è¡¨ç¤º mem head :ææ°ã®post mem delete_all :åé¤ mem delete -id 1 :IDãæå®ãã¦åé¤ mem init_db (~/.mem/dbãåé¤ãã¦ãã) sho@shomb% mem
post -m æ¬æ ã§æ¥æä»ãã§ã©ãã©ãæ¸ãã
sho@shomb% mem post -m "ã¦ãã¨ã¦ãã¨" posted: ã¦ãã¨ã¦ã㨠(1:mem_console@Thu Nov 27 08:30:44 +0900 2008) ã¦ãã¨ã¦ã㨠sho@shomb% mem post -m "postã§ããã" posted: postã§ããã (2:mem_console@Thu Nov 27 08:30:53 +0900 2008) postã§ããã (1:mem_console@Thu Nov 27 08:30:44 +0900 2008) ã¦ãã¨ã¦ã㨠sho@shomb% mem post -m ã»ããã posted: ã»ããã (3:mem_console@Thu Nov 27 08:30:59 +0900 2008) ã»ããã (2:mem_console@Thu Nov 27 08:30:53 +0900 2008) postã§ããã (1:mem_console@Thu Nov 27 08:30:44 +0900 2008) ã¦ãã¨ã¦ã㨠sho@shomb% mem log [~] (3:mem_console@Thu Nov 27 08:30:59 +0900 2008) ã»ããã (2:mem_console@Thu Nov 27 08:30:53 +0900 2008) postã§ããã (1:mem_console@Thu Nov 27 08:30:44 +0900 2008) ã¦ãã¨ã¦ãã¨
logã§å
¨postãheadã§ææ°10post表示
sho@shomb% mem head [~] (11:mem_console@Thu Nov 27 08:35:22 +0900 2008) headã¯10postã ã表示 (10:mem_console@Thu Nov 27 08:35:09 +0900 2008) 10postç® (9:mem_console@Thu Nov 27 08:34:59 +0900 2008) ç ãã (8:mem_console@Thu Nov 27 08:34:49 +0900 2008) é¤ (7:mem_console@Thu Nov 27 08:34:45 +0900 2008) å (6:mem_console@Thu Nov 27 08:34:40 +0900 2008) ã® (5:mem_console@Thu Nov 27 08:34:36 +0900 2008) ç (4:mem_console@Thu Nov 27 08:34:31 +0900 2008) å° (3:mem_console@Thu Nov 27 08:30:59 +0900 2008) ã»ããã (2:mem_console@Thu Nov 27 08:30:53 +0900 2008) postã§ããã sho@shomb% mem log [~] (11:mem_console@Thu Nov 27 08:35:22 +0900 2008) headã¯10postã ã表示 (10:mem_console@Thu Nov 27 08:35:09 +0900 2008) 10postç® (9:mem_console@Thu Nov 27 08:34:59 +0900 2008) ç ãã (8:mem_console@Thu Nov 27 08:34:49 +0900 2008) é¤ (7:mem_console@Thu Nov 27 08:34:45 +0900 2008) å (6:mem_console@Thu Nov 27 08:34:40 +0900 2008) ã® (5:mem_console@Thu Nov 27 08:34:36 +0900 2008) ç (4:mem_console@Thu Nov 27 08:34:31 +0900 2008) å° (3:mem_console@Thu Nov 27 08:30:59 +0900 2008) ã»ããã (2:mem_console@Thu Nov 27 08:30:53 +0900 2008) postã§ããã (1:mem_console@Thu Nov 27 08:30:44 +0900 2008) ã¦ãã¨ã¦ãã¨
delete -i idã§IDãæå®ãã¦poståé¤ãdelete_allã§å
¨åé¤
sho@shomb% mem delete -i 5 [~] sho@shomb% mem log [~] (11:mem_console@Thu Nov 27 08:35:22 +0900 2008) headã¯10postã ã表示 (10:mem_console@Thu Nov 27 08:35:09 +0900 2008) 10postç® (9:mem_console@Thu Nov 27 08:34:59 +0900 2008) ç ãã (8:mem_console@Thu Nov 27 08:34:49 +0900 2008) é¤ (7:mem_console@Thu Nov 27 08:34:45 +0900 2008) å (6:mem_console@Thu Nov 27 08:34:40 +0900 2008) ã® (4:mem_console@Thu Nov 27 08:34:31 +0900 2008) å° (3:mem_console@Thu Nov 27 08:30:59 +0900 2008) ã»ããã (2:mem_console@Thu Nov 27 08:30:53 +0900 2008) postã§ããã (1:mem_console@Thu Nov 27 08:30:44 +0900 2008) ã¦ãã¨ã¦ã㨠sho@shomb% mem delete_all [~] sho@shomb% mem log [~] no posts
simcon singleã§generateããããã¡ã¤ã«ãç·¨éãããã®
activerecordã®modelã¯post.rbã«ãSQLiteã¨ã®æ¥ç¶ãªã©ã¯db.rbã«æ¸ããã
postããå¾ã«headãåºãã¨ããheadã¨logã§Viewãå
±éã«ããã¨ããã§ãã¦æ¥½ã ã£ããã
http://www.bitbucket.org/shokai/mem/src/77f8aeacf76d/mem
#!/usr/bin/env ruby require "rubygems" require "simpleconsole" require "activerecord" require File.dirname(__FILE__) + "/post.rb" require File.dirname(__FILE__) + "/db.rb" class Controller < SimpleConsole::Controller params :string => {:m => :message}, :int => {:i => :id} def default Data.connect_db init_db if !File.exist?(Data.db_path) end def post @message = params[:message] or exit(0) Data.connect_db Post.create(:message => @message, :from => 'mem_console') @posts = head end def log Data.connect_db @posts = Post.find(:all, :order=>"updated_at desc") end def head Data.connect_db @posts = Post.find(:all, :order=>"updated_at desc", :limit => 10) end def delete_all Data.connect_db Post.delete_all end def delete Data.connect_db @id = params[:id] @delete_num = Post.delete(@id) end def init_db Data.connect_db Data.init_db # åæå end end class View < SimpleConsole::View def usage puts <<EOF usage: mem post -m "ã¡ã¢ã¡ã¢" mem log :postã®è¡¨ç¤º mem head :ææ°ã®post mem delete_all :åé¤ mem delete -id 1 :IDãæå®ãã¦åé¤ mem init_db (~/.mem/dbãåé¤ãã¦ãã) EOF end def default usage end def post puts 'posted: ' + @message head end def log if @posts.length < 1 puts 'no posts' return end @posts.each{ |post| puts post.to_s } end def head log end def delete puts 'error: ID not found' if @delete_num < 1 end def init_db puts 'finished' end end SimpleConsole::Application.run(ARGV, Controller, View)
Post modelã®ã¯ã©ã¹
http://www.bitbucket.org/shokai/mem/src/77f8aeacf76d/post.rb
# model class Post < ActiveRecord::Base def to_s return "(#{id}:#{from}@#{updated_at}) #{message}" end end
DBã¨ã®ããã¨ããä»»ããã¯ã©ã¹
http://www.bitbucket.org/shokai/mem/src/77f8aeacf76d/db.rb
class CreatePosts < ActiveRecord::Migration def self.up create_table(:posts){|t| t.string :message t.string :from t.timestamps } end def self.down drop_table :posts end end class Data home = ENV['HOME'] || ENV['USERPROFILE'] || ENV['HOMEPATH'] @dbdir = home+'/.mem/' @dbfile = @dbdir+'db' def Data.db_path return @dbfile end def Data.connect_db if !ActiveRecord::Base.connected? ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :dbfile => @dbfile) end end def Data.init_db puts 'initialize db..' begin Dir::mkdir(@dbdir) if !File.exist?(@dbdir) CreatePosts.migrate(:up) rescue puts 'error' exit(0) else puts 'success!' end end end