<commit_before>#########################################################################
#
# ProjEstimate, Open Source project estimation web application
# Copyright (c) 2012 Spirula (http://www.spirula.fr)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
########################################################################

#Master table
#Global attributes of project. Ex : size, cost, result, date etc...
#Those attributes are used into AttributeModule
class PeAttribute < ActiveRecord::Base
  include MasterDataHelper  #Module master data management (UUID generation, deep clone, ...)

  serialize :options, Array

  has_many :attribute_modules, :dependent => :destroy
  has_many :pemodules, :through => :attribute_modules

  belongs_to :record_status
  belongs_to :owner_of_change, :class_name => "User", :foreign_key => "owner_id"

  validates_presence_of :description, :attr_type, :record_status
  validates :uuid, :presence => true, :uniqueness => { :case_sensitive => false }
  validates :name, :alias, :presence => true,  :uniqueness => { :scope => :record_status_id, :case_sensitive => false }
  validates :custom_value, :presence => true, :if => :is_custom?

  ##Enable the amoeba gem for deep copy/clone (dup with associations)
  amoeba do
    enable
    exclude_field [:attribute_modules]

    customize(lambda { |original_record, new_record|
      new_record.reference_uuid = original_record.uuid
      new_record.reference_id = original_record.id
      new_record.record_status = RecordStatus.find_by_name("Proposed") #RecordStatus.first
    })
  end

  searchable do
    text :name, :description, :alias
  end

  def self.attribute_list
    PeAttribute.all.map(&:alias)
  end

  def self.attribute_updated_at
    PeAttribute.all.map(&:updated_at)
  end

  #Override
  def to_s
    self.name + ' - ' + self.description.truncate(20)
  end

  #Type of the aggregation
  #Not finished
  def self.type_aggregation
    [["Moyenne", "average" ] ,["Somme", "sum"], ["Maximum", "maxi" ]]
  end

  def self.type_values
    [["Integer", "integer" ] ,["Float", "float"], ["Date", "date" ], ["Text", "text" ], ["List", "list" ]]
  end

  def self.value_options
    [
     ["Greater than or equal to", ">=" ],
     ["Greater than", ">" ],
     ["Lower than or equal to", "<=" ],
     ["Lower than", "<" ],
     ["Equal to", "=="],
     ["Not equal to", "!="]
    ]
  end

  # Verify if params val is validate
  def is_validate(val)
    array = self.options.compact.reject { |s| s.nil? or s.empty? or s.blank? }

    if self.attribute_type == "numeric"
      unless array.empty?
        str = array[1] + array[2]
        str_to_eval = val + str.to_s
        begin
          eval(str_to_eval)
        rescue Exception => se
          return false
        end
      else
        return true
      end
    elsif self.attribute_type == "string"
      val.class == String
    elsif self.attribute_type == "date"
      str_to_eval = "'#{val}'.to_date #{ array[1]} '#{array[2]}'.to_date"
      begin
        eval(str_to_eval)
      rescue Exception => se
        return false
      end
    end
  end

  #return the data type
  def data_type
    self.attr_type
  end

  #return the data type
  def attribute_type
    case self.attr_type
      when "integer"
        "numeric"
      when "float"
        "numeric"
      when "date"
        "date"
      when "text"
        "string"
      when "list"
        "string"
      when "array"
        "string"
    end
  end

    #return the data type
  def explicit_data_type
    case self.attr_type
      when "integer"
        "numeric"
      when "float"
        "numeric"
      when "date"
        "date"
      when "text"
        "string"
      when "list"
        "string"
      when "array"
        "string"
    end
  end


end
<commit_msg>refs #280 adding between rule<commit_after>#########################################################################
#
# ProjEstimate, Open Source project estimation web application
# Copyright (c) 2012 Spirula (http://www.spirula.fr)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
########################################################################

#Master table
#Global attributes of project. Ex : size, cost, result, date etc...
#Those attributes are used into AttributeModule
class PeAttribute < ActiveRecord::Base
  include MasterDataHelper  #Module master data management (UUID generation, deep clone, ...)

  serialize :options, Array

  has_many :attribute_modules, :dependent => :destroy
  has_many :pemodules, :through => :attribute_modules

  belongs_to :record_status
  belongs_to :owner_of_change, :class_name => "User", :foreign_key => "owner_id"

  validates_presence_of :description, :attr_type, :record_status
  validates :uuid, :presence => true, :uniqueness => { :case_sensitive => false }
  validates :name, :alias, :presence => true,  :uniqueness => { :scope => :record_status_id, :case_sensitive => false }
  validates :custom_value, :presence => true, :if => :is_custom?

  ##Enable the amoeba gem for deep copy/clone (dup with associations)
  amoeba do
    enable
    exclude_field [:attribute_modules]

    customize(lambda { |original_record, new_record|
      new_record.reference_uuid = original_record.uuid
      new_record.reference_id = original_record.id
      new_record.record_status = RecordStatus.find_by_name("Proposed") #RecordStatus.first
    })
  end

  searchable do
    text :name, :description, :alias
  end

  def self.attribute_list
    PeAttribute.all.map(&:alias)
  end

  def self.attribute_updated_at
    PeAttribute.all.map(&:updated_at)
  end

  #Override
  def to_s
    self.name + ' - ' + self.description.truncate(20)
  end

  #Type of the aggregation
  #Not finished
  def self.type_aggregation
    [["Moyenne", "average" ] ,["Somme", "sum"], ["Maximum", "maxi" ]]
  end

  def self.type_values
    [["Integer", "integer" ] ,["Float", "float"], ["Date", "date" ], ["Text", "text" ], ["List", "list" ]]
  end

  def self.value_options
    [
     ["Greater than or equal to", ">=" ],
     ["Greater than", ">" ],
     ["Lower than or equal to", "<=" ],
     ["Lower than", "<" ],
     ["Equal to", "=="],
     ["Not equal to", "!="],
     ["Between", "between"]
    ]
  end

  # Verify if params val is validate
  def is_validate(val)
    array = self.options.compact.reject { |s| s.nil? or s.empty? or s.blank? }
    if self.attribute_type == "numeric"
      unless array.empty?
        if self.options[1] == "between"
          v1 = self.options[2].split(',').first.to_i
          v2 = self.options[2].split(',').last.to_i
          puts self.options[2]
          val.to_i.between?(v1, v2)
        else
          str = array[1] + array[2]
          str_to_eval = val + str.to_s
          begin
            eval(str_to_eval)
          rescue Exception => se
            return false
          end
        end
      else
        return true
      end
    elsif self.attribute_type == "string"
      val.class == String
    elsif self.attribute_type == "date"
      str_to_eval = "'#{val}'.to_date #{ array[1]} '#{array[2]}'.to_date"
      begin
        eval(str_to_eval)
      rescue Exception => se
        return false
      end
    end
  end

  #return the data type
  def data_type
    self.attr_type
  end

  #return the data type
  def attribute_type
    case self.attr_type
      when "integer"
        "numeric"
      when "float"
        "numeric"
      when "date"
        "date"
      when "text"
        "string"
      when "list"
        "string"
      when "array"
        "string"
    end
  end

    #return the data type
  def explicit_data_type
    case self.attr_type
      when "integer"
        "numeric"
      when "float"
        "numeric"
      when "date"
        "date"
      when "text"
        "string"
      when "list"
        "string"
      when "array"
        "string"
    end
  end


end

{'fraction_non_alphanumeric': 0.14067111159689583, 'fraction_numerical': 0.0036069515794075854, 'mean_word_length': 2.8332635106828654, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3797538', 'n_tokens_mistral': 2821, 'n_tokens_neox': 2747, 'n_words': 849}