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
- klass/
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
精彩评论