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

Custom methods not available on objects not created by HotCocoa #5

Open
ferrous26 opened this issue May 31, 2011 · 1 comment
Open
Labels

Comments

@ferrous26
Copy link
Member

HotCocoa::Mappings.map :net_service => NSNetService do
  def init_with_options service, opts
    service.initWithDomain 'local.', type:'_http._tcp.', name:'TEST', port:9999
  end

  custom_methods do
    def my_custom_method
      true
    end
  end
end

service = HotCocoa.net_service
service.respond_to?(:my_custom_method)  # => true

It returns true because the custom method is mixed in to the singleton class instead of the actual class.

other_service = NSNetService.alloc.initWithDomain 'local.', type:'_http._tcp.', name:'TEST', port:9999
other_service.respond_to?(:my_custom_method)  # => false

false is returned here because the custom methods are not applied to objects not created by HotCocoa.

Why should custom methods only apply to objects created by HotCocoa?

I think that custom methods should work everywhere. Mixing in methods only once to the class should be more efficient and would solve this issue. However, we need to be careful of inheritance and not break the lazy loading feature.

@ferrous26
Copy link
Member Author

The proper way to handle this is to subclass NSNetServer and then to include HotCocoa::Behaviors, which will mix in HotCocoa custom methods and delegate hooks.

However, there still exists the case where object is created by Cocoa and you have no control over that. For instance, when browsing for bonjour services, a new NSNetService will be created for you for each service that is found. It would still be nice if we could handle this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant