Sha256: 10df336dd0262b2b370c95dd9f228e39ead53d3d552def4c40710680cec9bfc7

Contents?: true

Size: 1007 Bytes

Versions: 1

Compression:

Stored size: 1007 Bytes

Contents

# frozen_string_literal: true

module Buoys
  class Loader
    class << self
      #
      # buoy :account do
      #   link 'Account', account_path
      # end
      #
      # buoy :account_edit do |account|
      #   link 'Account Edit', edit_account_path(account)
      #   pre_buoy :account
      # end
      def buoy(key, &block)
        buoys[key] = block
      end
      alias crumb buoy

      def buoys
        @buoys ||= {}
      end

      def loaded_times
        @loaded_times ||= []
      end

      def load_buoys_files
        return unless need_reload?

        buoys.clear
        loaded_times.clear

        buoy_files.each do |file|
          instance_eval File.read(file), file
          loaded_times << File.mtime(file)
        end
      end

      def buoy_files
        Dir[*Buoys.buoy_file_paths]
      end

      def need_reload?
        return true if buoys.empty?

        Rails.env.development? && loaded_times != buoy_files.map {|f| File.mtime(f) }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
buoys-2.1.0 lib/buoys/loader.rb