<commit_before>class Ruxero::BaseModel

  def self.singularized_name
    self.name.demodulize.singularize
  end

  def self.pluralized_name
    self.name.demodulize.pluralize
  end

  def self.parse_collection(collection)
    collection.css("#{pluralized_name} #{singularized_name}").collect { |record|
      parse_record(record)
    }.compact
  end

  def self.parse_record(record)
    new_record = self.new
    record.elements.each do |element|
      field_name = element.name.underscore.to_sym
      field = self._fields[field_name]
      next unless field
      value = convert_value(field, element, element.text.strip)
      new_record.instance_variable_set("@#{field_name}", value)
    end
    new_record
  end

  def self.convert_value(field, element, value)
    case field[:type].to_s
    when 'String'  then value.to_s
    when 'Time'    then Time.parse(value)
    when 'Date'    then Date.parse(value)
    when 'Float'   then value.to_f
    when 'Boolean' then (value == 'true')
    else
      if field[:type].to_s =~ /Ruxero/ # Ruxero model
        field[:type].parse_record(element)
      else
        value
      end
    end
  end

end<commit_msg>Properly parse times from Xero API as UTC<commit_after>class Ruxero::BaseModel

  def self.singularized_name
    self.name.demodulize.singularize
  end

  def self.pluralized_name
    self.name.demodulize.pluralize
  end

  def self.parse_collection(collection)
    collection.css("#{pluralized_name} #{singularized_name}").collect { |record|
      parse_record(record)
    }.compact
  end

  def self.parse_record(record)
    new_record = self.new
    record.elements.each do |element|
      field_name = element.name.underscore.to_sym
      field = self._fields[field_name]
      next unless field
      value = convert_value(field, element, element.text.strip)
      new_record.instance_variable_set("@#{field_name}", value)
    end
    new_record
  end

  def self.convert_value(field, element, value)
    case field[:type].to_s
    when 'String'  then value.to_s
    when 'Time'    then Time.parse("#{value} UTC")
    when 'Date'    then Date.parse(value)
    when 'Float'   then value.to_f
    when 'Boolean' then (value == 'true')
    else
      if field[:type].to_s =~ /Ruxero/ # Ruxero model
        field[:type].parse_record(element)
      else
        value
      end
    end
  end

end
{'fraction_non_alphanumeric': 0.10140008485362749, 'fraction_numerical': 0.0, 'mean_word_length': 3.0033955857385397, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3808545', 'n_tokens_mistral': 792, 'n_tokens_neox': 771, 'n_words': 169}