class Pleiades::Command::RoutingProxy

Attributes

mount_pairs[R]
routers[R]

Public Class Methods

collect_router_file(event) click to toggle source

読み込む router_files を決める。

@param [Line::Bot::Event] Eventクラス

@return [Array<Pathname, …>] 読み込むファイル一覧

# File lib/pleiades/core/command/routing_proxy.rb, line 21
def collect_router_file(event)
  @event = event
  proxy_exists? ? load(proxy_file) : routing

  collect!
end
new(event) click to toggle source
# File lib/pleiades/core/command/routing_proxy.rb, line 45
def initialize(event)
  @event = event
  @routers = []
  @mount_pairs = {}

  mount default: Pleiades::Constants::File::CONFIG_DIR_PATH
end
routing(&block) click to toggle source
# File lib/pleiades/core/command/routing_proxy.rb, line 9
def routing(&block)
  @_ = new(@event)

  block_given? ? @_.instance_eval(&block) : @_.default_routing
end

Private Class Methods

collect!() click to toggle source
# File lib/pleiades/core/command/routing_proxy.rb, line 38
def collect!
  @_.routers
end
proxy_exists?() click to toggle source
# File lib/pleiades/core/command/routing_proxy.rb, line 30
def proxy_exists?
  FileTest.exist?(proxy_file)
end
proxy_file() click to toggle source
# File lib/pleiades/core/command/routing_proxy.rb, line 34
def proxy_file
  Pleiades::Constants::File::ROUTING_PROXY
end

Public Instance Methods

add(router_name, mnt_key: :default) click to toggle source
# File lib/pleiades/core/command/routing_proxy.rb, line 57
      def add(router_name, mnt_key: :default)
        no_keyword_err = lambda { |key|
          <<~MES
            from #{__FILE__}:#{__LINE__ - 1}:in `#{__method__}`:
              Unmounted key `#{key}`. Please call `mount #{key}: 'path/to/router'` before `#{__method__}`.
          MES
        }
        dir = @mount_pairs.fetch(mnt_key) { |key| raise no_keyword_err.call(key) }

        @routers << "#{dir}/#{router_name}.rb"
      end
default_router() click to toggle source
# File lib/pleiades/core/command/routing_proxy.rb, line 102
def default_router
  :router
end
default_routing() click to toggle source
# File lib/pleiades/core/command/routing_proxy.rb, line 53
def default_routing
  add default_router
end
mount(**paths) click to toggle source

`add`で指定するキーワードを登録する。

@param [symbol_name: path, …]

symbol_name : キーワード
path        : プロジェクトホームからの相対パスまたは、絶対パス

## EXAMPLE mount hoge: 'path/to/hoge' add :fuga, mnt: :hoge # => path/to/hoge/fuga.rb

# File lib/pleiades/core/command/routing_proxy.rb, line 80
def mount(**paths)
  validate_mount_keys(paths.keys)

  paths.each_pair do |symbol, path|
    @mount_pairs.store(symbol, path =~ %r{(\S+)/$} ? $1 : path)
  end
end
unmount(*mnt_symbols) click to toggle source
# File lib/pleiades/core/command/routing_proxy.rb, line 88
def unmount(*mnt_symbols)
  return @mount_pairs.clear && nil if mnt_symbols.include?(:all)

  warn = ->(key) { "\nWarning from #{__FILE__}:#{__LINE__ - 3}:in `#{__method__}`: `#{key}` is unmounted." }

  mnt_symbols.each do |symbol|
    @mount_pairs.delete(symbol) { |sym| puts warn.call(sym) }
  end
end
unmount_all() click to toggle source
# File lib/pleiades/core/command/routing_proxy.rb, line 98
def unmount_all
  unmount :all
end

Private Instance Methods

mount_reserved_word?(word) click to toggle source
# File lib/pleiades/core/command/routing_proxy.rb, line 117
def mount_reserved_word?(word)
  %i[all].include?(word.to_sym)
end
mounted?(key) click to toggle source
# File lib/pleiades/core/command/routing_proxy.rb, line 121
def mounted?(key)
  @mount_pairs.key?(key)
end
validate_mount_keys(keys) click to toggle source
# File lib/pleiades/core/command/routing_proxy.rb, line 108
def validate_mount_keys(keys)
  keys.each do |key|
    raise "`#{key}` is reserved words. Please specify another key name." if mount_reserved_word?(key)

    raise "`#{key}` is already mounted." if mounted?(key)
  end
  true
end