Sha256: eacac1d6b5f3eb976a49b154d3916455de7069a10648544ae91fde634687dad4

Contents?: true

Size: 1.2 KB

Versions: 20

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module I18n::Tasks
  module Command
    module DSL
      def self.included(base)
        base.module_eval do
          @dsl = Hash.new { |h, k| h[k] = {} }
          extend ClassMethods
        end
      end

      def t(*args, **kwargs)
        I18n.t(*args, **kwargs)
      end

      module ClassMethods
        def cmd(name, conf = nil)
          if conf
            conf        = conf.dup
            conf[:args] = (conf[:args] || []).map { |arg| arg.is_a?(Symbol) ? arg(arg) : arg }
            dsl(:cmds)[name] = conf
          else
            dsl(:cmds)[name]
          end
        end

        def arg(ref, *args)
          if args.present?
            dsl(:args)[ref] = args
          else
            dsl(:args)[ref]
          end
        end

        def cmds
          dsl(:cmds)
        end

        def dsl(key)
          @dsl[key]
        end

        # late-bound I18n.t for module bodies
        def t(*args, **kwargs)
          proc { I18n.t(*args, **kwargs) }
        end

        # if class is a module, merge DSL definitions when it is included
        def included(base)
          base.instance_variable_get(:@dsl).deep_merge!(@dsl)
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/i18n-tasks-1.0.14/lib/i18n/tasks/command/dsl.rb
i18n-tasks-1.0.13 lib/i18n/tasks/command/dsl.rb
i18n-tasks-1.0.12 lib/i18n/tasks/command/dsl.rb
i18n-tasks-1.0.11 lib/i18n/tasks/command/dsl.rb
i18n-tasks-1.0.10 lib/i18n/tasks/command/dsl.rb
i18n-tasks-1.0.9 lib/i18n/tasks/command/dsl.rb
i18n-tasks-1.0.8 lib/i18n/tasks/command/dsl.rb
i18n-tasks-1.0.7 lib/i18n/tasks/command/dsl.rb
i18n-tasks-1.0.6 lib/i18n/tasks/command/dsl.rb
i18n-tasks-1.0.5 lib/i18n/tasks/command/dsl.rb
i18n-tasks-1.0.4 lib/i18n/tasks/command/dsl.rb
i18n-tasks-1.0.3 lib/i18n/tasks/command/dsl.rb
i18n-tasks-1.0.2 lib/i18n/tasks/command/dsl.rb
i18n-tasks-1.0.1 lib/i18n/tasks/command/dsl.rb
i18n-tasks-1.0.0 lib/i18n/tasks/command/dsl.rb
i18n-youdao-tasks-0.9.37 lib/i18n/tasks/command/dsl.rb
i18n-tasks-0.9.37 lib/i18n/tasks/command/dsl.rb
i18n-tasks-0.9.36 lib/i18n/tasks/command/dsl.rb
i18n-tasks-0.9.35 lib/i18n/tasks/command/dsl.rb
i18n-tasks-0.9.34 lib/i18n/tasks/command/dsl.rb