class FakeFlorence::FeatureCache

Attributes

root[R]

Public Class Methods

new(path) click to toggle source
# File lib/fake_florence/feature_cache.rb, line 7
def initialize(path)
  @root = path.join('features')
  @root.mkpath
  @callbacks = []
end

Public Instance Methods

announce_all_now() click to toggle source
# File lib/fake_florence/feature_cache.rb, line 30
def announce_all_now
  names = []

  Announcers.load(Config.announce).each do |announcer|
    names.push(announcer.name) if announcer.announce(noop: features)
  end

  { features: features.size, announcers: names }
end
feature(id) click to toggle source
# File lib/fake_florence/feature_cache.rb, line 17
def feature(id)
  Feature.read(id, id_to_file(id))
end
features() click to toggle source
# File lib/fake_florence/feature_cache.rb, line 13
def features
  Dir.glob(@root.join('*.yaml')).map(&method(:file_to_feature))
end
id_to_file(id) click to toggle source
# File lib/fake_florence/feature_cache.rb, line 26
def id_to_file(id)
  @root.join("#{id}.yaml")
end
register_listener(&block) click to toggle source
# File lib/fake_florence/feature_cache.rb, line 21
def register_listener(&block)
  @callbacks.push(block)
  listen_for_changes unless @listener
end

Private Instance Methods

file_to_feature(file) click to toggle source
# File lib/fake_florence/feature_cache.rb, line 63
def file_to_feature(file)
  Feature.read(file_to_id(file), file)
end
file_to_id(file) click to toggle source
# File lib/fake_florence/feature_cache.rb, line 71
def file_to_id(file)
  File.basename(file, '.yaml')
end
file_to_stub_feature(file) click to toggle source
# File lib/fake_florence/feature_cache.rb, line 67
def file_to_stub_feature(file)
  Feature.new('id' => file_to_id(file))
end
listen_for_changes() click to toggle source
# File lib/fake_florence/feature_cache.rb, line 42
def listen_for_changes
  @listener = Listen.to(@root.to_s, only: /.yaml$/) do |updated, created, deleted|
    features_map = {
      create: created.map(&method(:file_to_feature)),
      update: updated.map(&method(:file_to_feature)),
      delete: deleted.map(&method(:file_to_stub_feature)),
    }

    features_map.each_pair do |type, fs|
      next if fs.empty?
      Config.log.debug "Features #{type}d: #{fs.map(&:id).join(', ')}"
    end

    @callbacks.each do |callback|
      callback.call(features_map)
    end
  end

  @listener.start
end