-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
554 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# frozen_string_literal: true | ||
|
||
# Index for searching value from config.yml | ||
module Fusuma | ||
class Config | ||
# Search config.yml | ||
class Searcher | ||
def initialize | ||
@cache | ||
end | ||
|
||
# @param index [Index] | ||
# @param location [Hash] | ||
def search(index, location:) | ||
key = index.keys.first | ||
return location if key.nil? | ||
|
||
return nil if location.nil? | ||
|
||
return nil unless location.is_a?(Hash) | ||
|
||
next_index = Index.new(index.keys[1..-1]) | ||
next_locations = [ | ||
location[key.symbol], | ||
Searcher.use_fallback && key.fallback && location[key.fallback], | ||
Searcher.use_skip && key.skippable && location | ||
].compact | ||
|
||
value = nil | ||
next_locations.find do |next_location| | ||
value = search(next_index, location: next_location) | ||
end | ||
value | ||
end | ||
|
||
def search_with_cache(index, location:) | ||
cache([index.cache_key, Searcher.use_skip, Searcher.use_fallback]) do | ||
search(index, location: location) | ||
end | ||
end | ||
|
||
def cache(key) | ||
@cache ||= {} | ||
key = key.join(',') if key.is_a? Array | ||
if @cache.key?(key) | ||
@cache[key] | ||
else | ||
@cache[key] = block_given? ? yield : nil | ||
end | ||
end | ||
|
||
class << self | ||
attr_reader :use_fallback, :use_skip | ||
|
||
# switch context for fallback | ||
def fallback(&block) | ||
@use_fallback = true | ||
result = block.call | ||
@use_fallback = false | ||
result | ||
end | ||
|
||
def skip(&block) | ||
@use_skip = true | ||
result = block.call | ||
@use_skip = false | ||
result | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative './buffer.rb' | ||
|
||
module Fusuma | ||
module Plugin | ||
module Buffers | ||
# manage events and generate command | ||
class TimerBuffer < Buffer | ||
DEFAULT_SOURCE = 'timer_input' | ||
DEFAULT_SECONDS_TO_KEEP = 60 | ||
|
||
def config_param_types | ||
{ | ||
'source': [String], | ||
'seconds_to_keep': [Float, Integer] | ||
} | ||
end | ||
|
||
# @param event [Event] | ||
# @return [Buffer, NilClass] | ||
def buffer(event) | ||
return if event&.tag != source | ||
|
||
@events.push(event) | ||
self | ||
end | ||
|
||
def clear_expired(current_time: Time.now) | ||
@seconds_to_keep ||= (config_params(:seconds_to_keep) || DEFAULT_SECONDS_TO_KEEP) | ||
@events.each do |e| | ||
break if current_time - e.time < @seconds_to_keep | ||
|
||
MultiLogger.debug("#{self.class.name}##{__method__}") | ||
|
||
@events.delete(e) | ||
end | ||
end | ||
|
||
def empty? | ||
@events.empty? | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.