Class: AlController
- Inherits:
-
Object
- Object
- AlController
- Defined in:
- lib/al_controller.rb,
lib/al_mif.rb
Overview
Aloneコントローラクラス
コントローラ名、ステート名、アクション名、この3つのパラメータにより、 全体を駆動する。 コントローラは、パラメータ ctrl= で、アクションは、action= で指定される。 ただし、これらパラメータのパースはメインモジュールにて行い、ここでは、 その値のエイリアスをもらっている。 併せて、(ちょっとした工夫により)コントローラごとに名前空間分離した 専用セッション変数を持たせる機能もインプリメントしてある。
Defined Under Namespace
Classes: AlControllerSession
Constant Summary collapse
Instance Attribute Summary collapse
-
#flag_raise_state_error ⇒ Bool
readonly
ステートエラー時に、ランタイムエラーを起こすかのフラグ.
-
#respond_to ⇒ String
readonly
動作選出されたメソッド名.
-
#state ⇒ String
readonly
ステート.
Class Method Summary collapse
-
.debug_dump ⇒ String
デバグ用:各パラメータの表示用文字列を返す.
-
.inherited(subclass) ⇒ Object
ユーザプログラムで継承された場合にそれを保存する.
-
.suitable_class ⇒ Object
getter suitable_class.
Instance Method Summary collapse
-
#_exec ⇒ Object
アプリケーション実行開始(内部メソッド).
-
#action_create ⇒ Object
(MIF) 新規登録 フォーム表示アクション.
-
#action_create_submit ⇒ Object
(MIF) 新規登録 確定アクション.
-
#action_delete ⇒ Object
(MIF) 削除 確認画面表示アクション.
-
#action_delete_submit ⇒ Object
(MIF) 削除 確定アクション.
-
#action_list ⇒ Object
(MIF) 一覧表示アクション.
-
#action_update ⇒ Object
(MIF) 更新 フォーム表示アクション.
-
#action_update_submit ⇒ Object
(MIF) 更新 確定アクション.
-
#log(*args) ⇒ Object
ログ出力.
-
#make_uri(arg = {}) ⇒ Object
リンク用のURIを生成する.
-
#make_uri_key(persist, arg = {}) ⇒ String
(MIF) リンク用のURIをキーを含めて生成する.
-
#no_method_error ⇒ Object
メソッドエラーの場合のエラーハンドラ.
-
#param_order_by(key) ⇒ String
(MIF) 並べ替え用文字列生成.
-
#raise_state_error(flag = true) ⇒ Object
ステートエラー発生の制御.
-
#session ⇒ AlControllerSession
getter: session.
-
#set_state(state) ⇒ Object
(also: #state=)
現在のステートを宣言する.
Instance Attribute Details
#flag_raise_state_error ⇒ Bool (readonly)
Returns ステートエラー時に、ランタイムエラーを起こすかのフラグ.
102 103 104 |
# File 'lib/al_controller.rb', line 102 def flag_raise_state_error @flag_raise_state_error end |
#respond_to ⇒ String (readonly)
Returns 動作選出されたメソッド名.
99 100 101 |
# File 'lib/al_controller.rb', line 99 def respond_to @respond_to end |
#state ⇒ String (readonly)
Returns ステート.
96 97 98 |
# File 'lib/al_controller.rb', line 96 def state @state end |
Class Method Details
.debug_dump ⇒ String
デバグ用:各パラメータの表示用文字列を返す
221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/al_controller.rb', line 221 def self.debug_dump() r = "CTRL: #{CTRL}, STATE: #{$AlController.state}, ACTION: #{Alone::action}, RESPOND TO: #{$AlController.respond_to}\n" r << "SESSION VAR:\n" prefix = "AL_#{CTRL}_" AlSession::keys().each do |k| if k.to_s.index( prefix ) == 0 r << " #{k.to_s[prefix.size,100]}: #{AlSession[k]}\n" end end return r end |
.inherited(subclass) ⇒ Object
ユーザプログラムで継承された場合にそれを保存する
42 43 44 |
# File 'lib/al_controller.rb', line 42 def self.inherited( subclass ) @@suitable_class = subclass end |
.suitable_class ⇒ Object
getter suitable_class
34 35 36 |
# File 'lib/al_controller.rb', line 34 def self.suitable_class return @@suitable_class end |
Instance Method Details
#_exec ⇒ Object
アプリケーション実行開始(内部メソッド)
各パラメータによりユーザコードを選択し、実行する。
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/al_controller.rb', line 149 def _exec() # アクション名(メインモジュールの値のエイリアス) action = Alone::action if action.empty? action << "index" # 同じオブジェクトを使うために << を使う。 end @respond_to = "from_#{@state}_action_#{action}" if respond_to?( @respond_to ) return __send__( @respond_to ) end @respond_to = "state_#{@state}_action_#{action}" if respond_to?( @respond_to ) return __send__( @respond_to ) end @respond_to = "action_#{action}" if respond_to?( @respond_to ) return __send__( @respond_to ) end @respond_to = "state_#{@state}" if respond_to?( @respond_to ) return __send__( @respond_to ) end # 実行すべきメソッドが見つからない場合 @respond_to = "" no_method_error() end |
#action_create ⇒ Object
(MIF) 新規登録 フォーム表示アクション
111 112 113 114 115 116 |
# File 'lib/al_mif.rb', line 111 def action_create() () @form.action = Alone::make_uri( :action=>'create_submit' ) AlTemplate.run( @template_create || "#{AL_BASEDIR}/templates/form.rhtml" ) end |
#action_create_submit ⇒ Object
(MIF) 新規登録 確定アクション
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/al_mif.rb', line 122 def action_create_submit() () if ! @form.validate() # バリデーションエラーならフォームへ戻す AlTemplate.run( @template_create || "#{AL_BASEDIR}/templates/form.rhtml" ) return end set_persist_values_from_form() @result = @persist.create() AlTemplate.run( @template_create_submit || "#{AL_BASEDIR}/templates/form_submit.rhtml" ) end |
#action_delete ⇒ Object
(MIF) 削除 確認画面表示アクション
176 177 178 179 180 181 182 |
# File 'lib/al_mif.rb', line 176 def action_delete() raise "Primary key is not given." if ! @form.validate( @persist.pkeys ) raise "Read error. #{@form.values}" if ! @persist.read( @form.values ) set_form_values_from_persist() AlTemplate.run( @template_delete || "#{AL_BASEDIR}/templates/delete.rhtml" ) end |
#action_delete_submit ⇒ Object
(MIF) 削除 確定アクション
188 189 190 191 192 193 194 |
# File 'lib/al_mif.rb', line 188 def action_delete_submit() raise "Primary key is not given." if ! @form.validate( @persist.pkeys ) set_persist_values_from_form() @result = @persist.delete() AlTemplate.run( @template_delete_submit || "#{AL_BASEDIR}/templates/delete_submit.rhtml" ) end |
#action_list ⇒ Object
(MIF) 一覧表示アクション
ブラウザから渡されるパラメータとして、以下の3つを使用する。
total_rows
offset
order_by
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 |
# File 'lib/al_mif.rb', line 67 def action_list() # 検索条件の取得および調整 form_search_condition = AlForm.new( AlInteger.new( "total_rows", :min=>0 ), AlInteger.new( "offset", :min=>0 ), AlText.new( "order_by", :validator=>/\A[\w ,]+\z/ ) ) @search_condition ||= {} if form_search_condition.validate() if @search_condition[:total_rows] && form_search_condition[:total_rows] @search_condition[:total_rows] = form_search_condition[:total_rows].to_i end @search_condition[:offset] ||= form_search_condition[:offset].to_i if ! form_search_condition[:order_by].empty? @search_condition[:order_by] ||= form_search_condition[:order_by] end end @search_condition[:limit] ||= 20 @search_condition[:order_by] ||= @persist.pkeys() # データの取得 @datas = @persist.search( @search_condition ) # 表示用カラム配列作成 @columns = [] @form..each do |k,w| next if w.class == AlHidden || w.class == AlSubmit || w.class == AlPassword || w.hidden @columns << k end # 次のリクエストURI生成用インスタンス変数@requestを作る @request = AlForm.request_get if @persist.search_condition[:total_rows] @request[:total_rows] = @persist.search_condition[:total_rows] end # 表示開始 AlTemplate.run( @template_list || "#{AL_BASEDIR}/templates/list.rhtml" ) end |
#action_update ⇒ Object
(MIF) 更新 フォーム表示アクション
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/al_mif.rb', line 140 def action_update() raise "Primary key is not given." if ! @form.validate( @persist.pkeys ) raise "Read error. #{@form.values}" if ! @persist.read( @form.values ) set_form_values_from_persist() # フォームの調整 # プライマリキーとなるウィジェットを変更不可にする @persist.pkeys.each do |k| @form.[k].set_attr( :readonly=>'readonly' ) end @form.action = Alone::make_uri( :action=>'update_submit' ) # 表示開始 AlTemplate.run( @template_update || "#{AL_BASEDIR}/templates/form.rhtml" ) end |
#action_update_submit ⇒ Object
(MIF) 更新 確定アクション
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/al_mif.rb', line 160 def action_update_submit() if ! @form.validate() # バリデーションエラーならフォームへ戻す AlTemplate.run( @template_update || "#{AL_BASEDIR}/templates/form.rhtml" ) return end set_persist_values_from_form() @result = @persist.update() AlTemplate.run( @template_update_submit || "#{AL_BASEDIR}/templates/form_submit.rhtml" ) end |
#log(*args) ⇒ Object
ログ出力
120 121 122 |
# File 'lib/al_controller.rb', line 120 def log( *args ) Alone.log( *args ) end |
#make_uri(arg = {}) ⇒ Object
リンク用のURIを生成する
128 129 130 |
# File 'lib/al_controller.rb', line 128 def make_uri( arg = {} ) Alone.make_uri( arg ) end |
#make_uri_key(persist, arg = {}) ⇒ String
(MIF) リンク用のURIをキーを含めて生成する
36 37 38 39 40 41 42 |
# File 'lib/al_mif.rb', line 36 def make_uri_key( persist, arg = {} ) uri = Alone::make_uri( arg ) persist.pkeys.each do |k| uri << "&#{k}=#{Alone::encode_uri_component(persist[k])}" end return uri end |
#no_method_error ⇒ Object
メソッドエラーの場合のエラーハンドラ
ステートエラーは、raise_state_error()で動作を本番時とデバッグ時を切り替えられる。 エラー表示などしたければ、当メソッドをオーバライドすることもできる。
189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/al_controller.rb', line 189 def no_method_error() if @state.to_s.empty? Alone::add_http_header( "Status: 404 Not Found" ) raise "No action defined. CTRL: #{CTRL}, ACTION: #{Alone::action}" end if @flag_raise_state_error Alone::add_http_header( "Status: 404 Not Found" ) raise "No state/action defined. CTRL: #{CTRL}, STATE: #{@state}, ACTION: #{Alone::action}" end Alone::add_http_header( "Status: 204 No Content" ) end |
#param_order_by(key) ⇒ String
(MIF) 並べ替え用文字列生成
並べ替えを順序 (order by xxx asc|desc) をトグル動作とする為に、引数で渡したカラム名を加工する。
53 54 55 |
# File 'lib/al_mif.rb', line 53 def param_order_by( key ) return @request[:order_by] == key ? "#{key} desc" : key end |
#raise_state_error(flag = true) ⇒ Object
ステートエラー発生の制御
138 139 140 |
# File 'lib/al_controller.rb', line 138 def raise_state_error( flag = true ) @flag_raise_state_error = flag end |
#session ⇒ AlControllerSession
getter: session
110 111 112 |
# File 'lib/al_controller.rb', line 110 def session() return AlControllerSession end |