razorhead
razorhead

Reputation: 319

ActiveSupport's class_attribute undefined

I'm comfortable using Rails but I wanted to learn more about Ruby outside of the framework and thought about writing a gem for a particular problem. I started picking apart of couple (dynamoid and mongoid) to follow their pattern. I've fallen at the first and would welcome some help.

I have a module which looks like this:

module MyModule
  module Document
    extend ActiveSupport::Concern
    include Components

    included do
      class_attribute :foo
    end
  end
end

This imports another module which just a convince bag for extensions

module MyModule
  module Components
    include Fields
  end
end

and finally includes another module which looks like this:

module MyModule
  module Fields
    extend ActiveSupport::Concern

    included do
      class_attribute :bar
    end
  end
end

This follows the pattern of the other gems.

The class_attribute call in MyModule::Document works fine, as I would expect.

My problem is that I'm getting a undefined method `class_attribute' for MyModule::Components:Module (NoMethodError) in the Fields module. Now, this seems to make sense in that MyModule::Fields is being included in a module, not a class, and module doesn't have a class_attribute method. I just can't see how these other two gems are performing this trick or if there's an idiom I'm missing?

Upvotes: 3

Views: 2558

Answers (1)

Andrew Schwartz
Andrew Schwartz

Reputation: 4657

You need to get your class MyModule::Document to be the includer of MyModule::Fields rather than the module MyModule::Components. Would this work?

module MyModule
  module Components
    extend ActiveSupport::Concern
    included do
      include Fields
    end
  end
end

Upvotes: 2

Related Questions