Class: When::TM::IntervalLength

Inherits:
Duration
  • Object
show all
Defined in:
lib/when_exe/tmobjects.rb

Overview

ISO 11404 の時間間隔に基づいて定義された When::TM::Duration の subclass

see gml schema

Constant Summary collapse

Alias =
{'Y'=>'year', 'M'=>'month' , 'D'=>'day', 'W'=>'week', 'S'=>'system',
'h'=>'hour', 'm'=>'minute', 's'=>'second'}

Constants inherited from Duration

Duration::DAY, Duration::DurationUnits, Duration::HOUR, Duration::MINUTE, Duration::MONTH, Duration::SECOND, Duration::SYSTEM, Duration::Unit, Duration::UnitName, Duration::WEEK, Duration::YEAR

Instance Attribute Summary collapse

Attributes inherited from Duration

#duration, #repeat

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Duration

#<=>, #==, #[], #_enumerator, #abs, #after, #apply_delayed_options, #before, #coerce, dhms, #inspect, #rational_duration, #set_repeat, #sign, #to_as_duration, #to_dhms, #to_duration, #to_f, #to_i, #to_period_duration, unit

Constructor Details

#initialize(value, unit = 'system', factor = 0, radix = 10) ⇒ IntervalLength

オブジェクトの生成

Parameters:

  • value (Numeric)

    時間間隔の長さ / 測定単位(省略不可)

  • unit (String) (defaults to: 'system')

    時間間隔を表現するために使用した測定単位の名称(デフォルト : system) (‘year’|‘month’|‘week’|‘day’|‘hour’|‘minute’|‘second’|‘system’)

  • factor (Integer) (defaults to: 0)

    基底の冪乗を行う指数(デフォルト : 0)

  • radix (Integer) (defaults to: 10)

    時間単位となる乗数の基底となる正の整数(デフォルト : 10)

Raises:

  • (TypeError)


857
858
859
860
861
862
863
864
865
# File 'lib/when_exe/tmobjects.rb', line 857

def initialize(value, unit='system', factor=0, radix=10)
  @value, @factor, @radix = value, factor, radix
  @unit_quantity   = Unit[unit.downcase] || Unit[Alias[unit[0..0]]] if unit.kind_of?(String)
  @unit_quantity ||= unit.to_f
  raise TypeError, "Wrong Unit Type: #{unit}" unless @unit_quantity.kind_of?(Numeric)
  @unit = UnitName[@unit_quantity] ||
          When::Coordinates::Pair._en_number(@unit_quantity).to_s
  @duration = @value * @radix ** (-@factor) * @unit_quantity
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

Note:

When::TM::IntervalLength で定義されていないメソッドは 処理を @duration (type:Numeric) に委譲する

その他のメソッド



890
891
892
893
894
895
896
897
# File 'lib/when_exe/tmobjects.rb', line 890

def method_missing(name, *args, &block)
  self.class.module_eval %Q{
    def #{name}(*args, &block)
      @duration.send("#{name}", *args, &block)
    end
  } unless When::Parts::MethodCash.escape(name)
  @duration.send(name, *args, &block)
end

Instance Attribute Details

#factorInteger (readonly)

基底のべき乗を行う指数

Returns:

  • (Integer)


697
698
699
# File 'lib/when_exe/tmobjects.rb', line 697

def factor
  @factor
end

#radixInteger (readonly)

時間単位となる乗数の基底となる正の整数

Returns:

  • (Integer)


691
692
693
# File 'lib/when_exe/tmobjects.rb', line 691

def radix
  @radix
end

#unitString (readonly)

時間間隔を表現するために使用した測定単位の名称

Returns:

  • (String)

    (year|month|week|day|hour|minute|second|system)



685
686
687
# File 'lib/when_exe/tmobjects.rb', line 685

def unit
  @unit
end

#unit_quantityNumeric (readonly)

測定単位の大きさ

Returns:



711
712
713
# File 'lib/when_exe/tmobjects.rb', line 711

def unit_quantity
  @unit_quantity
end

#valueNumeric

時間間隔の長さ / 測定単位

Returns:



703
704
705
# File 'lib/when_exe/tmobjects.rb', line 703

def value
  @value
end

Class Method Details

._to_array(interval) ⇒ Array<Numeric, String>

Note:

value, factor, radix は Numeric

Interval Length 形式の表現を分解して配列化する

Parameters:

Returns:

  • (Array<Numeric, String>)

    ( value, unit_name, factor, radix )

    value [Numeric
    • 時間間隔 / 単位 ]

    unit_name [String
    • 単位名 ]

    factor [Numeric
    • 冪乗 ]

    radix [Numeric
    • 冪乗の基数 ]



659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
# File 'lib/when_exe/tmobjects.rb', line 659

def self._to_array(interval)
  return nil unless interval =~ /\A([-+]?[\d.]+)(?:(E|X|\((\d+)\))([-+]?\d+))?([A-Za-z]+|\*([\d.]+)S)\z/
  value, radix, radix_quantity, factor, unit, unit_quantity = $~[1..6]
  value  = When::Coordinates::Pair._en_number(value)
  radix  = case radix
           when 'E', nil ; 10
           when 'X'      ; 60
           else          ; radix_quantity.to_i
           end
  factor = factor ? -factor.to_i : 0
  return [value, unit_quantity, factor, radix] if unit_quantity
  unit_quantity = Unit[unit.downcase] || Unit[Alias[unit[0..0]]]
  return [value,  UnitName[unit_quantity], factor, radix] if unit_quantity
  return nil
end

.difference(ended, begun) ⇒ When::TM::IntervalLength

オブジェクトの生成(終点と始点を指定)

Parameters:

Returns:

  • (When::TM::IntervalLength)
    日単位の精度 - 終点と始点の分解能のいずれかが“日”またはそれより粗い場合
    システム精度 - 終点と始点の分解能がともに“日”より細かい場合


876
877
878
879
880
# File 'lib/when_exe/tmobjects.rb', line 876

def self.difference(ended, begun)
  precision = [ended.precision, begun.precision].min
  return new(ended-begun) if precision > When::DAY
  return new(ended.to_i - begun.to_i, unit='day')
end

Instance Method Details

#*(times) ⇒ When::TM::IntervalLength

乗算

Parameters:

Returns:



800
801
802
803
804
805
# File 'lib/when_exe/tmobjects.rb', line 800

def *(times)
  interval = self.dup
  interval.value    = times * @value
  interval.duration = times * @duration
  return interval
end

#+(other) ⇒ When::TM::IntervalLength, other と同じクラス

加算

Parameters:

Returns:

  • (When::TM::IntervalLength)

    (other が Numeric, When::TM::Duration の場合)

  • (other と同じクラス)

    (other がその他の場合)



767
768
769
770
771
772
773
774
775
776
777
# File 'lib/when_exe/tmobjects.rb', line 767

def +(other)
  case other
  when Duration ; diff = other.duration
  when Numeric  ; diff = other * SECOND
  else          ; return other + self
  end
  interval = self.dup
  interval.duration = @duration + diff
  interval.value    = interval.duration / (@radix ** (-@factor) * @unit_quantity)
  return interval
end

#-(other) ⇒ When::TM::IntervalLength

減算

Parameters:

Returns:



787
788
789
790
791
792
# File 'lib/when_exe/tmobjects.rb', line 787

def -(other)
  interval = self.dup
  interval.duration = @duration - (other.kind_of?(Duration) ? other.duration : other * SECOND)
  interval.value    = interval.duration / (@radix ** (-@factor) * @unit_quantity)
  return interval
end

#-@When::TM::IntervalLength

符号反転



750
751
752
753
754
755
# File 'lib/when_exe/tmobjects.rb', line 750

def -@
  interval = self.dup
  interval.value    = -@value
  interval.duration = -@duration
  return interval
end

#/(other) ⇒ When::TM::IntervalLength, Numeric

除算

Parameters:

Returns:



816
817
818
# File 'lib/when_exe/tmobjects.rb', line 816

def /(other)
  other.kind_of?(Duration) ? @duration / other.duration : self * (1.0 / other)
end

#datenil

日付配列

Returns:

  • (nil)

    When::TM::PeriodDuration との互換性のために提供



731
732
733
# File 'lib/when_exe/tmobjects.rb', line 731

def date
  nil
end

#timeNumeric

時刻配列

Returns:

  • (Numeric)

    When::TM::PeriodDuration との互換性のために提供



720
721
722
# File 'lib/when_exe/tmobjects.rb', line 720

def time
  [0, 0, @duration / SECOND]
end

#to_interval_lengthWhen::TM::IntervalLength

When::TM::IntervalLength への変換

単なるオブジェクトのコピー


845
846
847
# File 'lib/when_exe/tmobjects.rb', line 845

def to_interval_length
  self.dup
end

#to_sString

文字列化

Returns:



824
825
826
827
828
829
830
831
832
833
834
835
836
# File 'lib/when_exe/tmobjects.rb', line 824

def to_s
  expression = @value.to_s
  unless @factor == 0
    case @radix
    when 10 ; expression += 'E'
    when 60 ; expression += 'X'
    else    ; expression += '(%d)' % @radix
    end
    expression += '%+d' % (-@factor)
  end
  expression += Alias.invert[@unit] || "*#{When::Coordinates::Pair._en_number(@unit)}S"
  return expression
end

#weeksnil

暦週配列

Returns:

  • (nil)

    When::TM::PeriodDuration との互換性のために提供



742
743
744
# File 'lib/when_exe/tmobjects.rb', line 742

def weeks
  nil
end