Sha256: 51db0e1199574ece499f84d67773a6006267a23e58864ba741f128a0d659c6b5

Contents?: true

Size: 1.33 KB

Versions: 14

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module KepplerFrontend
  module Views
    # CodeHandler
    class RoutesHandler
      def initialize(view_data)
        @view = view_data
      end

      def install
        routes_file = File.readlines(config.routes)
        idx = code_search(routes_file).search_line(flag_point)
        routes_file.insert(idx.to_i + 1, template)
        routes_file = routes_file.join('')
        File.write(config.routes, routes_file)
        true
      rescue StandardError
        false
      end

      def uninstall
        routes_file = File.readlines(config.routes)
        idx = code_search(routes_file).search_line(template)
        return if idx.zero?
        routes_file.delete_at(idx.to_i)
        routes_file = routes_file.join('')
        File.write(config.routes, routes_file)
        true
      rescue StandardError
        false
      end

      private

      def config
        KepplerFrontend::Urls::Config.new
      end

      def flag_point
        'KepplerFrontend::Engine.routes.draw do'
      end

      def template
        active = @view.active.eql?(false) ? '#' : ''
        "#{active}  #{@view.method.downcase} '#{@view.url}'," \
            " to: 'app/frontend##{@view.name}', as: :#{@view.name}\n"
      end

      def code_search(html)
        KepplerFrontend::Utils::CodeSearch.new(html)
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
keppler-2.1.18 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/views/routes_handler.rb
keppler-2.1.17 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/views/routes_handler.rb
keppler-2.1.16 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/views/routes_handler.rb
keppler-2.1.15 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/views/routes_handler.rb
keppler-2.1.14 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/views/routes_handler.rb
keppler-2.1.13 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/views/routes_handler.rb
keppler-2.1.12 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/views/routes_handler.rb
keppler-2.1.11 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/views/routes_handler.rb
keppler-2.1.10 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/views/routes_handler.rb
keppler-2.1.9 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/views/routes_handler.rb
keppler-2.1.8 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/views/routes_handler.rb
keppler-2.1.7 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/views/routes_handler.rb
keppler-2.1.6 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/views/routes_handler.rb
keppler-2.1.5 installer/core/rockets/keppler_frontend/app/services/keppler_frontend/views/routes_handler.rb