<commit_before>module StateMachine
  
  class UndefinedTransition < NotImplementedError
  end
  
  def self.included(base)
    base.send(:include, StateMachine::EventDrivenNonDeterministic)
  end
  
  # A couple of utilities for converting between names and symbols.  Assumes
  # names are human friendly titleized strings and symbols are snakecased.
  def name_to_symbol(name)
    name.downcase.gsub(/ /, "_").to_sym
  end
  
  def symbol_to_name(symbol)
    symbol.to_s.titleize
  end
  
  module EventDrivenNonDeterministic
    def self.included(base)
      base.extend StateMethods
    end

    module StateMethods
      
      # SomeStatefulModel.has_state
      #
      # Added to some_stateful_model.rb will create a has_many association with the State model.
      #
      def has_state
        unless included_modules.include? InstanceMethods
          extend ClassMethods
          include InstanceMethods
        end
        write_inheritable_attribute :states, []
        write_inheritable_attribute :transitions, {}
        write_inheritable_attribute :initial_state, []
        
        class_inheritable_reader :initial_state
        
        has_many :states, :as => :stateful_entity, :dependent => :destroy
        
        after_create :set_initial_state, :run_initial_state_actions
      end
    end

    module ClassMethods
      
      # SomeStatefulModel.states or SomeStatefulModel.states(:state1, state2, ...)
      #
      # When invoked without arguments, returns the states that have been declared through
      # the SomeStatefulModel.state or SomeStatefulModel.event methods.
      #
      # When invoked with arguments, takes that argument as a list of states to declare.
      #
      def states(*states)
        return read_inheritable_attribute(:states) if states.empty?
        states.each do |state|
          state(state)
        end
      end
      
      # SomeStatefulModel.state
      #
      # Declares a state (stores it in the states array).  Not really needed except to declare a state as initial
      # since SomeStateModel.event will declare the states passed in through its transitions hash (:to, :from).
      #
      # In some_stateful_model.rb (made stateful by including the line 'has_state') add:
      #
      #   state :accepted, :initial => true
      #
      def state(state, options={}, &block)
        read_inheritable_attribute(:states) << state unless read_inheritable_attribute(:states).include? state
        read_inheritable_attribute(:initial_state) << state if options[:initial]
      end
      
      # SomeStatefulModel.event
      #
      # In some_stateful_model.rb (made stateful by including the line 'has_state') add:
      #
      #  event :accept, :to => :accepted, :from => [:submitted]
      #   # transitions (:to, :from) are required
      #
      # This will create the instance method SomeStatefulModel#accept!
      #
      def event(action, transitions, &block)
        transitions(:to => transitions[:to], :from => transitions[:from])
        states(transitions[:to],*transitions[:from])
        define_method("#{action.to_s}!") {
          self.current_state = transitions[:to]
        }
      end
      
      # SomeStatefulModel.transitions or SomeStatefulModel.transitions(:to => :some_state, :from => [from state list])
      #
      # When invoked without arguments, returns the transitions that have been defined through
      # the SomeStatefulModel.event methods.
      #
      # When invoked with arguments, takes that argument as a hash of transitions to define.
      #
      def transitions(options={})
        return read_inheritable_attribute(:transitions) if options.empty?
        options[:from].each do |from|
          read_inheritable_attribute(:transitions)[from] ||= []
          read_inheritable_attribute(:transitions)[from] << options[:to]
        end
      end
    end

    module InstanceMethods
      def set_initial_state
        self.current_state = self.class.initial_state
      end<commit_msg>Made bang (!) optional in the .event class method invocation, so now event :submit!, to =>... declares a submit! method, and event :submit, to =>... declares a submit method.<commit_after>module StateMachine
  
  class UndefinedTransition < NotImplementedError
  end
  
  def self.included(base)
    base.send(:include, StateMachine::EventDrivenNonDeterministic)
  end
  
  # A couple of utilities for converting between names and symbols.  Assumes
  # names are human friendly titleized strings and symbols are snakecased.
  def name_to_symbol(name)
    name.downcase.gsub(/ /, "_").to_sym
  end
  
  def symbol_to_name(symbol)
    symbol.to_s.titleize
  end
  
  module EventDrivenNonDeterministic
    def self.included(base)
      base.extend StateMethods
    end

    module StateMethods
      
      # SomeStatefulModel.has_state
      #
      # Added to some_stateful_model.rb will create a has_many association with the State model.
      #
      def has_state
        unless included_modules.include? InstanceMethods
          extend ClassMethods
          include InstanceMethods
        end
        write_inheritable_attribute :states, []
        write_inheritable_attribute :transitions, {}
        write_inheritable_attribute :initial_state, []
        
        class_inheritable_reader :initial_state
        
        has_many :states, :as => :stateful_entity, :dependent => :destroy
        
        after_create :set_initial_state, :run_initial_state_actions
      end
    end

    module ClassMethods
      
      # SomeStatefulModel.states or SomeStatefulModel.states(:state1, state2, ...)
      #
      # When invoked without arguments, returns the states that have been declared through
      # the SomeStatefulModel.state or SomeStatefulModel.event methods.
      #
      # When invoked with arguments, takes that argument as a list of states to declare.
      #
      def states(*states)
        return read_inheritable_attribute(:states) if states.empty?
        states.each do |state|
          state(state)
        end
      end
      
      # SomeStatefulModel.state
      #
      # Declares a state (stores it in the states array).  Not really needed except to declare a state as initial
      # since SomeStateModel.event will declare the states passed in through its transitions hash (:to, :from).
      #
      # In some_stateful_model.rb (made stateful by including the line 'has_state') add:
      #
      #   state :accepted, :initial => true
      #
      def state(state, options={}, &block)
        read_inheritable_attribute(:states) << state unless read_inheritable_attribute(:states).include? state
        read_inheritable_attribute(:initial_state) << state if options[:initial]
      end
      
      # SomeStatefulModel.event
      #
      # In some_stateful_model.rb (made stateful by including the line 'has_state') add:
      #
      #  event :accept, :to => :accepted, :from => [:submitted]
      #   # transitions (:to, :from) are required
      #
      # This will create the instance method SomeStatefulModel#accept!
      #
      def event(action, transitions, &block)
        transitions(:to => transitions[:to], :from => transitions[:from])
        states(transitions[:to],*transitions[:from])
        define_method("#{action.to_s}") {
          self.current_state = transitions[:to]
        }
      end
      
      # SomeStatefulModel.transitions or SomeStatefulModel.transitions(:to => :some_state, :from => [from state list])
      #
      # When invoked without arguments, returns the transitions that have been defined through
      # the SomeStatefulModel.event methods.
      #
      # When invoked with arguments, takes that argument as a hash of transitions to define.
      #
      def transitions(options={})
        return read_inheritable_attribute(:transitions) if options.empty?
        options[:from].each do |from|
          read_inheritable_attribute(:transitions)[from] ||= []
          read_inheritable_attribute(:transitions)[from] << options[:to]
        end
      end
    end

    module InstanceMethods
      def set_initial_state
        self.current_state = self.class.initial_state
      end
{'fraction_non_alphanumeric': 0.07995110024449878, 'fraction_numerical': 0.0004889975550122249, 'mean_word_length': 2.7068418667874945, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, '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': '1637999', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2190, 'n_words': 632}