Class: Opencellid::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/opencellid/cell.rb

Overview

Models a Cell object, both as an output from the server and as an input to queries. When using an object of this type to specify query parameters, the class attributes that should be used as filters should be set to the desired values, while the other attributes should be set to nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, mnc, mcc, lac) ⇒ Cell

Returns a new instance of Cell.

Parameters:

  • id (Integer)

    the id of the cell

  • mnc (Integer)

    the mnc code of the cell

  • mcc (Integer)

    the mcc code of the cell

  • lac (Integer)

    the lac code of the cell



17
18
19
20
21
22
23
# File 'lib/opencellid/cell.rb', line 17

def initialize(id, mnc, mcc, lac)
  @id = id
  @mnc = mnc
  @mcc = mcc
  @lac = lac
  @measures = []
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



11
12
13
# File 'lib/opencellid/cell.rb', line 11

def id
  @id
end

#lacObject

Returns the value of attribute lac.



11
12
13
# File 'lib/opencellid/cell.rb', line 11

def lac
  @lac
end

#latObject

Returns the value of attribute lat.



11
12
13
# File 'lib/opencellid/cell.rb', line 11

def lat
  @lat
end

#lonObject

Returns the value of attribute lon.



11
12
13
# File 'lib/opencellid/cell.rb', line 11

def lon
  @lon
end

#mccObject

Returns the value of attribute mcc.



11
12
13
# File 'lib/opencellid/cell.rb', line 11

def mcc
  @mcc
end

#measuresObject

Returns the value of attribute measures.



11
12
13
# File 'lib/opencellid/cell.rb', line 11

def measures
  @measures
end

#mncObject

Returns the value of attribute mnc.



11
12
13
# File 'lib/opencellid/cell.rb', line 11

def mnc
  @mnc
end

#no_of_samplesObject

Returns the value of attribute no_of_samples.



11
12
13
# File 'lib/opencellid/cell.rb', line 11

def no_of_samples
  @no_of_samples
end

#rangeObject

Returns the value of attribute range.



11
12
13
# File 'lib/opencellid/cell.rb', line 11

def range
  @range
end

Class Method Details

.from_element(element) ⇒ Cell

Parses the given element setting the information into a Cell object

Parameters:

  • element (REXML::Element)

    the XML element containing the representation of the Cell element

Returns:

  • (Cell)

    the Cell object obtained by parsing the XML

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/opencellid/cell.rb', line 28

def self.from_element(element)
  return nil unless element
  raise ArgumentError, 'element must be of type XEXML::Element' unless element.is_a? REXML::Element
  raise ArgumentError, 'element must be a <cell>' unless element.name == 'cell'
  attrs = element.attributes

  result = Cell.new(::Opencellid.to_i_or_nil(attrs['cellId']), ::Opencellid.to_i_or_nil(attrs['mnc']),
                    ::Opencellid.to_i_or_nil(attrs['mcc']),::Opencellid.to_i_or_nil(attrs['lac']))
  result.lat = ::Opencellid.to_f_or_nil(attrs['lat'])
  result.lon = ::Opencellid.to_f_or_nil(attrs['lon'])
  result.range = ::Opencellid.to_i_or_nil(attrs['range'])
  result.no_of_samples= ::Opencellid.to_i_or_nil(attrs['nbSamples'])
  element.elements.each('measure') { |e| result.add_measure Measure.from_element e}
  result
end

Instance Method Details

#add_measure(measure) ⇒ Object

Adds a new measure to this cell does NOT result in an add_measure call being invoked on the Opencellid object

Parameters:

  • measure (Measure)

    a measure object to be added to this cell. Note that adding the object to the cell



54
55
56
# File 'lib/opencellid/cell.rb', line 54

def add_measure(measure)
  @measures << measure
end

#has_measures?bool

Indicates whether this cell contains measure objects

Returns:

  • (bool)

    whether this cell contains measure objects



47
48
49
# File 'lib/opencellid/cell.rb', line 47

def has_measures?
  @measures.count > 0
end

#to_lng_latArray

Returns an array containing the longitude and latitude of the cell, this method makes the Cell object to be compatible with mongoid_spacial gem

Returns:

  • (Array)

    an array containing longitude and latitude of the cell.



68
69
70
# File 'lib/opencellid/cell.rb', line 68

def to_lng_lat
  [lon, lat]
end

#to_query_hashHash

Helper function that transforms the parameters of this cell in a hash of query parameters to be used with the library cell querying functions

Returns:

  • (Hash)

    a hash object containing the non nil parameters which can be used in while querying for cells



61
62
63
# File 'lib/opencellid/cell.rb', line 61

def to_query_hash
  {cellid: id, mnc: mnc, mcc: mcc, lac: lac}.delete_if {|_,v| v.nil?}
end