Sha256: cf7ab143a7d65438cd8ed7ef4a0aff6e177700cadc469793c5424106f994886d

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

module EnumerateIt
  module Generators
    class EnumGenerator < Rails::Generators::NamedBase
      source_root File.expand_path('templates', __dir__)

      argument :attributes, type: :hash

      class_option :singular, type: 'string', desc: 'Singular name for i18n'

      class_option :lang, type: 'string', desc: 'Language to use in i18n', default: 'en'

      desc 'Creates a locale file on config/locales'
      def create_locale
        template 'locale.yml', File.join('config/locales', "#{singular_name}.yml")
      end

      desc 'Creates the enumeration'
      def create_enumerate_it
        template 'enumerate_it.rb', File.join('app/enumerations', "#{singular_name}.rb")
      end

      private

      def default_lang
        options[:lang]
      end

      def singular
        singular_name
      end

      def fields
        if attributes.empty?
          args
        elsif attributes.first.type == :string
          attributes.map(&:name)
        else
          attributes.map { |attribute| [attribute.name, attribute.type] }
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
enumerate_it-4.0.0 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-3.3.0 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-3.2.4 lib/generators/enumerate_it/enum/enum_generator.rb