<commit_before>class School < Hashie::Mash
  extend ActiveModel::Callbacks
  
  #include ActiveModel::SerializerSupport
  include ActiveModel::Dirty
  include ActiveModel::Validations
  include ActiveModel::Validations::Callbacks
  #include ActiveModel::Conversion
  include ActiveModel::Observing

  def full_address
    "#{address}, #{city}, #{state}, #{zipcode}#{zipcode4.blank? ? '' : '-' + zipcode4}"
  end

  def geocode_address
    
      geocoder_results = begin
        Geocoder.search(full_address)
      rescue => e
        puts "Exception Geocoding #{full_address}: #{e}"
        []
      end
    
    unless geocoder_results.empty?
      geo_location = geocoder_results.first
      
      self.latitude = geo_location.latitude
      self.longitude = geo_location.longitude
      
      point = Geomodel::Types::Point.new(self.latitude, self.longitude)
      self.geocells = Geomodel::GeoCell.generate_geocells(point)
      self.geocoded = true
    else
      self.geocoded = false
      #errors.add(:address, "Could not Geocode address") 
      puts "Could not geocode address"
    end
  end
  
  def location
    Geomodel::Types::Point.new(self.latitude, self.longitude)
  end
    
  #
  # Data persistence
  #              
  
  def self.all
    CassandraMigrations::Cassandra.select(:schools).map { |attributes| School.new(attributes) }
  end
  
  def self.find(id)
    School.new(CassandraMigrations::Cassandra.select(:schools,
      :selection => "id = #{id}",
      :limit => 1
    ).first)
  end
  
  def self.first
    School.new(CassandraMigrations::Cassandra.select(:schools,
      :limit => 1
    ).first)
  end
  
  def self.where(pks, options = {})
    selection = pks.reject { |n,v| v.nil? }.map { |n,v| "#{n} = '#{v}'" }.join(' AND ')
    
    CassandraMigrations::Cassandra.select(
      :schools,
      :selection => selection
    ).map { |attributes| School.new(attributes) }
  end
  
  def update_attributes(attributes, options = {})
    attributes.each do |k, v|
      if respond_to?("#{k}=")
        send("#{k}=", v)
      else
        raise(UnknownAttributeError, "unknown attribute: #{k}")
      end
    end
    update(attributes)
  end
  
  def destroy
  end
  
  def errors
    []
  end
  
  def save
    CassandraMigrations::Cassandra.write!(:schools, self.to_hash)
    true
  end
  
  def update(attributes = {})
    to_be_updated = attributes.empty? ? self.to_hash.delete_if { |k,v| k == 'id' } : attributes.delete_if { |k,v| v.nil? }
    CassandraMigrations::Cassandra.update!(:schools, "id = #{self.id}", to_be_updated)
    true<commit_msg>Added better error output to geocoding failures, escaped single quotes in CQL values<commit_after>class School < Hashie::Mash
  extend ActiveModel::Callbacks
  
  #include ActiveModel::SerializerSupport
  include ActiveModel::Dirty
  include ActiveModel::Validations
  include ActiveModel::Validations::Callbacks
  #include ActiveModel::Conversion
  include ActiveModel::Observing

  def full_address
    "#{address}, #{city}, #{state}, #{zipcode}#{zipcode4.blank? ? '' : '-' + zipcode4}"
  end

  def geocode_address
    
    geocoder_results = begin
      Geocoder.search(full_address)
    rescue => e
      puts "Exception Geocoding #{full_address}: #{e}"
      []
    end
    
    puts "Geocoder Results ==> #{geocoder_results}"
    
    unless geocoder_results.empty?
      geo_location = geocoder_results.first
      
      self.latitude = geo_location.latitude
      self.longitude = geo_location.longitude
      
      point = Geomodel::Types::Point.new(self.latitude, self.longitude)
      self.geocells = Geomodel::GeoCell.generate_geocells(point)
      self.geocoded = true
    else
      self.geocoded = false
      #errors.add(:address, "Could not Geocode address") 
      puts "Could not geocode address: #{full_address}"
    end
  end
  
  def location
    Geomodel::Types::Point.new(self.latitude, self.longitude)
  end
    
  #
  # Data persistence
  #              
  
  def self.all
    CassandraMigrations::Cassandra.select(:schools).map { |attributes| School.new(attributes) }
  end
  
  def self.find(id)
    School.new(CassandraMigrations::Cassandra.select(:schools,
      :selection => "id = #{id}",
      :limit => 1
    ).first)
  end
  
  def self.first
    School.new(CassandraMigrations::Cassandra.select(:schools,
      :limit => 1
    ).first)
  end
  
  def self.where(pks, options = {})
    selection = pks.reject { |n,v| v.nil? }.map { |n,v| "#{n} = '#{v}'" }.join(' AND ')
    
    CassandraMigrations::Cassandra.select(
      :schools,
      :selection => selection
    ).map { |attributes| School.new(attributes) }
  end
  
  def update_attributes(attributes, options = {})
    attributes.each do |k, v|
      if respond_to?("#{k}=")
        send("#{k}=", v)
      else
        raise(UnknownAttributeError, "unknown attribute: #{k}")
      end
    end
    update(attributes)
  end
  
  def destroy
  end
  
  def errors
    []
  end
  
  def save
    values = self.to_hash.inject({}) { |h, (k, v)| h[k] = (String === v ? v.gsub("'", "''") : v); h } # escape single quotes
    CassandraMigrations::Cassandra.write!(:schools, values)
    true
  end
  
  def update(attributes = {})
    to_be_updated = attributes.empty? ? self.to_hash.delete_if { |k,v| k == 'id' } : attributes.delete_if { |k,v| v.nil? }
    CassandraMigrations::Cassandra.update!(:schools, "id = #{self.id}", to_be_updated)
    true
{'fraction_non_alphanumeric': 0.13252122554448137, 'fraction_numerical': 0.0014765596160944998, 'mean_word_length': 2.952589350838804, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4360395', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1818, 'n_words': 364}