Sha256: 5ddc5765e9032a4745205d034ddca4212370d75deeb5c0053c6bce85f127297a

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module USGeo
  # This module is mixed into all models with a population and land area.
  module Population
    # @!attribute population
    #   @return [Integer, nil] Total population of the area.

    # @!attribute housing_units
    #   @return [Integer, nil] Total housing units in the area.

    # Population per square mile.
    #
    # @return [Float, nil]
    def population_density
      population.to_f / land_area if population && land_area.to_f > 0
    end

    # Population per square kilometer.
    #
    # @return [Float, nil]
    def population_density_km
      population.to_f / land_area_km if population && land_area.to_f > 0
    end

    # Housing units per square mile.
    #
    # @return [Float, nil]
    def housing_density
      housing_units.to_f / land_area if housing_units && land_area.to_f > 0
    end

    # Housing units per square kilometer.
    #
    # @return [Float, nil]
    def housing_density_km
      housing_units.to_f / land_area_km if housing_units && land_area.to_f > 0
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
us_geo-2.1.1 lib/us_geo/population.rb
us_geo-2.1.0 lib/us_geo/population.rb
us_geo-2.0.4 lib/us_geo/population.rb
us_geo-2.0.3 lib/us_geo/population.rb