Sha256: 866d04b2fa114645a28c60ebb2b6218f149e142b7305d28cc5f13c19ebcd8105

Contents?: true

Size: 1.9 KB

Versions: 23

Compression:

Stored size: 1.9 KB

Contents

require 'thor'
require 'active_support/all'

module Gris
  module Generators
    class ApiGenerator < Thor::Group
      include Thor::Actions
      argument :name

      def name_underscore
        name.underscore
      end

      def name_tableize
        name.tableize
      end

      def output_directory
        '.'
      end

      def path_to_application_endpoint
        "#{output_directory}/app/endpoints/application_endpoint.rb"
      end

      def path_to_root_presenter
        "#{output_directory}/app/presenters/root_presenter.rb"
      end

      def append_endpoint_to_application_endpoint
        say 'Mounting new endpoint on ApplicationEndpoint.'
        insert_into_file path_to_application_endpoint, after: "# Additional mounted endpoints\n" do
          text =  "  mount #{name.classify.pluralize}Endpoint\n"
          text
        end
      end

      def append_endpoint_links_to_root_presenter
        say 'Appending links to RootPresenter.'
        insert_into_file path_to_root_presenter, after: "# Additional endpoint links\n" do
          text =  "\n"
          text << "  link :#{name_tableize} do |opts|\n"
          text << "    {\n"
          text << '      href: "#{base_url(opts)}/'
          text << "#{name_tableize}{?page,size}\",\n"
          text << "      templated: true\n"
          text << "    }\n"
          text << "  end\n"
          text << "\n"
          text << "  link :#{name_underscore} do |opts|\n"
          text << "    {\n"
          text << '      href: "#{base_url(opts)}/'
          text << "#{name_tableize}/{id}\",\n"
          text << "      templated: true\n"
          text << "    }\n"
          text << "  end\n"
          text
        end
      end

      def api
        self.class.source_root "#{File.dirname(__FILE__)}/templates/api"
        say 'Generating api...'
        directory '.', output_directory
        say 'API files created!', :green
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
gris-0.0.9 lib/gris/generators/api_generator.rb
gris-0.0.8 lib/gris/generators/api_generator.rb
gris-0.0.7 lib/gris/generators/api_generator.rb