Sha256: a24d7515254ee67507b559d91faf626ebf7b744deb1ff2b0145c2d590b05e7fe

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

module ServerGemListen
  module Listener
    class << self
      def start
        listen_events.start
        sleep
      rescue Interrupt => e
        puts "\n#{ServerGemListen::NOTIFICATION_PREFIX} interrupted".green
      end

      private

      def notify(modified, added, removed)
        files = { modified: modified, added: added, removed: removed }
        files.each_pair do |key, value|
          next if value.empty?
          value.map! { |v| v.gsub(ENV['GEM_HOME'], '') }
          puts "#{ServerGemListen::NOTIFICATION_PREFIX} #{'file'.pluralize(value.size)} #{key}".green
          ap value
        end
      end

      def listen_events
        gems = Bundler.definition.specs.to_hash.keys
        Listen.to(ENV['GEM_HOME']) do |*args|
          process_event(gems, *args)
        end
      end

      def process_event(gems, *args)
        return unless need_restart?(gems, *args)
        notify(*args)
        Server.restart
      end

      def need_restart?(gems, *args)
        args.each do |arg|
          arg.each do |path|
            matches = gems.select { |gem| path.include? gem }
            return true unless matches.empty?
          end
        end
        false
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
server-gem-listen-0.2.5 lib/server_gem_listen/listener.rb
server-gem-listen-0.2.4 lib/server_gem_listen/listener.rb
server-gem-listen-0.2.3 lib/server_gem_listen/listener.rb
server-gem-listen-0.2.2 lib/server_gem_listen/listener.rb