Module: KingpinInstanceMethods

Defined in:
lib/king_pin.rb

Overview

KingpinInstanceMethods - provides models new instance methods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



21
22
23
# File 'lib/king_pin.rb', line 21

def self.included(base)
  base.extend KingpinClassMethods
end

Instance Method Details

#add_pinObject

add a Pincaster record for self with in layer: self.class



26
27
28
# File 'lib/king_pin.rb', line 26

def add_pin
  Pincaster.add_record(self)
end

#additional_attributesObject

returns additional attributes of self that shall be included into the pin



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/king_pin.rb', line 88

def additional_attributes
  return {} unless !!self.class.kingpin_args[:include]
  case self.class.kingpin_args[:include].class.to_s
  when "Symbol"
    raise ":all is the only stand alone symbol that is allowed with :include" unless self.class.kingpin_args[:include] == :all
    self.attributes
  when "Hash"
    if self.class.kingpin_args[:include].size > 1 or not [:only, :except].include? self.class.kingpin_args[:include].first.first
      raise ":include supports :only => [:foo, :bar] and :except => [:scooby, :doo] only"
    end
    case self.class.kingpin_args[:include].first.first
    when :only
      self.attributes.delete_if{ |name, value| !self.class.kingpin_args[:include].first[1].include?(name.to_sym) }
    when :except
      self.attributes.delete_if{ |name, value| self.class.kingpin_args[:include].first[1].include?(name.to_sym) }
    end
  else
    raise ":include needs :all, {:only => [:foo, :bar]} or {:except => [:scooby, :doo]}"
  end
end

#autopinObject

automatically adds a pin via after_save hook for self in case autopin option was set to true for self’s AR model



83
84
85
# File 'lib/king_pin.rb', line 83

def autopin
  self.add_pin
end

#delete_pin!Object

deletes the Pincaster pin for self



36
37
38
# File 'lib/king_pin.rb', line 36

def delete_pin!
  Pincaster.delete_record(self)
end

#nearby(radius, limit = nil) ⇒ Object

returns nearby found records in same layer as self, radius metera away, number of results limited to limit



50
51
52
# File 'lib/king_pin.rb', line 50

def nearby(radius, limit=nil)
  self.class.find(nearby_ids(radius, limit))
end

#nearby_ids(radius, limit = nil) ⇒ Object

returns nearby found record_ids in same layer as self, radius meters away, number of results limited to limit



41
42
43
# File 'lib/king_pin.rb', line 41

def nearby_ids(radius, limit=nil)
  JSON.parse(Pincaster.nearby(self, radius, limit))["matches"].map{|p| p["key"]}
end

#nearby_pins(radius, limit = nil) ⇒ Object



45
46
47
# File 'lib/king_pin.rb', line 45

def nearby_pins(radius, limit=nil)
  JSON.parse(Pincaster.nearby(self, radius, limit))["matches"]
end

#pinObject

returns the Pincaster pin for self



31
32
33
# File 'lib/king_pin.rb', line 31

def pin
  Pincaster.get_record(self)
end

#pin_latObject

returns objects latitude depending on configured method name for access as well as DEG or RAD configuration



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/king_pin.rb', line 69

def pin_lat
  if not !!self.class.kingpin_args[:methods]
    [:latitude, :lati, :ltt, :ltd, :lat, :lttd].each do |l|
      if self.respond_to?(l)
        return !!self.class.kingpin_args[:rad] ? self.send(l).to_f * 180 / Math::PI : self.send(l)
      end
    end
    return nil
  else
    return !!self.class.kingpin_args[:rad] ? self.send(self.class.kingpin_args[:methods][:lat]).to_f * 180.0 / Math::PI : self.send(self.class.kingpin_args[:methods][:lat])
  end
end

#pin_lngObject

returns objects longitude depending on configured method name for access as well as DEG or RAD configuration



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/king_pin.rb', line 55

def pin_lng
  if not !!self.class.kingpin_args[:methods]
    [:longitude, :long, :lng, :lgt, :lgtd, :lngtd].each do |l|
      if self.respond_to?(l)
        return !!self.class.kingpin_args[:rad] ? self.send(l).to_f * 180 / Math::PI : self.send(l)
      end
    end
    return nil
  else
    return !!self.class.kingpin_args[:rad] ? self.send(self.class.kingpin_args[:methods][:lng]).to_f * 180.0 / Math::PI : self.send(self.class.kingpin_args[:methods][:lng])
  end
end