Permalink
Browse files

Add Location class

  • Loading branch information...
1 parent 6a08487 commit 76fab098ecd234567176469406c359dd80758e1f @Floppy committed Aug 26, 2008
Showing with 227 additions and 0 deletions.
  1. +1 −0 lib/eeml.rb
  2. +90 −0 lib/eeml/location.rb
  3. +136 −0 spec/location_spec.rb
View
@@ -1,4 +1,5 @@
require 'eeml/exceptions'
+require 'eeml/location'
require 'eeml/environment'
require 'eeml/unit'
require 'eeml/data'
View
@@ -0,0 +1,90 @@
+module EEML
+
+ # An class representing a location. Can be added to EEML::Data objects.
+ class Location
+
+ # Create a new EEML::Location item. Domain setting is required, and must be either :physical or :virtual.
+ def initialize(loc_domain, options = {})
+ set_domain(loc_domain)
+ set_exposure(options[:exposure])
+ set_disposition(options[:disposition])
+ @name = options[:name]
+ self.lat = options[:lat]
+ self.lng = options[:lng]
+ @ele = options[:ele]
+ end
+
+ # Domain (:physical or :virtual)
+ attr_reader :domain
+
+ # Set the domain of the Location - must be either :physical or :virtual
+ def domain=(loc_domain)
+ set_domain(loc_domain)
+ end
+
+ # Exposure (:indoor or :outdoor)
+ attr_reader :exposure
+
+ # Set the exposure of the Location - must be either :indoor or :outdoor
+ def exposure=(loc_exposure)
+ set_exposure(loc_exposure)
+ end
+
+ # Disposition (:fixed or :mobile)
+ attr_reader :disposition
+
+ # Set the disposition of the Location - must be either :fixed or :mobile
+ def disposition=(loc_disposition)
+ set_disposition(loc_disposition)
+ end
+
+ # The name of the location
+ attr_accessor :name
+
+ # Latitude of location
+ attr_reader :lat
+
+ # Set the latitude of the location
+ def lat=(latitude)
+ raise ArgumentError.new("Latitude must be between -90 and +90") if latitude && (latitude < -90 || latitude > 90)
+ @lat = latitude
+ end
+
+ # Longitude of location
+ attr_reader :lng
+
+ # Set the longitude of the location
+ def lng=(longitude)
+ raise ArgumentError.new("Longitude must be between -180 and +180") if longitude && (longitude < -180 || longitude > 180)
+ @lng = longitude
+ end
+
+ # Elevation of location
+ attr_accessor :ele
+
+ protected
+
+ def set_domain(loc_domain)
+ unless [:physical, :virtual].include?(loc_domain)
+ raise ArgumentError.new("Domain must be :physical or :virtual")
+ end
+ @domain = loc_domain
+ end
+
+ def set_exposure(loc_exposure)
+ unless [nil, :indoor, :outdoor].include?(loc_exposure)
+ raise ArgumentError.new("Exposure must be :indoor or :outdoor")
+ end
+ @exposure = loc_exposure
+ end
+
+ def set_disposition(loc_disposition)
+ unless [nil, :fixed, :mobile].include?(loc_disposition)
+ raise ArgumentError.new("Disposition must be :fixed or :mobile")
+ end
+ @disposition = loc_disposition
+ end
+
+ end
+
+end
View
@@ -0,0 +1,136 @@
+require File.dirname(__FILE__) + '/spec_helper.rb'
+
+describe EEML::Location do
+
+ it "can be created with just a domain" do
+ lambda {
+ EEML::Location.new(:physical)
+ }.should_not raise_error
+ end
+
+ it "cannot be created with an invalid domain" do
+ lambda {
+ EEML::Location.new(nil)
+ }.should raise_error(ArgumentError, "Domain must be :physical or :virtual")
+ end
+
+ it "can be created with extra options" do
+ loc = EEML::Location.new(:physical,
+ :exposure => :indoor,
+ :disposition => :fixed,
+ :name => "My Room",
+ :lat => 32.4,
+ :lng => 22.7,
+ :ele => 0.2)
+ loc.exposure.should == :indoor
+ loc.disposition.should == :fixed
+ loc.name.should == "My Room"
+ loc.lat.should == 32.4
+ loc.lng.should == 22.7
+ loc.ele.should == 0.2
+ end
+
+ describe "with minimal information" do
+
+ before :each do
+ @loc = EEML::Location.new(:physical)
+ end
+
+ it "allows domain to be set to :physical" do
+ @loc.domain = :physical
+ @loc.domain.should == :physical
+ end
+
+ it "allows domain to be set to :virtual" do
+ @loc.domain = :virtual
+ @loc.domain.should == :virtual
+ end
+
+ it "does not allow invalid domain settings" do
+ lambda {
+ @loc.domain = :cheese
+ }.should raise_error(ArgumentError, "Domain must be :physical or :virtual")
+ end
+
+ it "allows exposure to be set to :indoor" do
+ @loc.exposure = :indoor
+ @loc.exposure.should == :indoor
+ end
+
+ it "allows exposure to be set to :outdoor" do
+ @loc.exposure = :outdoor
+ @loc.exposure.should == :outdoor
+ end
+
+ it "does not allow invalid exposure settings" do
+ lambda {
+ @loc.exposure = :cheese
+ }.should raise_error(ArgumentError, "Exposure must be :indoor or :outdoor")
+ end
+
+ it "allows disposition to be set to :fixed" do
+ @loc.disposition = :fixed
+ @loc.disposition.should == :fixed
+ end
+
+ it "allows disposition to be set to :mobile" do
+ @loc.disposition = :mobile
+ @loc.disposition.should == :mobile
+ end
+
+ it "does not allow invalid disposition settings" do
+ lambda {
+ @loc.disposition = :cheese
+ }.should raise_error(ArgumentError, "Disposition must be :fixed or :mobile")
+ end
+
+ it "can have a name" do
+ name = "My Room"
+ @loc.name = name
+ @loc.name.should == name
+ end
+
+ it "can have a latitude" do
+ lat = 32.4
+ @loc.lat = lat
+ @loc.lat.should == lat
+ end
+
+ it "cannot have a latitude outside -90..90" do
+ lat = 90.1
+ lambda {
+ @loc.lat = lat
+ }.should raise_error(ArgumentError, "Latitude must be between -90 and +90")
+ lat = -90.1
+ lambda {
+ @loc.lat = lat
+ }.should raise_error(ArgumentError, "Latitude must be between -90 and +90")
+ end
+
+ it "can have a longitude" do
+ lng = 22.7
+ @loc.lng = lng
+ @loc.lng.should == lng
+ end
+
+ it "cannot have a longitude outside -180..180" do
+ lng = 180.1
+ lambda {
+ @loc.lng = lng
+ }.should raise_error(ArgumentError, "Longitude must be between -180 and +180")
+ lng = -180.1
+ lambda {
+ @loc.lng = lng
+ }.should raise_error(ArgumentError, "Longitude must be between -180 and +180")
+ end
+
+ it "can have an elevation" do
+ ele = 0.2
+ @loc.ele = ele
+ @loc.ele.should == ele
+ end
+
+ end
+
+
+end

0 comments on commit 76fab09

Please sign in to comment.