module NeuronCheckSystem::DeclarationMethods
宣言用のメソッドやメソッド追加時の処理を定義したモジュール。NeuronCheckを行いたい対象のモジュールやクラスにextendすることで使用する
Public Instance Methods
__neuroncheck_ndecl_main(expecteds, block, declared_caller_locations)
click to toggle source
ndeclのメイン処理
# File lib/neuroncheck/declaration.rb, line 27 def __neuroncheck_ndecl_main(expecteds, block, declared_caller_locations) # 2回連続で宣言された場合はエラー if @__neuron_check_last_declaration then raise DeclarationError, "repeated declarations - Declaration block and method definition must correspond one-to-one" end # ブロックが渡されたかどうかで処理を分岐 if block then # ブロックが渡された場合 __neuroncheck_ndecl_main_with_block(block, declared_caller_locations) else # 短縮記法はNeuronCheckSyntax使用可能時のみ unless defined?(NeuronCheckSyntax) then raise DeclarationError, "NeuronCheck shorthand syntax (without block) can be used only in Ruby 2.1 or later" end # ブロックが渡されていない場合 (短縮記法) __neuroncheck_ndecl_main_without_block(expecteds, declared_caller_locations) end end
__neuroncheck_ndecl_main_with_block(block, declared_caller_locations)
click to toggle source
ndeclの通常記法
# File lib/neuroncheck/declaration.rb, line 49 def __neuroncheck_ndecl_main_with_block(block, declared_caller_locations) # 宣言ブロック実行用のコンテキストを作成 context = NeuronCheckSystem::DeclarationContext.new # 宣言ブロックの内容を実行 context.instance_eval(&block) # 呼び出し場所を記憶 context.declaration.declared_caller_locations = declared_caller_locations # 宣言の内容を「最後の宣言」として保持 @__neuron_check_last_declaration = context.declaration end
__neuroncheck_ndecl_main_without_block(expecteds, declared_caller_locations)
click to toggle source
ndeclの短縮記法
# File lib/neuroncheck/declaration.rb, line 64 def __neuroncheck_ndecl_main_without_block(expecteds, declared_caller_locations) # 宣言ブロック実行用のコンテキストを作成 context = NeuronCheckSystem::DeclarationContext.new # 引数の解釈 expected_args = nil expected_return = nil if expecteds.last.kind_of?(Hash) and expecteds.last.size == 1 then # expectedsの最後が、値が1つだけ格納されたHashであれば、キーを最後の引数、値を戻り値と解釈する # 例: String, String => Numeric last_hash = expecteds.pop expected_args = expecteds.concat([last_hash.keys.first]) expected_return = last_hash.values.first else # 上記以外の場合はすべて引数と見なす expected_args = expecteds end # 引数1つで、かつ空配列が渡された場合は、「引数なし」と宣言されたとみなす if expected_args[0].kind_of?(Array) and expected_args.size == 1 then expected_args = [] end # 簡易宣言を実行 context.instance_eval do unless expected_args.empty? then args *expected_args end if expected_return then returns expected_return end end # 短縮記法フラグON context.declaration.shorthand = true # 呼び出し場所を記憶 context.declaration.declared_caller_locations = declared_caller_locations context.declaration.arg_matchers.each do |matcher| matcher.declared_caller_locations = context.declaration.declared_caller_locations end if context.declaration.return_matcher then context.declaration.return_matcher.declared_caller_locations = context.declaration.declared_caller_locations end # 宣言の内容を「最後の宣言」として保持 (通常のndeclと同じ) @__neuron_check_last_declaration = context.declaration end
ndecl(*expecteds, &block)
click to toggle source
宣言を実行
# File lib/neuroncheck/declaration.rb, line 10 def ndecl(*expecteds, &block) # 未初期化の場合、NeuronCheck用の初期化を自動実行 unless @__neuron_check_initialized then NeuronCheckSystem.initialize_module_for_neuron_check(self) end # メイン処理実行 __neuroncheck_ndecl_main(expecteds, block, caller(1, 1)) end