module Shaf::RegistrableFactory

Public Instance Methods

all() click to toggle source
# File lib/shaf/registrable_factory.rb, line 6
def all
  reg.dup
end
create(*params, **options) click to toggle source
# File lib/shaf/registrable_factory.rb, line 42
def create(*params, **options)
  clazz = lookup(*params)
  raise NotFoundError.new(%Q(Command '#{ARGV}' is not supported)) unless clazz

  args = init_args(clazz, params)
  clazz.new(*args, **options)
end
each() { |c| ... } click to toggle source
# File lib/shaf/registrable_factory.rb, line 10
def each
  return all.each unless block_given?
  all.each { |c| yield c }
end
lookup(*str) click to toggle source
# File lib/shaf/registrable_factory.rb, line 28
def lookup(*str)
  return if str.empty? || !str.all?
  reg.select { |clazz| matching_class? str, clazz }
    .sort_by(&method(:identifier_count))
    .last
end
register(clazz) click to toggle source
# File lib/shaf/registrable_factory.rb, line 19
def register(clazz)
  reg << clazz
end
size() click to toggle source
# File lib/shaf/registrable_factory.rb, line 15
def size
  reg.size
end
unregister(*str) click to toggle source
# File lib/shaf/registrable_factory.rb, line 23
def unregister(*str)
  return if str.empty? || !str.all?
  reg.delete_if { |clazz| matching_class? str, clazz }
end
usage() click to toggle source
# File lib/shaf/registrable_factory.rb, line 35
def usage
  reg.compact.map do |entry|
    usage = entry.instance_variable_get(:@usage)
    usage.respond_to?(:call) ? usage.call : usage
  end
end

Private Instance Methods

identifier_count(clazz) click to toggle source
# File lib/shaf/registrable_factory.rb, line 69
def identifier_count(clazz)
  clazz.instance_variable_get(:@identifiers)&.size || 0
end
init_args(clazz, params) click to toggle source
# File lib/shaf/registrable_factory.rb, line 73
def init_args(clazz, params)
  first_non_id = identifier_count(clazz)
  params[first_non_id..-1]
end
matching_class?(strings, clazz) click to toggle source
# File lib/shaf/registrable_factory.rb, line 56
def matching_class?(strings, clazz)
  identifiers = clazz.instance_variable_get(:@identifiers)
  return false if strings.size < identifiers.size
  identifiers.zip(strings).all? { |pattern, str| matching_identifier? str, pattern }
end
matching_identifier?(str, pattern) click to toggle source
# File lib/shaf/registrable_factory.rb, line 62
def matching_identifier?(str, pattern)
  return false if pattern.nil? || str.nil? || str.empty?
  pattern = pattern.to_s if pattern.is_a? Symbol
  return str == pattern if pattern.is_a? String
  str.match(pattern) || false
end
reg() click to toggle source
# File lib/shaf/registrable_factory.rb, line 52
def reg
  @reg ||= []
end