2012年9月29日土曜日

rubymotion用ライブラリの作り方

ライブラリのコードは予め書いておき、またrubygemsに公開するなら、
そのアカウントを取得しておく。

適当なフォルダで
# bundle gem motion-hogehoge
で、gemの雛形を作成

.gemspecに必要な項目を入力。
依存ライブラリがある場合は、ここに
gem.add_dependency "bubble-wrap", "~>1.1.4"
などと追記しておく。

次に、lib/motion-hogehoge.rbというファイルができているので、ここを修正。
rubymotionではrequireが対応していないので、rubymotion用に書き直す必要がある。
以下がサンプル

  1. unless defined?(Motion::Project::Config)  
  2.   raise "This file must be required within a RubyMotion project Rakefile."  
  3. end  
  4.   
  5. Motion::Project::App.setup do |app|  
  6.   Dir.glob(File.join(File.dirname(__FILE__), 'motion-hogehoge/*rb')).each do |file|  
  7.     app.files.unshift(file)  
  8.   end  
  9. end  

ライブラリのコードは、lib/motion-hogehoge/以下に入れておく。

rubygemsに登録する場合は、テストが通ってる必要がある。
とりあえず通すだけなら、以下でOK。

app/app_delegate.rbに
  1. class AppDelegate  
  2.   def application(application, didFinishLaunchingWithOptions:launchOptions)  
  3.     @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)  
  4.     @window.rootViewController = UIViewController.alloc.init  
  5.     @window.makeKeyAndVisible  
  6.     true  
  7.   end  
  8. end  
spec/main_spec.rbに
  1. describe "Application 'modeltest'" do  
  2.   before do  
  3.     @app = UIApplication.sharedApplication  
  4.   end  
  5.   
  6.   it "has one window" do  
  7.     @app.windows.size.should == 1  
  8.   end  
  9. end  

で、
#bundle install
#rake spec
でテストが通ることを確認する。

問題がなければ、
gem build motion-hogehoge.gemspec
で、gemファイルを作成し、

gem push motion-hogehoge-0.0.1.gem
で、rubygemsにアップする。

アップするときに取得したアカウント情報が聞かれるので入力すればOK。

2012年9月28日金曜日

Railsとjsonでやり取りするmotion-rails-modelというrubymotion用ライブラリを作ってみた

Railsと連携する際、babble-wrapを使っていたのですが、
取得先が複数になったときに面倒だったのでラッパーライブラリを作ってみました。
Active Record風のメソッドで、Rest風にアクセスをします。

github

使い方
rails側はindex,show,create,update,destoryをjsonで返信するようにします。 アプリ側では適当にmodelクラスを作って、motion-rails-modelを継承し、
attr_accessorとattributes_updateに項目を追加します。
  1. class Entries < RM::Model  
  2.   attr_accessor :title:description  
  3.     
  4.   def attributes_update(json)  
  5.     @title          = json['title']  
  6.     @description    = json['description']  
  7.     super  
  8.   end  
  9. end  
利用するときは、まずurlを設定します。
  1. class AppDelegate  
  2.   def application(application, didFinishLaunchingWithOptions:launchOptions)  
  3.     RM::Model.set_url("http://localhost:3000/")  
  4.   
  5.     @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)  
  6.     @window.rootViewController = RootViewController.alloc.init  
  7.     @window.makeKeyAndVisible  
  8.     true  
  9.   end  
  10. end  
あとは、
  1. Entries.all do |x|  
  2.   if x  
  3.     p x  
  4.   else  
  5.     p "error"  
  6.   end  
  7. end  
で、
http://localhost:3000/entriesにアクセスし
  1. Entries.find(1) do |x|  
  2.   if x  
  3.     p x  
  4.   else  
  5.     p "error"  
  6.   end  
  7. end  
で、
http://localhost:3000/entries/1
にアクセス。
  1. @entry = Entries.new  
  2. @entry.title = "title"  
  3. @entry.description = "description"  
  4. @entry.save do |x|  
  5.   if x  
  6.     p x  
  7.   else  
  8.     p "error"  
  9.   end  
  10. end  
で、
http://localhost:3000/entries
にpost。
  1. @entry.title = "title2"  
  2. @entry.save do |x|  
  3.   if x  
  4.     p x  
  5.   else  
  6.     p "error"  
  7.   end  
  8. end  
で、http://localhost:3000/entries/1
にput。
  1. @entry.destory do |x|  
  2.   if x  
  3.     p x  
  4.   else  
  5.     p "error"  
  6.   end  
  7. end  
で、http://localhost:3000/entries/1
にdeleteでアクセスするようになってます。

TODOとして
attributes_updateを設定しなくてもいいようにする。 エラー処理をもう少ししやすく。

2012年9月26日水曜日

現在のフォルダにあるファイルをリネームする

  1. files = Dir::entries(Dir::pwd)  
  2.   
  3. files.each do |f|  
  4. File.rename(f, f.sub(/aaa-/, '')) if f =~ /aaa-/  
  5. end  

csvを読み込んで、別のcsvの項目のうち2つ一致した項目を表示する

  1. # -*- coding: utf-8 -*-  
  2. require 'csv'  
  3.   
  4. a_csvs=[]  
  5. b_csvs=[]  
  6. succsess = []  
  7. failed = []  
  8. a_csvs_tmp = CSV.open('a.csv''r')  
  9. b_csvs_tmp = CSV.open('b.csv''r')  
  10.   
  11.   
  12. a_csvs_tmp.each{|x| a_csvs << x }  
  13. b_csvs_tmp.each{|x| b_csvs << x }  
  14.   
  15. a_csvs.each do |x|  
  16. a = b_csvs.select{|b| x[0] == b[0] and x[1] == b[1] }  
  17. unless a.empty?  
  18.   succsess << a  
  19. else  
  20.   failed << x  
  21. end  
  22. end  
  23.   
  24. puts "見つかったモノ"  
  25. p succsess.flatten  
  26. puts "失敗したモノ"  
  27. p failed.flatten