Class: AlController

Inherits:
Object
  • Object
show all
Defined in:
lib/al_controller.rb,
lib/al_mif.rb

Overview

Aloneコントローラクラス

コントローラ名、ステート名、アクション名、この3つのパラメータにより、全体を駆動する。コントローラは、パラメータ ctrl= で、アクションは、action= で指定される。ただし、これらパラメータのパースはメインモジュールにて行い、ここでは、その値のエイリアスをもらっている。併せて、(ちょっとした工夫により)コントローラごとに名前空間分離した専用セッション変数を持たせる機能もインプリメントしてある。

Defined Under Namespace

Classes: AlControllerSession

Constant Summary collapse

CTRL =

コントローラ名(メインモジュールの値のエイリアス)

Alone::ctrl
@@suitable_class =

フレームワーク側で生成するクラスを保存

self

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#flag_raise_state_errorBool (readonly)

Returns ステートエラー時に、ランタイムエラーを起こすかのフラグ.

Returns:

  • (Bool)

    ステートエラー時に、ランタイムエラーを起こすかのフラグ



108
109
110
# File 'lib/al_controller.rb', line 108

def flag_raise_state_error
  @flag_raise_state_error
end

#respond_toString (readonly)

Returns 動作選出されたメソッド名.

Returns:

  • (String)

    動作選出されたメソッド名



105
106
107
# File 'lib/al_controller.rb', line 105

def respond_to
  @respond_to
end

#stateString (readonly)

Returns ステート.

Returns:

  • (String)

    ステート



102
103
104
# File 'lib/al_controller.rb', line 102

def state
  @state
end

Class Method Details

.debug_dumpString

デバグ用:各パラメータの表示用文字列を返す

Returns:

  • (String)

    デバグ用文字列



227
228
229
230
231
232
233
234
235
236
237
# File 'lib/al_controller.rb', line 227

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

ユーザプログラムで継承された場合にそれを保存する



48
49
50
# File 'lib/al_controller.rb', line 48

def self.inherited( subclass )
  @@suitable_class = subclass
end

.suitable_classObject

getter suitable_class



40
41
42
# File 'lib/al_controller.rb', line 40

def self.suitable_class
  return @@suitable_class
end

Instance Method Details

#_execObject

Note:

アプリケーション実行開始(内部メソッド)

各パラメータによりユーザコードを選択し、実行する。



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
180
181
182
183
184
185
# File 'lib/al_controller.rb', line 155

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_createObject

(MIF) 新規登録 フォーム表示アクション



112
113
114
115
116
117
# File 'lib/al_mif.rb', line 112

def action_create()
  delete_foreign_widget()
  @form.action = Alone::make_uri( :action=>'create_submit' )

  AlTemplate.run( @template_create || "#{AL_LIB_DIR}/templates/form.rhtml" )
end

#action_create_submitObject

(MIF) 新規登録 確定アクション



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/al_mif.rb', line 123

def action_create_submit()
  delete_foreign_widget()

  if ! @form.validate()
    # バリデーションエラーならフォームへ戻す
    AlTemplate.run( @template_create || "#{AL_LIB_DIR}/templates/form.rhtml" )
    return
  end

  set_persist_values_from_form()
  @result = @persist.create()
  AlTemplate.run( @template_create_submit || "#{AL_LIB_DIR}/templates/form_submit.rhtml" )
end

#action_deleteObject

(MIF) 削除 確認画面表示アクション



177
178
179
180
181
182
183
# File 'lib/al_mif.rb', line 177

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_LIB_DIR}/templates/delete.rhtml" )
end

#action_delete_submitObject

(MIF) 削除 確定アクション



189
190
191
192
193
194
195
# File 'lib/al_mif.rb', line 189

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_LIB_DIR}/templates/delete_submit.rhtml" )
end

#action_listObject

Note:

(MIF) 一覧表示アクション

ブラウザから渡されるパラメータとして、以下の3つを使用する。

total_rows
offset
order_by


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
# File 'lib/al_mif.rb', line 68

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.widgets.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_LIB_DIR}/templates/list.rhtml" )
end

#action_updateObject

(MIF) 更新 フォーム表示アクション



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/al_mif.rb', line 141

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.widgets[k].set_attr( :readonly=>'readonly' )
  end
  @form.action = Alone::make_uri( :action=>'update_submit' )

  # 表示開始
  AlTemplate.run( @template_update || "#{AL_LIB_DIR}/templates/form.rhtml" )
end

#action_update_submitObject

(MIF) 更新 確定アクション



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/al_mif.rb', line 161

def action_update_submit()
  if ! @form.validate()
    # バリデーションエラーならフォームへ戻す
    AlTemplate.run( @template_update || "#{AL_LIB_DIR}/templates/form.rhtml" )
    return
  end

  set_persist_values_from_form()
  @result = @persist.update()
  AlTemplate.run( @template_update_submit || "#{AL_LIB_DIR}/templates/form_submit.rhtml" )
end

#gettext_message_uriString

翻訳ファイルのパスを返す

Examples:

htmlテンプレート中で以下のように使う
<script src="<%=s gettext_message_uri() %>"></script>

Returns:

  • (String)

    翻訳ファイルのパス



272
273
274
# File 'lib/al_controller.rb', line 272

def gettext_message_uri()
  return "locale/#{@al_locale}/messages.js"
end

#init_locale(locale_string = nil) ⇒ String

ロケールの設定

Parameters:

  • locale_string (String) (defaults to: nil)

    ロケール文字列。e.g. “ja_JP”

Returns:

  • (String)

    設定されたロケール文字列



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/al_controller.rb', line 246

def init_locale( locale_string = nil )
  if locale_string
    @al_locale = locale_string.to_s
    AlSession[:al_locale] = @al_locale

  elsif AlSession[:al_locale]
    @al_locale = AlSession[:al_locale]

  else
    @al_locale = ""
    return @al_locale
  end

  set_locale( @al_locale )
  return @al_locale
end

#log(*args) ⇒ Object

ログ出力

See Also:

  • Alone.log()


126
127
128
# File 'lib/al_controller.rb', line 126

def log( *args )
  Alone.log( *args )
end

#make_uri(arg = {}) ⇒ Object

リンク用のURIを生成する



134
135
136
# File 'lib/al_controller.rb', line 134

def make_uri( arg = {} )
  Alone.make_uri( arg )
end

#make_uri_key(persist, arg = {}) ⇒ String

(MIF) リンク用のURIをキーを含めて生成する

Parameters:

  • persist (AlPersist)

    AlPersistオブジェクト

  • arg (Hash<String>) (defaults to: {})

    URIに含めるパラメータ

Returns:

  • (String)

    生成したURI



37
38
39
40
41
42
43
# File 'lib/al_mif.rb', line 37

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_errorObject

Note:

メソッドエラーの場合のエラーハンドラ

ステートエラーは、raise_state_error()で動作を本番時とデバッグ時を切り替えられる。エラー表示などしたければ、当メソッドをオーバライドすることもできる。



195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/al_controller.rb', line 195

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

Note:

(MIF) 並べ替え用文字列生成

並べ替えを順序 (order by xxx asc|desc) をトグル動作とする為に、引数で渡したカラム名を加工する。

Parameters:

  • key (String)

    並べ替えする項目

Returns:

  • (String)

    加工後



54
55
56
# File 'lib/al_mif.rb', line 54

def param_order_by( key )
  return @request[:order_by] == key ? "#{key} desc" : key
end

#raise_state_error(flag = true) ⇒ Object

ステートエラー発生の制御

Parameters:

  • flag (Bool) (defaults to: true)

    ステートエラー時に、ランタイムエラーを起こすかのフラグ



144
145
146
# File 'lib/al_controller.rb', line 144

def raise_state_error( flag = true )
  @flag_raise_state_error = flag
end

#sessionAlControllerSession

getter: session

Returns:

  • (AlControllerSession)

    コントローラローカルセッションの操作オブジェクト



116
117
118
# File 'lib/al_controller.rb', line 116

def session()
  return AlControllerSession
end

#set_state(state) ⇒ Object Also known as: state=

現在のステートを宣言する

Parameters:

  • state (String)

    ステート文字列



215
216
217
218
# File 'lib/al_controller.rb', line 215

def set_state( state )
  @state = state.to_s
  AlSession["AL_STATE_#{CTRL}"] = @state
end