<commit_before># Module that abstracts some common patterns for data storage/retrieval in AMEE.  This is 
# automatically included in ActiveRecord::Base object when used with Rails.  See the gem
# README for more information on the application structure this gem assumes.
# 
# Classes that have the has_carbon_data_stored_in_amee decleartion require the following
# database columns:
# * name (string) - a name for the object, often set by user but can be assigned automatically
# * amee_profile_item_id (string) - the AMEE profile Item ID used to store the carbon data
# * carbon_output_cache (float) - the amount of carbon produced
# * units (string) - the units the amount field is in
# * amount (float) - the amount of the thing being recorded, eg 6 (kg), 9 (litres)
# * amee_profile (string) - optional.  The amee profile identifier under which all the data
#   is stored.  Although this field is optional, either this or profile_id is required
# * profile_id (integer) - optional.  Used when the model belongs_to a parent that has the
#   amee_profile identifier.  Name is changed according to the :profile option passed into 
#   the has_carbon_data_stored_in_amee.  Although this field is optional, either this or 
#   amee_profile is required.
# * repetitions (integer) - optional.  Used when the model object is composed of several
#   repetitions - for example 6 x 3 miles would make the repetitions 6  
# * start_date (date) - optional.  Used in combination with the has_date_range option on
#   has_carbon_data_stored_in_amee to store the start date for the data
# * end_date (date) - optional.  Used in combination with the has_date_range option on
#   has_carbon_data_stored_in_amee to store the end date for the data
module AmeeCarbonStore
  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    # Class method that configures a class for storing carbon data in amee.  Options are as follows:
    # * profile - if set will use this model (through a belongs_to relationship) to access the amee
    #   profile to store the data under rather than the model itself.  Pass the model to use as symbol
    #   - eg :user or :project.  Introduces the need for the profile_id field as described in header
    #   and the referenced model must store the amee profile key under the field amee_profile (as 
    #   would be provided by the has_amee_profile decleration)
    # * nameless - if set then automatically assign the name field so the user doesn't have to
    #   (still requires the name field in the database as a name must be set to store in amee)
    # * has_date_range - will check for the presence of a start_date and end_date on the object
    #   and pass that through to AMEE to store with the data.  Will also check the name is unique 
    #   given the dates unless used in conjunction with :nameless option (the two together are
    #   therefore not recommended as it will be easy to create overlapping data).  Requires the 
    #   start_date and end_date database fields as described in header.
    # * repetitions - allows repetitions of the data at a database level where AMEE doesn't support
    #   it natively.  For example multiple journeys can be setup with this option.  The value stored
    #   in AMEE will be the total for all journeys.  Requires the repetitions database field as 
    #   described in header.
    # * singular_types - if using a structure where multiple types are available for a model and 
    #   the type is stored in the field "#{model}_type", this option enforces that only one instance
    #   of each type may exist in the database (for a given project if using a project structure)
    def has_carbon_data_stored_in_amee(options = {})
      
      if options[:profile]
        belongs_to options[:profile]
      else
        has_amee_profile
      end
      
      validates_numericality_of :amount
      validate_on_create :units_are_valid
      validates_presence_of :start_date, :end_date if options[:has_date_range]
      unless options[:nameless]
        if options[:has_date_range]
          validate :name_is_unique_given_date_range
        else
          uniqueness_options = options[:profile] ? {:scope => "#{options[:profile]}_id".to_sym} : {}
          validates_uniqueness_of :name, uniqueness_options
        end
        validates_format_of :name, :with => /\A[\w -]+\Z/, 
          :message => "must be letters, numbers, spaces or underscores only"
        validates_length_of :name, :maximum => 250
      end
      if options[:singular_types]
        validate_on_create :maximum_one_instance_for_each_type
      end
      if options[:repetitions]
        validates_numericality_of :repetitions, :only_integer => true
      end
      
      before_create :add_to_amee
      before_update :update_amee<commit_msg>Remove naming restrictions [COM-204]<commit_after># Module that abstracts some common patterns for data storage/retrieval in AMEE.  This is 
# automatically included in ActiveRecord::Base object when used with Rails.  See the gem
# README for more information on the application structure this gem assumes.
# 
# Classes that have the has_carbon_data_stored_in_amee decleartion require the following
# database columns:
# * name (string) - a name for the object, often set by user but can be assigned automatically
# * amee_profile_item_id (string) - the AMEE profile Item ID used to store the carbon data
# * carbon_output_cache (float) - the amount of carbon produced
# * units (string) - the units the amount field is in
# * amount (float) - the amount of the thing being recorded, eg 6 (kg), 9 (litres)
# * amee_profile (string) - optional.  The amee profile identifier under which all the data
#   is stored.  Although this field is optional, either this or profile_id is required
# * profile_id (integer) - optional.  Used when the model belongs_to a parent that has the
#   amee_profile identifier.  Name is changed according to the :profile option passed into 
#   the has_carbon_data_stored_in_amee.  Although this field is optional, either this or 
#   amee_profile is required.
# * repetitions (integer) - optional.  Used when the model object is composed of several
#   repetitions - for example 6 x 3 miles would make the repetitions 6  
# * start_date (date) - optional.  Used in combination with the has_date_range option on
#   has_carbon_data_stored_in_amee to store the start date for the data
# * end_date (date) - optional.  Used in combination with the has_date_range option on
#   has_carbon_data_stored_in_amee to store the end date for the data
module AmeeCarbonStore
  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    # Class method that configures a class for storing carbon data in amee.  Options are as follows:
    # * profile - if set will use this model (through a belongs_to relationship) to access the amee
    #   profile to store the data under rather than the model itself.  Pass the model to use as symbol
    #   - eg :user or :project.  Introduces the need for the profile_id field as described in header
    #   and the referenced model must store the amee profile key under the field amee_profile (as 
    #   would be provided by the has_amee_profile decleration)
    # * nameless - if set then automatically assign the name field so the user doesn't have to
    #   (still requires the name field in the database as a name must be set to store in amee)
    # * has_date_range - will check for the presence of a start_date and end_date on the object
    #   and pass that through to AMEE to store with the data.  Will also check the name is unique 
    #   given the dates unless used in conjunction with :nameless option (the two together are
    #   therefore not recommended as it will be easy to create overlapping data).  Requires the 
    #   start_date and end_date database fields as described in header.
    # * repetitions - allows repetitions of the data at a database level where AMEE doesn't support
    #   it natively.  For example multiple journeys can be setup with this option.  The value stored
    #   in AMEE will be the total for all journeys.  Requires the repetitions database field as 
    #   described in header.
    # * singular_types - if using a structure where multiple types are available for a model and 
    #   the type is stored in the field "#{model}_type", this option enforces that only one instance
    #   of each type may exist in the database (for a given project if using a project structure)
    def has_carbon_data_stored_in_amee(options = {})
      
      if options[:profile]
        belongs_to options[:profile]
      else
        has_amee_profile
      end
      
      validates_numericality_of :amount
      validate_on_create :units_are_valid
      validates_presence_of :start_date, :end_date if options[:has_date_range]
      unless options[:nameless]
        if options[:has_date_range]
          validate :name_is_unique_given_date_range
        else
          uniqueness_options = options[:profile] ? {:scope => "#{options[:profile]}_id".to_sym} : {}
          validates_uniqueness_of :name, uniqueness_options
        end
        validates_length_of :name, :maximum => 250
      end
      if options[:singular_types]
        validate_on_create :maximum_one_instance_for_each_type
      end
      if options[:repetitions]
        validates_numericality_of :repetitions, :only_integer => true
      end
      
      before_create :add_to_amee
      before_update :update_amee
{'fraction_non_alphanumeric': 0.05066079295154185, 'fraction_numerical': 0.0019928676316341514, 'mean_word_length': 3.369844179651696, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '5616682', 'n_tokens_mistral': 2652, 'n_tokens_neox': 2557, 'n_words': 1172}