Class: AlWidget

Inherits:
Object
  • Object
show all
Defined in:
lib/al_form.rb

Overview

ウィジェット スーパークラス

Direct Known Subclasses

AlButton, AlFile, AlNumber, AlSelector, AlText, AlTimestamp

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, arg = {}) ⇒ AlWidget

(AlWidget) constructor

Parameters:

  • name (String)

    ウィジェット識別名 英文字を推奨

  • arg (Hash) (defaults to: {})

    引数ハッシュ

Options Hash (arg):

  • :label (String)

    ラベル文字列

  • :value (Object)

    初期値

  • :required (Boolean)

    必須入力フラグ

  • :filter (Proc)

    入力値フィルター

  • :tag_type (String)

    htmlタグ生成時のtype文字列

  • :tag_attr (Hash)

    htmlタグ要素の追加アトリビュート

  • :foreign (Boolean)

    値が外部で生成されるかのフラグ

  • :hidden (Boolean)

    hiddenタグとして生成するかのフラグ



646
647
648
649
650
651
652
653
654
655
656
657
# File 'lib/al_form.rb', line 646

def initialize( name, arg = {} )
  @name = name.to_s
  @label = arg[:label] || @name
  @value = arg[:value]     # (note) 初期値にはfilterをかける必要はないだろう
  @required = arg[:required]
  @filter = arg[:filter]
  @tag_type = arg[:tag_type] if arg[:tag_type]
  @tag_attr = arg[:tag_attr] || {}
  @foreign = arg[:foreign]
  @hidden = arg[:hidden]
  @message = ""
end

Instance Attribute Details

#filterString

Returns 入力フィルター.

Returns:

  • (String)

    入力フィルター



614
615
616
# File 'lib/al_form.rb', line 614

def filter
  @filter
end

#foreignBoolean

Returns 値が外部で生成されて、フォーム入力ではない事を示すフラグ.

Returns:

  • (Boolean)

    値が外部で生成されて、フォーム入力ではない事を示すフラグ



623
624
625
# File 'lib/al_form.rb', line 623

def foreign
  @foreign
end

#hiddenBoolean

Returns HTMLタグ input type=“hidden” として生成するかのフラグ.

Returns:

  • (Boolean)

    HTMLタグ input type=“hidden” として生成するかのフラグ



626
627
628
# File 'lib/al_form.rb', line 626

def hidden
  @hidden
end

#labelString

Returns ラベル.

Returns:

  • (String)

    ラベル



605
606
607
# File 'lib/al_form.rb', line 605

def label
  @label
end

#messageString (readonly)

Returns メッセージ.

Returns:

  • (String)

    メッセージ



629
630
631
# File 'lib/al_form.rb', line 629

def message
  @message
end

#nameSymbol (readonly)

Returns 識別名.

Returns:

  • (Symbol)

    識別名



602
603
604
# File 'lib/al_form.rb', line 602

def name
  @name
end

#requiredBoolean

Returns 必須入力フラグ.

Returns:

  • (Boolean)

    必須入力フラグ



611
612
613
# File 'lib/al_form.rb', line 611

def required
  @required
end

#tag_attrHash

Returns htmlタグ生成時の追加アトリビュート.

Returns:

  • (Hash)

    htmlタグ生成時の追加アトリビュート



620
621
622
# File 'lib/al_form.rb', line 620

def tag_attr
  @tag_attr
end

#tag_typeString

Returns htmlタグ生成時のtype文字列.

Returns:

  • (String)

    htmlタグ生成時のtype文字列



617
618
619
# File 'lib/al_form.rb', line 617

def tag_type
  @tag_type
end

#valueObject

Returns 入力値または初期値.

Returns:

  • (Object)

    入力値または初期値



608
609
610
# File 'lib/al_form.rb', line 608

def value
  @value
end

Instance Method Details

#make_tag(appendix_tag = {}) ⇒ String

(AlWidget) HTMLタグの生成

Parameters:

  • appendix_tag (Hash) (defaults to: {})

    htmlタグへ追加するアトリビュートを指定

Returns:

  • (String)

    htmlタグ



690
691
692
693
694
695
696
697
698
699
# File 'lib/al_form.rb', line 690

def make_tag( appendix_tag = {} )
  if @hidden
    return %Q(<input type="hidden" name="#{@name}" id="#{@name}" value="#{Alone::escape_html( @value )}">\n)
  end

  r = %Q(<input type="#{@tag_type || "text"}" name="#{@name}" id="#{@name}" value="#{Alone::escape_html( @value )}")
  make_tag_attr(r, appendix_tag)
  r << ">"
  return r
end

#make_value(*arg) ⇒ String

(AlWidget) HTML値の生成

Parameters:

  • arg (String)

    表示値。指定なければ内部値を使う。

Returns:

  • (String)

    html文字列



708
709
710
# File 'lib/al_form.rb', line 708

def make_value( *arg )
  return Alone::escape_html( arg.empty? ? @value : arg[0] )
end

#set_attr(arg) ⇒ Object

Note:

名称はsetだが、実質はaddである。 一貫していないようだが、テンプレートとの兼ね合いもあり、 この名称の方が自然に記述できる。

(AlWidget) アトリビュートの設定

Parameters:

  • arg (Hash)

    セットする値



679
680
681
# File 'lib/al_form.rb', line 679

def set_attr( arg )
  @tag_attr.merge!( arg )
end

#set_value(v) ⇒ Object Also known as: value=

(AlWidget) 値のセット

Parameters:

  • v

    セットする値



665
666
667
# File 'lib/al_form.rb', line 665

def set_value( v )
  @value = v
end