2011-10-19 10 views
8

में पैक की गई फ़ाइलों तक पहुंचने के लिए मेरे पास एक बिल्डर एक्सटेंशन है जिसे मैं एक मणि के रूप में पैकेजिंग कर रहा हूं। मेरे पास स्क्रिप्ट का संग्रह है जिसे मैं एक पैकेज में जोड़ना चाहता हूं। वर्तमान में, मेरे पास ये स्क्रिप्ट एक बड़े टेक्स्ट ब्लॉक के रूप में संग्रहीत हैं जिन्हें मैं फ़ाइल में लिख रहा हूं। मैं व्यक्तिगत फाइलें रखना पसंद करूंगा जो मैं या तो सीधे कॉपी या पढ़/लिख सकता हूं। मैं इन फ़ाइलों को मणि में पैक करना चाहता हूं। मुझे उन्हें पैकेजिंग में कोई समस्या नहीं है (बस उन्हें rake install से पहले फ़ाइल सिस्टम में चिपकाएं) लेकिन मैं यह नहीं समझ सकता कि उन्हें कैसे पहुंचाया जाए। क्या एक जेम संसाधन बंडल प्रकार की बात है?रुबी जेम

उत्तर

16

वहाँ मूलतः दो तरीके,

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 = '[email protected]' 
    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 ... } 
+0

पहला व्यक्ति आकर्षण की तरह काम करता था। :) – Drew

+0

सही पथ खोजने के लिए कृपया Gem.data_dir का उपयोग करें। – ch2500