Sha256: 50b4b6e5ab26460bee7d0d7062a2833dcd0265813d5ab2f3eb88d3d3334e8e3b

Contents?: true

Size: 1.33 KB

Versions: 12

Compression:

Stored size: 1.33 KB

Contents

require 'listen'

module Locomotive::Wagon
  class Listen

    attr_accessor :reader

    def self.instance
      @@instance = new
    end

    def start(reader)
      self.reader = reader

      self.definitions.each do |definition|
        self.apply(definition)
      end
    end

    def definitions
      [
        ['config', /\.yml/, [:site, :content_types, :pages, :snippets, :content_entries, :translations]],
        ['app/views', /\.liquid/, [:pages, :snippets]],
        ['app/content_types', /\.yml/, [:content_types, :content_entries]],
        ['data', /\.yml/, :content_entries]
      ]
    end

    protected

    def apply(definition)
      reloader = Proc.new do |modified, added, removed|
        resources = [*definition.last]
        names     = resources.map { |n| "\"#{n}\"" }.join(', ')

        Locomotive::Wagon::Logger.info "* Reloaded #{names} at #{Time.now}"

        begin
          reader.reload(resources)
        rescue Exception => e
          Locomotive::Wagon::MounterException.new('Unable to reload', e)
        end
      end

      filter  = definition[1]
      path    = File.join(self.reader.mounting_point.path, definition.first)
      path    = File.expand_path(path)

      listener = ::Listen.to(path).filter(filter).change(&reloader)

      # non blocking listener
      listener.start(false)
    end

  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
locomotivecms_wagon-1.4.0 lib/locomotive/wagon/listen.rb
locomotivecms_wagon-1.3.3 lib/locomotive/wagon/listen.rb
locomotivecms_wagon-1.3.2 lib/locomotive/wagon/listen.rb
locomotivecms_wagon-1.3.1 lib/locomotive/wagon/listen.rb
locomotivecms_wagon-1.3.0 lib/locomotive/wagon/listen.rb
locomotivecms_wagon-1.2.2 lib/locomotive/wagon/listen.rb
locomotivecms_wagon-1.2.1 lib/locomotive/wagon/listen.rb
locomotivecms_wagon-1.2.0 lib/locomotive/wagon/listen.rb
locomotivecms_wagon-1.1.0 lib/locomotive/wagon/listen.rb
locomotivecms_wagon-1.0.2 lib/locomotive/wagon/listen.rb
locomotivecms_wagon-1.0.1 lib/locomotive/wagon/listen.rb
locomotivecms_wagon-1.0.0 lib/locomotive/wagon/listen.rb