<commit_before>module Stripe
  module Plans
    @plans = {}

    def self.all
      @plans.values
    end

    def self.[](key)
      @plans[key.to_s]
    end

    def self.[]=(key, value)
      @plans[key.to_s] = value
    end

    def self.put!
      all.each do |plan|
        plan.put!
      end
    end

    def plan(id)
      config = Configuration.new(id)
      yield config
      config.finalize!
    end

    class Configuration
      include ActiveModel::Validations
      attr_reader :id, :currency
      attr_accessor :name, :amount, :interval, :interval_count, :trial_period_days

      validates_presence_of :id, :name, :amount
      validates_inclusion_of :interval, :in => %w(month year), :message => "'%{value}' is not one of 'month' or 'year'"

      def initialize(id)
        @id = id
        @currency = 'usd'
        @interval_count = 1
        @trial_period_days = 0
      end

      def finalize!
        validate!
        globalize!
      end

      def validate!
        fail InvalidPlanError, errors if invalid?
      end

      def globalize!
        Stripe::Plans[@id.to_s] = self
        Stripe::Plans.const_set(@id.to_s.upcase, self)
      end

      def put!
        if exists?
          puts "[EXISTS] - #{@id}"
        else
          plan = Stripe::Plan.create(
            :id => @id,
            :currency => @currency,
            :name => @name,
            :amount => @amount,
            :interval => @interval,
            :interval_count => @interval_count,
            :trial_period_days => @trial_period_days
          )
          puts "[CREATE] - #{plan}"
        end
      end

      def exists?
        !!Stripe::Plan.retrieve("#{@id}")
      rescue Stripe::InvalidRequestError
        false
      end
    end

    class InvalidPlanError < StandardError
      attr_reader :errors

      def initialize(errors)<commit_msg>make a stripe plan use its id as string representation<commit_after>module Stripe
  module Plans
    @plans = {}

    def self.all
      @plans.values
    end

    def self.[](key)
      @plans[key.to_s]
    end

    def self.[]=(key, value)
      @plans[key.to_s] = value
    end

    def self.put!
      all.each do |plan|
        plan.put!
      end
    end

    def plan(id)
      config = Configuration.new(id)
      yield config
      config.finalize!
    end

    class Configuration
      include ActiveModel::Validations
      attr_reader :id, :currency
      attr_accessor :name, :amount, :interval, :interval_count, :trial_period_days

      validates_presence_of :id, :name, :amount
      validates_inclusion_of :interval, :in => %w(month year), :message => "'%{value}' is not one of 'month' or 'year'"

      def initialize(id)
        @id = id
        @currency = 'usd'
        @interval_count = 1
        @trial_period_days = 0
      end

      def finalize!
        validate!
        globalize!
      end

      def validate!
        fail InvalidPlanError, errors if invalid?
      end

      def globalize!
        Stripe::Plans[@id.to_s] = self
        Stripe::Plans.const_set(@id.to_s.upcase, self)
      end

      def put!
        if exists?
          puts "[EXISTS] - #{@id}"
        else
          plan = Stripe::Plan.create(
            :id => @id,
            :currency => @currency,
            :name => @name,
            :amount => @amount,
            :interval => @interval,
            :interval_count => @interval_count,
            :trial_period_days => @trial_period_days
          )
          puts "[CREATE] - #{plan}"
        end
      end

      def to_s
        @id.to_s
      end

      def exists?
        !!Stripe::Plan.retrieve("#{@id}")
      rescue Stripe::InvalidRequestError
        false
      end
    end

    class InvalidPlanError < StandardError
      attr_reader :errors

      def initialize(errors)
{'fraction_non_alphanumeric': 0.10628272251308901, 'fraction_numerical': 0.0010471204188481676, 'mean_word_length': 1.789051094890511, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '5492577', 'n_tokens_mistral': 1217, 'n_tokens_neox': 1167, 'n_words': 294}