开发者

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

开发者 https://www.devze.com 2023-03-10 10:35 出处:网络
After I restructured some files, I\'m getting an error. When I call @batch << Formatter.get(@fields)

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

精彩评论

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