Rails プロジェクトに rb-scpt gem をインストールし、バンドルを実行しました。gem は rb-appscript からのフォークです。
次のプログラムは、ターミナルから実行すると機能します。
#!/usr/bin/env ruby
# List names of playlists in iTunes.
require "rb-scpt"
itu = Appscript.app("iTunes")
p itu.sources[1].user_playlists.name.get
同じことを行うコントローラーを作成しました。
class ItunesController < ApplicationController
def play
itu = ::Appscript.app('iTunes')
play_list = itu.sources[1].user_playlists.name.get
render text(play_list)
end
end
実行すると、3 行目でエラーが発生します。
#<NameError: uninitialized constant ItunesController::Appscript>
私は何が欠けていますか?シンプルなものだとおもいます。
IDE として RubyMine を使用しています。::Appscript. と入力すると、ソースが Appscript であるという IDE からのコードヒントが表示され始めます。これは、gem の rb-scpt.rb からの抜粋です。
module Appscript
...
class GenericApplication < GenericReference
def initialize(app_class)
@_app_class = app_class
super(['app'])
end
def by_name(name, terms=true)
return @_app_class.by_name(name, terms)
end
def by_id(id, terms=true)
return @_app_class.by_id(id, terms)
end
def by_creator(creator, terms=true)
return @_app_class.by_creator(creator, terms)
end
def by_pid(pid, terms=true)
return @_app_class.by_pid(pid, terms)
end
def by_url(url, terms=true)
return @_app_class.by_url(url, terms)
end
def by_aem_app(aem_app, terms=true)
return @_app_class.by_aem_app(aem_app, terms)
end
def current(terms=true)
return @_app_class.current(terms)
end
end
#######
AS_App = Appscript::GenericApplication.new(Application)
AS_Con = Appscript::GenericReference.new(['con'])
AS_Its = Appscript::GenericReference.new(['its'])
######################################################################
# REFERENCE ROOTS
######################################################################
# public (note: Application & GenericApplication classes may also be accessed if subclassing Application class is required)
def Appscript.app(*args)
if args == []
return AS_App
else
return AS_App.by_name(*args)
end
end