Sha256: cc71bc433f1f00866d364ff0ff7785153047e76166486c0b14ff871b94656af2

Contents?: true

Size: 1.59 KB

Versions: 12

Compression:

Stored size: 1.59 KB

Contents

require 'earth/locality'
require 'earth/industry/industry'

class MecsRatio < ActiveRecord::Base
  self.primary_key = "name"
  
  belongs_to :industry
  
  col :name
  col :census_region_number, :type => :integer
  col :naics_code
  col :energy_per_dollar_of_shipments, :type => :float
  col :energy_per_dollar_of_shipments_units
  
  # Find the first record whose naics_code matches code and whose energy per dollar shipment is present.
  # If no record found chop off the last character of code and try again, and so on.
  def self.find_by_naics_code(code)
    candidate = find_by_naics_code_and_census_region_number(code, nil)
  end
  
  # Find the first record whose census_region_number matches number, whose naics_code matches code, and whose energy per dollar of shipments is present.
  # If none found and we know census region number, try looking nationwide
  # If none found and looking nationwide, chop off the last character of code and try again looking in census region
  # And so on
  def self.find_by_naics_code_and_census_region_number(code, number, original_number = number)
    if code.blank?
      record = nil
    else
      code = Industry.format_naics_code code
      candidate = where(:census_region_number => number, :naics_code => code).first
      
      if candidate.try(:energy_per_dollar_of_shipments).present?
        record = candidate
      elsif number.present?
        record = find_by_naics_code_and_census_region_number(code, nil, original_number)
      else
        record = find_by_naics_code_and_census_region_number(code[0..-2], original_number)
      end
    end
    record
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
earth-0.12.4 lib/earth/industry/mecs_ratio.rb
earth-0.12.3 lib/earth/industry/mecs_ratio.rb
earth-0.12.2 lib/earth/industry/mecs_ratio.rb
earth-0.12.1 lib/earth/industry/mecs_ratio.rb
earth-0.12.0 lib/earth/industry/mecs_ratio.rb
earth-0.11.20 lib/earth/industry/mecs_ratio.rb
earth-0.11.19 lib/earth/industry/mecs_ratio.rb
earth-0.11.18 lib/earth/industry/mecs_ratio.rb
earth-0.11.17 lib/earth/industry/mecs_ratio.rb
earth-0.11.16 lib/earth/industry/mecs_ratio.rb
earth-0.11.15 lib/earth/industry/mecs_ratio.rb
earth-0.11.14 lib/earth/industry/mecs_ratio.rb