Sha256: 724f8efb4cb178bef1aef2e3543cb30c5c0b781df5fcbb366e274574396756ca

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

# encoding: utf-8

require "hexx-cli"

require_relative "cli/attribute"

module Policy

  # Scaffolds a policy with a specification and translations
  #
  # @api private
  class CLI < Hexx::CLI::Base

    # @private
    def self.source_root
      ::File.expand_path "../cli", __FILE__
    end

    desc "Scaffolds a policy object with a specification and translations."
    namespace :new

    argument(
      :name,
      banner: "NAME",
      desc: "The name of the policy.",
      type: :string
    )

    class_option(
      :attributes,
      aliases: %w(-a),
      banner: " debet{Transaction} credit{Transaction}",
      default: %w(),
      desc: "The list of policy object's attributes.",
      type: :array
    )

    class_option(
      :namespace,
      aliases: %w(-n),
      banner: " policies financial",
      default: %w(policies),
      desc: "The namespace for the policy.",
      type: :array
    )

    class_option(
      :locales,
      aliases: %w(-l),
      banner:  " en jp",
      default: %w(en ru),
      desc: "The list of locales for policy messages' translations.",
      type: :array
    )

    # @private
    def add_spec
      template "spec.erb", "spec/tests/#{ policy.path }_spec.rb"
    end

    # @private
    def add_policy
      template "policy.erb", "app/#{ policy.path }.rb"
    end

    # @private
    def add_locales
      locales.each do |locale|
        @locale = locale
        template "locale.erb", "config/locales/#{ policy.path }/#{ locale }.yml"
      end
    end

    private

    def project
      @project ||= Hexx::CLI::Name.new ::File.basename(destination_root)
    end

    def policy
      @policy ||= Hexx::CLI::Name.new [options[:namespace], name].join("/")
    end

    def attributes
      @attributes ||= options[:attributes].map(&Attribute.method(:new))
    end

    def locales
      @locales ||= options[:locales].map(&:downcase)
    end

  end # class CLI

end # module Policy

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
policy-2.0.0 lib/policy/cli.rb