Module: FoursquareDocument::ClassMethods

Defined in:
lib/foursquare_document.rb

Instance Method Summary collapse

Instance Method Details

#foursquare_id(symbol, index = false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/foursquare_document.rb', line 6

def foursquare_id(symbol,index=false)
  field symbol, :type => Moped::BSON::ObjectId


  if index
    index({symbol => 1})
  end
  
  attr_accessible symbol, :foursquare_cache
  accepts_nested_attributes_for :foursquare_cache

  class_eval <<-RUBY, __FILE__, __LINE__+1
    define_callbacks :add_foursquare_info

    set_callback(:save,:after) do |doc|
        if doc.#{symbol}_changed? &&
        (doc.foursquare_cache.blank? || !doc.foursquare_cache.changed?)
          doc.add_foursquare_info
        end
    end


    def add_foursquare_info
      run_callbacks :add_foursquare_info do
        client = FoursquareDocument::Config::get_client
        venue_info = client.venue(self.#{symbol})
        self.foursquare_cache = FoursquareCache.new(venue_info)
        self.foursquare_cache.save!         
      end
    end
  RUBY
end