Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

class-level instance variables #246

Closed
skippy opened this issue Feb 1, 2015 · 3 comments
Closed

class-level instance variables #246

skippy opened this issue Feb 1, 2015 · 3 comments

Comments

@skippy
Copy link
Contributor

skippy commented Feb 1, 2015

hey folks,

I'm looking at using the base protobuf class and wrapping business logic around it in a separate class, using class inheritance. However, this doesn't work in this library:

require 'protobuf'
require 'protobuf/message'
module Corp
  module Protobuf
    class Error < ::Protobuf::Message
      required :string, :foo, 1
    end
  end
end
module Corp
  class ErrorHandler < Corp::Protobuf::Error
  end
end
# this works
Corp::Protobuf::Error.new(foo: :bar).encode #=> "\n\x03bar"
#this does not
Corp::ErrorHandler.new(foo: :bar).encode #=> ""

The root issue is within lib/protobuf/message/fields.rb and the fact that there is a somewhat hidden class instance variable @field_store and a bunch of class instance variables uses for memoization. The module doesn't define it as a class instance variable, obviously, but it extends the message class, which makes it a class instance variable.

Is this intended behavior? Do you think it is worth fixing? If so, and since active_support is already included, I think mattr_accessor will do the trick (I can submit a patch and find out :) And of course I can change my class to use the protobuf as an internal object and forgo inheritance all together.

thoughts? And thank you for the library!

@localshred
Copy link
Contributor

@skippy definitely not intended. I extracted a bunch of class-level methods from Message about a year ago and obviously didn't have a test for what you're doing. Would love to see a PR fixing it. 👍

@localshred
Copy link
Contributor

Fixed under v3.4.4.

@skippy
Copy link
Contributor Author

skippy commented Feb 27, 2015

thanks!

@skippy skippy closed this as completed Feb 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants