<commit_before>module Activr
  module Generators

    class ActivityGenerator < Rails::Generators::NamedBase
      DEFAULT_ENTITY_FIELD = :name

      source_root File.expand_path("../templates", __FILE__)

      check_class_collision :suffix => "Activity"

      argument :entities, :type => :array, :default => [], :banner => "entity[:class] entity[:class]"
      class_option :full, type: :boolean, :desc => "Generate full class"

      desc "Creates an Activity class"

      def create_activity_file
        template "activity.rb", "app/activities/#{file_name}_activity.rb"
      end

      def entities_infos
        entities.map do |str|
          ary = str.split(':')
          raise "Erroneous entity argument: #{str}" unless (ary.size == 2)

          {
            :name  => ary[0].underscore,
            :class => ary[1].camelize,
          }
        end
      end

      def humanization
        result = ""

        actor = entities_infos.find{ |entity| entity[:name] == 'actor' }
        if actor
          result += "{{actor.#{_resolve_entity_field(actor)}}} "
        end

        result += name.underscore.gsub('_', ' ')

        entities_infos.each do |entity|
          if entity[:name] != 'actor'
            result += " {{#{entity[:name]}.#{_resolve_entity_field(entity)}}}"
          end
        end

        result
      end

      private

      def _resolve_entity_field(entity_infos)
        klass = entity_infos[:class].constantize

        field = [ :fullname, :name, :title ].find do |meth_name|
          klass.method_defined?(meth_name)
        end

        field || DEFAULT_ENTITY_FIELD
      end
    end

  end
end
<commit_msg>activitu generate: be smarter<commit_after>module Activr
  module Generators

    class ActivityGenerator < Rails::Generators::NamedBase
      DEFAULT_ENTITY_FIELD = :name

      source_root File.expand_path("../templates", __FILE__)

      check_class_collision :suffix => "Activity"

      argument :entities, :type => :array, :default => [], :banner => "entity[:class] entity[:class]"
      class_option :full, type: :boolean, :desc => "Generate full class"

      desc "Creates an Activity class"

      def create_activity_file
        template "activity.rb", "app/activities/#{file_name}_activity.rb"
      end

      def entities_infos
        entities.map do |str|
          ary = str.split(':')

          name  = ary[0].underscore
          klass = (ary[1] || ary[0]).camelize

          {
            :name  => name,
            :class => klass,
          }
        end
      end

      def humanization
        result = ""

        actor = entities_infos.find{ |entity| entity[:name] == 'actor' }
        if actor
          result += "{{actor.#{_resolve_entity_field(actor)}}} "
        end

        result += name.underscore.gsub('_', ' ')

        entities_infos.each do |entity|
          if entity[:name] != 'actor'
            result += " {{#{entity[:name]}.#{_resolve_entity_field(entity)}}}"
          end
        end

        result
      end

      private

      def _resolve_entity_field(entity_infos)
        klass = entity_infos[:class].constantize

        field = [ :fullname, :name, :title ].find do |meth_name|
          klass.method_defined?(meth_name)
        end

        field || DEFAULT_ENTITY_FIELD
      end
    end

  end
end

{'fraction_non_alphanumeric': 0.12210526315789473, 'fraction_numerical': 0.0018045112781954887, 'mean_word_length': 2.155597722960152, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '6976302', 'n_tokens_mistral': 1011, 'n_tokens_neox': 978, 'n_words': 220}