class Toys::Loader::ToolData

Tool data

@private

Public Class Methods

new(words) click to toggle source

@private

# File lib/toys/loader.rb, line 659
def initialize(words)
  @words = validate_words(words)
  @definitions = {}
  @top_priority = @active_priority = nil
  @mutex = ::Monitor.new
end

Public Instance Methods

activate_tool(priority, loader) click to toggle source

@private

# File lib/toys/loader.rb, line 690
def activate_tool(priority, loader)
  @mutex.synchronize do
    return active_definition if @active_priority == priority
    return nil if @active_priority && @active_priority > priority
    @active_priority = priority
    get_tool(priority, loader)
  end
end
cur_definition() click to toggle source

@private

# File lib/toys/loader.rb, line 667
def cur_definition
  @mutex.synchronize { active_definition || top_definition }
end
empty?() click to toggle source

@private

# File lib/toys/loader.rb, line 672
def empty?
  @definitions.empty?
end
get_tool(priority, loader, tool_class = nil) click to toggle source

@private

# File lib/toys/loader.rb, line 677
def get_tool(priority, loader, tool_class = nil)
  @mutex.synchronize do
    if @top_priority.nil? || @top_priority < priority
      @top_priority = priority
    end
    if tool_class && @definitions.include?(priority)
      raise ToolDefinitionError, "Tool already defined for #{@words.inspect}"
    end
    @definitions[priority] ||= loader.build_tool(@words, priority, tool_class)
  end
end

Private Instance Methods

active_definition() click to toggle source
# File lib/toys/loader.rb, line 713
def active_definition
  @active_priority ? @definitions[@active_priority] : nil
end
top_definition() click to toggle source
# File lib/toys/loader.rb, line 709
def top_definition
  @top_priority ? @definitions[@top_priority] : nil
end
validate_words(words) click to toggle source
# File lib/toys/loader.rb, line 701
def validate_words(words)
  words.each do |word|
    if /[[:cntrl:] #"$&'()*;<>\[\\\]\^`{|}]/.match(word)
      raise ToolDefinitionError, "Illegal characters in name #{word.inspect}"
    end
  end
end