Module: NeuronCheckSystem::DeclarationMethods
- Defined in:
- lib/neuroncheck/declaration.rb
Overview
宣言用のメソッドやメソッド追加時の処理を定義したモジュール。NeuronCheckを行いたい対象のモジュールやクラスにextendすることで使用する
Instance Method Summary collapse
-
#__neuroncheck_ndecl_main(expecteds, block, declared_caller_locations) ⇒ Object
ndeclのメイン処理.
-
#__neuroncheck_ndecl_main_with_block(block, declared_caller_locations) ⇒ Object
ndeclの通常記法.
-
#__neuroncheck_ndecl_main_without_block(expecteds, declared_caller_locations) ⇒ Object
ndeclの短縮記法.
-
#ndecl(*expecteds, &block) ⇒ Object
(also: #ncheck, #ndeclare, #nsig, #ntypesig)
宣言を実行.
Instance Method Details
#__neuroncheck_ndecl_main(expecteds, block, declared_caller_locations) ⇒ Object
ndeclのメイン処理
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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) ⇒ Object
ndeclの通常記法
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# 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) ⇒ Object
ndeclの短縮記法
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# 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) ⇒ Object Also known as: ncheck, ndeclare, nsig, ntypesig
宣言を実行
10 11 12 13 14 15 16 17 18 |
# 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 |