Class: AlTimestamp

Inherits:
AlWidget show all
Defined in:
lib/al_form.rb

Overview

Note:

年月日と時分秒を扱う

タイムスタンプウィジェット

内部的にはTimeオブジェクトで保存する。

Direct Known Subclasses

AlDate, AlTime

Constant Summary collapse

STRFTIME_FORMAT =
"%Y-%m-%d %H:%M:%S"

Instance Attribute Summary collapse

Attributes inherited from AlWidget

#filter, #foreign, #hidden, #label, #message, #name, #required, #tag_attr, #tag_type, #value, #value_format

Instance Method Summary collapse

Methods inherited from AlWidget

#set_attr, #set_value

Constructor Details

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

(AlTimestamp) constructor

Parameters:

  • name (String)

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

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

    引数ハッシュ

Options Hash (arg):

  • :max (Integer)

    最大値

  • :min (Integer)

    最小値

See Also:



1518
1519
1520
1521
1522
1523
1524
1525
# File 'lib/al_form.rb', line 1518

def initialize( name, arg = {} )
  super( name, arg )

  @value_format ||= self.class::STRFTIME_FORMAT
  @filter = arg[:filter] || AlForm::FILTER_STRIP
  @max = normalize( arg[:max] )
  @min = normalize( arg[:min] )
end

Instance Attribute Details

#maxInteger

Returns 最大値.

Returns:

  • (Integer)

    最大値



1503
1504
1505
# File 'lib/al_form.rb', line 1503

def max
  @max
end

#minInteger

Returns 最小値.

Returns:

  • (Integer)

    最小値



1506
1507
1508
# File 'lib/al_form.rb', line 1506

def min
  @min
end

Instance Method Details

#make_tag(appendix_tag = {}) ⇒ String

(AlTimestamp) HTMLタグの生成

Parameters:

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

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

Returns:

  • (String)

    htmlタグ



1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
# File 'lib/al_form.rb', line 1571

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

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

#make_value(*arg) ⇒ String

(AlTimestamp) HTML値の生成

Parameters:

  • arg (String, Time)

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

Returns:

  • (String)

    html文字列



1589
1590
1591
1592
1593
1594
1595
1596
# File 'lib/al_form.rb', line 1589

def make_value( *arg )
  v = arg.empty? ? @value : arg[0]
  if v.class == Time
    return v.strftime( @value_format )
  else
    return Alone::escape_html( v )
  end
end

#validateBoolean

(AlTimestamp) バリデート

Returns:

  • (Boolean)

    成否



1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
# File 'lib/al_form.rb', line 1533

def validate()
  @message = ""

  if @value == "" || @value == nil
    if @required
      @message = p_("al", "%s is required.") % @label
      return false
    end
    @value = nil
    return true
  end

  begin
    @value = normalize( @value.to_s )
  rescue
    @message = p_("al", "Please enter %s correctly.") % @label
    return false
  end

  if @max && @value > @max
    @message = p_("al", "%1$s must be no more than %2$s.") % [@label, @max.strftime(@value_format)]
    return false
  end
  if @min && @value < @min
    @message = p_("al", "%1$s must be at least %2$s.") % [@label, @min.strftime(@value_format)]
    return false
  end

  return true
end