<commit_before>module IpAuth
  class IpConfig < ::ActiveRecord::Base
    # internal constants
    IP_CONFIG_UNIT_SEPARATOR = "<>"

    # accessors
    attr_accessor :decoded_setting, :decoded_setting_valid

    # associations
    belongs_to :ip_authorizable, polymorphic: true

    # methods

    def configuration(options = {})
      begin
        case options[:method]
        when :get
          get_configuration(options)
        when :set
          set_configuration(options)
        end
      rescue => error
        options[:raise] ? raise(error) : false
      end
    end

    def get_configuration(options = {})
      update_decoded_setting unless @decoded_setting_valid
      @decoded_setting
    end

    def set_configuration(options = {})
      raise IpAuth::Exception::General.new("Ip configuration is not a Array") unless options[:config].is_a?(Array)
      @decoded_setting_valid = false
      self.update(setting: get_encoded_string(options[:config]))
      get_configuration(options)
    end

    def get_encoded_string(conf)
      conf.collect(&:to_json).join(IP_CONFIG_UNIT_SEPARATOR)
    end

    def update_decoded_setting
      @decoded_setting = []
      self.setting.split(IP_CONFIG_UNIT_SEPARATOR).each do |unit_str|
        @decoded_setting << IpAuth::IpConfigUnit.new(JSON.load(unit_str))
      end
      @decoded_setting_valid = true<commit_msg>refactoring<commit_after>module IpAuth
  class IpConfig < ::ActiveRecord::Base
    # internal constants
    IP_CONFIG_UNIT_SEPARATOR = "<>"

    # accessors
    attr_accessor :decoded_setting, :decoded_setting_valid

    # associations
    belongs_to :ip_authorizable, polymorphic: true

    # methods

    def configuration(options = {})
      begin
        case options[:method]
        when :get
          get_configuration(options)
        when :set
          set_configuration(options)
        end
      rescue => error
        options[:raise] ? raise(error) : false
      end
    end

    def get_configuration(options = {})
      update_decoded_setting unless @decoded_setting_valid
      @decoded_setting
    end

    def set_configuration(options = {})
      raise IpAuth::Exception::General.new("Ip configuration is not a Array") unless options[:config].is_a?(Array)
      @decoded_setting_valid = false
      self.update(setting: get_encoded_string(options[:config]))
      get_configuration(options)
    end

    def get_encoded_string(conf)
      conf.collect(&:to_json).join(IP_CONFIG_UNIT_SEPARATOR)
    end

    def update_decoded_setting
      @decoded_setting = self.setting.split(IP_CONFIG_UNIT_SEPARATOR).map{|unit_str| IpAuth::IpConfigUnit.new(JSON.load(unit_str))}
      @decoded_setting_valid = true
{'fraction_non_alphanumeric': 0.08864985163204747, 'fraction_numerical': 0.0, 'mean_word_length': 3.220657276995305, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '4592058', 'n_tokens_mistral': 806, 'n_tokens_neox': 794, 'n_words': 147}