扩展Faker gem本地未初始化的常量Faker :: xxx

时间:2013-01-08 18:00:35

标签: ruby ruby-on-rails-3 faker

我想扩展faker gem for rails也生成其他随机日期(在我的情况下是电脑游戏名称)

#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>'

奇怪的是我不明白的是,第一次运行生成器时,我得到了一个有效的结果,但是第二次我得到了一个错误......

我缺少什么?

1 个答案:

答案 0 :(得分:1)

你应该把它放进去 lib/faker/game.rb
从lib目录开始,rails约定是最外层的模块名称 - &gt; in是文件夹名称。然后实际的类/模块名称是文件名,强调为camelcase 另一个例子

module Foo
  module Bar
    class BazParty
      def self.hello
        puts "hello"
      end
    end
  end
end

将进入lib/foo/bar/baz_party.rb