2015-01-16 50 views
1

我有一個Ruby gem which gets used across multiple projects包含一些Mongoid模型。猴子修補包含在Ruby寶石中的Mongoid模型

我目前正試圖在項目中重複使用它們,而猴子則修補了一些額外的方法。但是,當我需要項目並運行測試時。我得到一個NoMethodError。任何想法,我要去錯了嗎?

這裏是我的主項目文件:

require 'bundler' 
Bundler.require(:default) 

require 'mongoid-elasticsearch' 
Mongoid::Elasticsearch.prefix = ENV["MONGOID_ENVIRONMENT"] 

require 'mongoid_address_models/require_all' # This is where I include my gem 

Mongoid.load!(File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml"), ENV["MONGOID_ENVIRONMENT"] || :development) 

# Here are my monkey patched models 
require 'models/street' 
require 'models/locality' 
require 'models/town' 
require 'models/postcode' 

require 'sorting_office/address' 

module SortingOffice 
end 

這是猴子修補模型

class Postcode 

    REGEX = /([A-PR-UWYZ01][A-Z01]?[0-9IO][0-9A-HJKMNPR-YIO]\s?[0-9IO][ABD-HJLNPQ-Z10]{2})/i 

    def self.calculate(address) 
    postcode = UKPostcode.new(address.match(REGEX)[0]) 
    where(name: postcode.norm).first 
    end 

end 

之一的例子當我打電話Postcode.calculate(地址)(例如)我得到一個NoMethodError

回答

0

我想我現在已經釘住了這一點。而不是把猴子補丁裏面lib/models,我把它們搬到lib/sorting_office/models,要求他們像這樣:

require 'sorting_office/models/street' 
require 'sorting_office/models/locality' 
require 'sorting_office/models/town' 
require 'sorting_office/models/postcode' 

我仍然有興趣知道爲什麼這個工作雖然