Class: When::Coordinates::Pair

Inherits:
Numeric
  • Object
show all
Defined in:
lib/when_exe/coordinates.rb,
lib/when_exe/inspect.rb

Overview

暦座標値

暦座標の値を表現する

Direct Known Subclasses

LeapSeconds

Constant Summary collapse

DL0 =
{'-'=> 0,   '.'=> 0, ':'=> 0,   ','=> 0, ' '=> 0, '@'=>-2 }
DL1 =
{'!'=>-2.5, '%'=>-2, '&'=>-1.5, '*'=>-1, '+'=>-0.5,
'<'=> 0.5, '='=> 1, '>'=> 1.5, '?'=> 2 }
DL2 =
DL1.invert

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Numeric

#to_day_of_week, #to_month_name, #to_pair, #unit_duration, #unit_interval_length, #unit_period_duration

Methods included from TM::TemporalPosition::Conversion

#julian_date, #tm_position

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

その他のメソッド

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


1088
1089
1090
1091
1092
1093
1094
1095
# File 'lib/when_exe/coordinates.rb', line 1088

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

Instance Attribute Details

#branchNumeric

暦要素の枝

Returns:

  • (Numeric)

    暦要素のうち(閏であるかなど)幹で表現しきれない枝の部分



900
901
902
# File 'lib/when_exe/coordinates.rb', line 900

def branch
  @branch
end

#sumNumeric (readonly)

Note:

個別の実装において、本変数が When::TM::Calendar や When::TM::Clock が扱うべき 座標値を表すように配慮されている。

例: 会計年度など年の変わり目が暦年と異なる場合、trunk+=1, branch-=1 として、

trunk が会計年度, sum が暦年を表現するようにできる。この場合、trunk は表記上の
年、branch は会計年度と暦年にずれがあるという情報を表現していることになる。

暦要素の幹と枝の和

Returns:



915
916
917
# File 'lib/when_exe/coordinates.rb', line 915

def sum
  @sum
end

#trunkNumeric

暦要素の幹

Returns:

  • (Numeric)

    年番号,月番号,日番号など暦要素の幹となる部分



893
894
895
# File 'lib/when_exe/coordinates.rb', line 893

def trunk
  @trunk
end

Class Method Details

._de_pair(source) ⇒ Numeric+

When::Coordinates::Pair の trunk と branch の和をとって Numeric 化する

Parameters:

  • source (When::Coordinates::Pair)

    trunk と branch の和を返す

  • source (Array)

    各要素を _de_pair した Arrayを返す

  • source (その他 Numeric)

    をそのまま返す

Returns:



807
808
809
810
811
812
813
814
815
816
817
818
# File 'lib/when_exe/coordinates.rb', line 807

def _de_pair(source)
  case source
  when Array
    return (source.map!{|v| _de_pair(v)})
  when self
    return source.sum
  when Numeric
    return source
  else
    raise TypeError, "Irregal type for #{self}"
  end
end

._en_number(source, default = 0) ⇒ Numeric

source を Numeric に変換する

Parameters:

  • source (Object)
  • default (Numeric) (defaults to: 0)

    source が nil の場合の代替値

Returns:



749
750
751
752
753
754
# File 'lib/when_exe/coordinates.rb', line 749

def _en_number(source, default=0)
  return default unless(source)
  integer = source.to_i
  float   = source.to_f
  return (integer==float) ? integer : float
end

._en_pair(trunk, branch = nil) ⇒ trunk自体, When::Coordinates::Pair

branch が有効なら (source, branch) を When::Coordinates::Pair に変換する。

Parameters:

Returns:



790
791
792
793
794
795
796
# File 'lib/when_exe/coordinates.rb', line 790

def _en_pair(trunk, branch=nil)
  return trunk if (trunk.kind_of?(self))
  branch = DL0[branch] || DL1[branch] || branch
  trunk  = _en_number(trunk) if (trunk)
  return trunk unless (branch && branch != 0)
  return new(trunk, branch)
end

._en_pair_array(source) ⇒ Array<When::Coordinates::Pair>

文字列を When::Coordinates::Pair のArray 化する

Parameters:

Returns:



827
828
829
830
831
832
833
# File 'lib/when_exe/coordinates.rb', line 827

def _en_pair_array(source)
  source = $1 if (source=~/^\s*\[?(.+?)\]?\s*$/)
  source.split(/,/).map {|v|
     v =~ /^\s*(.+?)([^\d\s])?\s*$/
     _en_pair($1, $2)
  }
end

._en_pair_date_time(source) ⇒ Array<When::Coordinates::Pair>

日付を When::Coordinates::Pair のArray 化する

Parameters:

Returns:



842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
# File 'lib/when_exe/coordinates.rb', line 842

def _en_pair_date_time(source)
  source = $1 if source =~ /^\s*\[(.+)\]\s*$/
  trunk, branch, *rest = source.strip.split(/([^\d])/)
  if trunk == ''
    sign = branch
    trunk, branch, *rest = rest
    trunk = sign + trunk if trunk
  end
  pairs = []
  while (trunk)
    pairs << _en_pair(trunk, branch)
    trunk, branch, *rest = rest
  end
  return pairs
end

._force_pair(source, branch = nil) ⇒ When::Coordinates::Pair

(source, branch) を When::Coordinates::Pair に変換する

Parameters:

  • source (Object)
  • branch (Numeric) (defaults to: nil)

Returns:



764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# File 'lib/when_exe/coordinates.rb', line 764

def _force_pair(source, branch=nil)
  case source
  when self
    return source
  when Numeric
    return new(_en_number(source), branch)
  when String
    tail   = source[-1..-1]
    branch = DL0[tail] || DL1[tail]
    source = (branch) ? source[0..-2] : source.dup
    trunk  = (source =~ /\.|E/i) ? source.to_f : source.to_i
    return new(trunk, branch)
  else
    raise TypeError, "Irregal type for #{self} :#{source}"
  end
end

._format(format, list) ⇒ String

Note:

%0s は文字列引数の表示を抑制するための指定となる。C言語のprintfと異なるので注意。

branch文字を意識して、書式文字列により When::Coordinates::Pair の Array を文字列化

Parameters:

  • format (String)

    書式文字列

  • list (Array)

    書式に従って文字列化されるオブジェクトの Array

Returns:



869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
# File 'lib/when_exe/coordinates.rb', line 869

def _format(m17ns)
  index = 1
  m17ns[0].scan(/(%(?:(\d)\$)?[-+0# ]?[*\d.$]*[cspdiuboxXfgeEG])([-.:])?|(%%|.)/).inject('') { |form, m17n|
    t,i,s,c = m17n
    case t
    when '%0s'
      m17n[index..index] = nil
    when nil
      form  += c
    else
      suffix = DL2[m17ns[i ? i.to_i : index]*0]  || s if s
      form  += t + ({nil=>'', '%'=>'%%'}[suffix] || suffix)
      index += t.length - t.gsub(/\*/,'').length + 1
    end
    form
  } % m17ns[1..-1].map { |v| v.kind_of?(self) ? v.trunk : v }
end

Instance Method Details

#%(other) ⇒ When::Coordinates::Pair

剰余

Parameters:

Returns:



1030
1031
1032
# File 'lib/when_exe/coordinates.rb', line 1030

def %(other)
  self.class.new(@trunk % other, @branch)
end

#*(other) ⇒ Numeric

Note:

When::Coordinates::Pair 以外の Numeric では、1 による乗算は恒等変換になる。 また、0 による乗算は恒に 0になる。 このため、When::TM::Calendar や When::TM::Clock の実装は、暦要素が When::Coordinates::Pair か 否かを判断することなく、暦要素に 1 による乗算を施すことによって、trunk に相当する値を、 0 による乗算を施すことによって、branch に相当する値を取得できる。

@trunk, @branch を取得する

Parameters:

  • other (Integer)

    (1, 0, -1)

Returns:

  • (Numeric)
    other == 1 - @trunk
    other == 0 - @branch
    other == -1 - -@trunk


978
979
980
981
982
983
984
985
# File 'lib/when_exe/coordinates.rb', line 978

def *(other)
  case other
  when  1 ;  @trunk
  when  0 ;  @branch
  when -1 ; -@trunk
  else    ; raise ArgumentError, "Irregal designation : #{other}"
  end
end

#+(other) ⇒ When::Coordinates::Pair

加算

Parameters:

Returns:

  • (When::Coordinates::Pair)

    other が When::Coordinates::Pair でない場合、trunk に対する加算となる



994
995
996
997
# File 'lib/when_exe/coordinates.rb', line 994

def +(other)
  return self.class.new(@trunk + other, @branch) unless other.kind_of?(self.class)
  return self.class.new(@trunk + other.trunk, @branch + other.branch)
end

#+@Numeric

Note:

When::Coordinates::Pair 以外の Numeric では、単項演算 + は恒等変換になる。 このため、When::TM::Calendar や When::TM::Clock の実装は、暦要素が When::Coordinates::Pair か 否かを判断することなく、暦要素に単項演算 + を施すことによって、必要な暦要素を取得できる。

属性 @sum を取得する

Returns:



951
952
953
# File 'lib/when_exe/coordinates.rb', line 951

def +@
  return  @sum
end

#-(other) ⇒ When::Coordinates::Pair

減算

Parameters:

Returns:

  • (When::Coordinates::Pair)

    other が When::Coordinates::Pair でない場合、trunk に対する減算となる



1006
1007
1008
1009
# File 'lib/when_exe/coordinates.rb', line 1006

def -(other)
  return self.class.new(@trunk - other, @branch) unless other.kind_of?(self.class)
  return self.class.new(@trunk - other.trunk, @branch - other.branch)
end

#-@When::Coordinates::Pair

trunk の符号を反転する

Returns:



959
960
961
# File 'lib/when_exe/coordinates.rb', line 959

def -@
  return self.class.new(-@trunk, @branch)
end

#<=>(other) ⇒ Integer

比較

Parameters:

Returns:

  • (Integer)

    (負,0,正) trunk の比較が優先される



1041
1042
1043
1044
# File 'lib/when_exe/coordinates.rb', line 1041

def <=>(other)
  other = self.class._force_pair(other)
  (@trunk <=> other.trunk).nonzero? || (@branch <=> other.branch)
end

#_to_hash_value(options = {}) ⇒ Object

to_h のための要素生成



137
138
139
# File 'lib/when_exe/inspect.rb', line 137

def _to_hash_value(options={})
  to_s
end

#coerce(other) ⇒ Object

強制型変換



1058
1059
1060
# File 'lib/when_exe/coordinates.rb', line 1058

def coerce(other)
  [self.class._force_pair(other), self]
end

#divmod(other) ⇒ When::Coordinates::Pair

商と剰余

Parameters:

Returns:



1018
1019
1020
1021
# File 'lib/when_exe/coordinates.rb', line 1018

def divmod(other)
  div, mod = @trunk.divmod(other)
  return div, self.class.new(mod, @branch)
end

#to_s(zero = '') ⇒ String

文字列化

Parameters:

  • zero (String) (defaults to: '')

    @branch == 0 を表現する文字列

Returns:



1052
1053
1054
# File 'lib/when_exe/coordinates.rb', line 1052

def to_s(zero='')
  return @trunk.to_s + (@branch==0 ? zero : DL2[@branch])
end