开发者

Call a custom getter method from within initialize

开发者 https://www.devze.com 2023-02-27 12:11 出处:网络
I am trying to create a class that does a couple of things: 1) Implements a custom getter for an attribute

I am trying to create a class that does a couple of things: 1) Implements a custom getter for an attribute 2) Calls the custom getter from within the initialize method

Here's what it looks like:

class Book

  # RSolr lib for interacting with Solr
  require 'rsolr' 

  # Instance variables 
  @isbn
  @title

  # Solr playlist instance URL
  @solr_domain
  @solr

  def initialize(isbn)
    @solr_domain = "http://solr.com:9003/solr"
    @solr = RSolr.connect :url => @solr_domain
    @isbn = isbn
    @title = self开发者_如何学C.title(isbn)
  end

  # Get Solr URL
  def solr_domain
    return @solr_domain
  end

# Set Solr URL and reset Solr connection object
  def solr_domain(newurl)
    @solr_domain = newurl
    @solr = RSolr.connect :url => @solr_domain
  end

  # Custom getter for title
  def title=(isbn)
    result = solr.get 'select', :params => {:q => 'isbn:(' + isbn + ')'}
    return result["response"]["docs"][0]["title"]
      end
end

The key lines are

@title = self.title(isbn)

where we attempt to call the getter for title, so that title gets set when the object is initialized.

What we want is a publicly accessible getter for title, as well as a way to populate @title during initialization of the object.


For being able to preset the title, you could do something like this:

def initialize(isbn, user_title = nil)
  ...
  @title = user_title || self.title(isbn)
end

There's an optional argument the user can supply. If he/she does, @title will get set to that value, otherwise we'll look up the title ourselves with the ISBN. I hope I understood you correctly...

Edit: BTW, are you sure you want class level instance variables?

http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/


I am not exactly sure what you are trying to do, but I feel that you are confusing instance variables of a class and instance variables of an instance. If you directly write @... in the class body, that will be the former, if you write @... within an instance method definition, it will be the latter. It seems you just want the latter. The following is probably what you want. Here, I set solr_domain and title as getters, so within some external class A, for some instance of Book b, you can do b.solr_domain or b.title to get them. Within Book, you do not need to access variables through getters. Directly accessing them by their names @... is less confusing and hence will likely decrease bugs.

class Book
  require 'rsolr' 
  attr_reader :solr_domain, :title
  def initialize(isbn)
    set_solr_domain("http://solr.com:9003/solr")
    @isbn = isbn
    @title = set_title
  end
  def set_solr_domain(newurl)
    @solr_domain = newurl
    @solr = RSolr.connect(:url => @solr_domain)
  end
  def set_title
    @solr.get('select', :params => {:q => "isbn:(#@isbn)"})["response"]["docs"][0]["title"]
  end
end
0

精彩评论

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