我想扩展rails的faker gem以生成其他随机日期(在我的情况下是计算机游戏名称)
#lib/extended_faker.rb
require 'faker'
require 'extended_faker/game'
#lib/extended_faker/game.rb
Module Faker
class Game < Faker::Base
class << self
def name
fetch('game.name')
end
end
end
end
#config/locals/faker_en.yml
en:
faker:
game:
name: ["a", "b", "c"]
#config/application.rb
...
config.autoload_paths += Dir["#{config.root}/lib/**/"]
...
然后当我在 Rails 控制台中运行它时,我得到以下信息
Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > Faker::Game.name
LoadError: Expected /home/enermis/School/Projects/IG/test/lib/extended_faker/game.rb to define Game
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:503:in `load_missing_constant'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:514:in `load_missing_constant'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing'
from (irb):1
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
当我将 lib/extended_faker/game.rb 文件更改为此
require 'faker'
require 'extended_faker/game'
include 'extended_faker/item'
include 'extended_faker/team'
我在控制台中出现奇怪的行为
Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > Faker::Game.name
=> "b"
1.9.3p194 :002 > Faker::Game.name
NameError: uninitialized constant Faker::Game
from (irb):2
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
我不明白的奇怪的事情是,我第一次运行生成器时,我得到了一个有效的结果,但第二次我得到了一个错误......
我错过了什么?