开发者

My class method is not called, and I think it's related to my directory structure

After I restructured some files, I'm getting an error. When I call

@batch << Formatter.get(@fields)

it returns @fields. When I try to use debugger to go into the Formatter.get method, I see that it is skipped.

I have a directory structure like:

  • lib/
    • klass/
      • formatter.rb
      • formatter/
        • formatter.rb
        • foo_formatter.rb
        • bar_formatter.rb

lib/klass/formatter.rb contains:

require 'formatter/formatter'
require 'formatter/foo_formatter'
require 'formatter/bar_formatter'
module Klass
  cl开发者_如何学编程ass Formatter
  end
end

And lib/klass/formatter/formatter.rb contains:

module Klass
    class Formatter

        attr_accessor :fields

        def self.get fields
            case fields[:field_id]
            when "foo"; FooFormatter.new fields
            when "bar"; BarFormatter.new fields
            end
         end

lib/klass/formatter/foo_formatter.rb contains:

module Klass
    class FooFormatter < Formatter


You will need to change the namespace of lib/klass/formatter/formatter.rb so that it reads

module Klass::Formatter
    class Formatter

        attr_accessor :fields

        def self.get fields
            case fields[:field_id]
            when "foo"; FooFormatter.new fields
            when "bar"; BarFormatter.new fields
            end
         end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜