Ruby Gemにパッケージ化されたファイルにアクセスします
質問
私は宝石としてパッケージ化しているbuildr拡張機能を持っています。パッケージに追加したいスクリプトのコレクションがあります。現在、これらのスクリプトは、ファイルするために書いている大きなテキストブロックとして保存されています。直接コピーするか、読み返すことができる個々のファイルを持ちたいと思います。これらのファイルを宝石にパッケージ化したいと思います。パッケージ化に問題はありません(以前にファイルシステムに貼り付けるだけです rake install
)しかし、私はそれらにアクセスする方法を理解できません。宝石リソースバンドルタイプのものはありますか?
解決
基本的に2つの方法があります、
1)宝石のルビーファイルに関連するリソースを使用してロードできます __FILE__
:
def path_to_resources
File.join(File.dirname(File.expand_path(__FILE__)), '../path/to/resources')
end
2)宝石から任意のパスを追加できます $LOAD_PATH
変数、そして歩きます $LOAD_PATH
リソースを見つけるには、例えば
Gem::Specification.new do |spec|
spec.name = 'the-name-of-your-gem'
spec.version ='0.0.1'
# this is important - it specifies which files to include in the gem.
spec.files = Dir.glob("lib/**/*") + %w{History.txt Manifest.txt} +
Dir.glob("path/to/resources/**/*")
# If you have resources in other directories than 'lib'
spec.require_paths << 'path/to/resources'
# optional, but useful to your users
spec.summary = "A more longwinded description of your gem"
spec.author = 'Your Name'
spec.email = 'you@yourdomain.com'
spec.homepage = 'http://www.yourpage.com'
# you did document with RDoc, right?
spec.has_rdoc = true
# if you have any dependencies on other gems, list them thusly
spec.add_dependency('hpricot')
spec.add_dependency('log4r', '>= 1.0.5')
end
その後、
$LOAD_PATH.each { |dir| ... look for resources relative to dir ... }
所属していません StackOverflow