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タグとして生成するかのフラグ



650
651
652
653
654
655
656
657
658
659
660
661
662
# File 'lib/al_form.rb', line 650

def initialize( name, arg = {} )
  @name = name.to_s
  @label = arg[:label] || @name
  @value = arg[:value]     # (note) 初期値にはfilterをかける必要はないだろう
  @value_format = arg[:value_format]
  @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)

    入力フィルター



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

def filter
  @filter
end

#foreignBoolean

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

Returns:

  • (Boolean)

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



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

def foreign
  @foreign
end

#hiddenBoolean

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

Returns:

  • (Boolean)

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



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

def hidden
  @hidden
end

#labelString

Returns ラベル.

Returns:

  • (String)

    ラベル



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

def label
  @label
end

#messageString (readonly)

Returns メッセージ.

Returns:

  • (String)

    メッセージ



633
634
635
# File 'lib/al_form.rb', line 633

def message
  @message
end

#nameSymbol (readonly)

Returns 識別名.

Returns:

  • (Symbol)

    識別名



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

def name
  @name
end

#requiredBoolean

Returns 必須入力フラグ.

Returns:

  • (Boolean)

    必須入力フラグ



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

def required
  @required
end

#tag_attrHash

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

Returns:

  • (Hash)

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



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

def tag_attr
  @tag_attr
end

#tag_typeString

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

Returns:

  • (String)

    htmlタグ生成時のtype文字列



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

def tag_type
  @tag_type
end

#valueObject

Returns 入力値または初期値.

Returns:

  • (Object)

    入力値または初期値



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

def value
  @value
end

#value_formatString, Nil

Returns make_valueで使用する変換フォーマット文字列.

Returns:

  • (String, Nil)

    make_valueで使用する変換フォーマット文字列



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

def value_format
  @value_format
end

Instance Method Details

#make_tag(appendix_tag = {}) ⇒ String

(AlWidget) HTMLタグの生成

Parameters:

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

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

Returns:

  • (String)

    htmlタグ



695
696
697
698
699
700
701
702
703
704
# File 'lib/al_form.rb', line 695

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文字列



713
714
715
# File 'lib/al_form.rb', line 713

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)

    セットする値



684
685
686
# File 'lib/al_form.rb', line 684

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

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

(AlWidget) 値のセット

Parameters:

  • v

    セットする値



670
671
672
# File 'lib/al_form.rb', line 670

def set_value( v )
  @value = v
end