Module: When::Parts::Resource

Extended by:
Pool
Includes:
Synchronize
Included in:
BasicTypes::M17n, BasicTypes::Object, TM::Position, TM::TemporalPosition
Defined in:
lib/when_exe/parts/resource.rb

Overview

Resource which has ‘International Resource Identifier’

Defined Under Namespace

Modules: Pool, Synchronize Classes: Element, Enumerator

Constant Summary collapse

Prefix =

登録済み Prefix

{'_w'   => When::SourceURI + '/',
          '_p'   => When::SourceURI + 'Parts/',
          '_b'   => When::SourceURI + 'BasicTypes/',
          '_m'   => When::SourceURI + 'BasicTypes/M17n/',
          '_co'  => When::SourceURI + 'Coordinates/',
          '_l'   => When::SourceURI + 'Coordinates/Spatial?',
          '_v'   => When::SourceURI + 'V/',
          '_rs'  => When::SourceURI + 'RS/',
          '_ex'  => When::SourceURI + 'EX/',
          '_tm'  => When::SourceURI + 'TM/',
          '_e'   => When::SourceURI + 'TM/CalendarEra/',
          '_t'   => When::SourceURI + 'TimeStandard/',
          '_ep'  => When::SourceURI + 'Ephemeris/',
          '_c'   => When::SourceURI + 'CalendarTypes/',
          '_n'   => When::SourceURI + 'CalendarTypes/CalendarNote/',
          '_sc'  => When::SourceURI + 'Ephemeris/V50/'
}
PrefixKeys =
Prefix.keys.sort.reverse.map {|k| k.downcase}
PrefixValues =
Prefix.values.sort.reverse
PrefixIndex =
Prefix.invert
LabelProperty =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pool

[]=, _setup_

Methods included from Synchronize

#synchronize

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

その他のメソッド

When::Parts::GeometricComplex で定義されていないメソッドは
処理を @child (type: Array) に委譲する


784
785
786
# File 'lib/when_exe/parts/resource.rb', line 784

def method_missing(name, *args, &block)
  @child.send(name.to_sym, *args, &block)
end

Instance Attribute Details

#_poolObject (readonly)



398
399
400
# File 'lib/when_exe/parts/resource.rb', line 398

def _pool
  @_pool
end

#childArray<When::Parts::Resource> (readonly)

self が has-a 関係で包含するオブジェクト



404
405
406
# File 'lib/when_exe/parts/resource.rb', line 404

def child
  @child
end

#keysArray<String> (readonly)

strftime で有効な locale

Returns:



431
432
433
# File 'lib/when_exe/parts/resource.rb', line 431

def keys
  @keys
end

#localeArray<String> (readonly)

Resource包含階層で使用する locale

When::BasicTypes::M17n の生成に使用する locale を定義する。
RFC 5545 に対する拡張である。

Returns:



425
426
427
# File 'lib/when_exe/parts/resource.rb', line 425

def locale
  @locale
end

#namespaceHash (readonly)

Resource包含階層で使用する namespace

When::BasicTypes::M17n の生成に使用する namespace を定義する。
RFC 5545 に対する拡張である。
xml で記述する場合には、本ライブラリ以外でも namespace を定義している。

Returns:

  • (Hash)

    { prefix => prefix文字列 }



415
416
417
# File 'lib/when_exe/parts/resource.rb', line 415

def namespace
  @namespace
end

Class Method Details

._extract_prefix(path, capitalize = false) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/when_exe/parts/resource.rb', line 234

def _extract_prefix(path, capitalize=false)
  if (path =~ /^(.+?):+(.+)$/)
    prefix, klass = $~[1..2]
    if capitalize
      prefix = '_' + prefix.downcase
      klass  = klass.capitalize if klass == klass.upcase
    end
    path = Prefix[prefix] + klass if (Prefix[prefix])
  elsif capitalize && path =~ /^(v[^\/]+|daylight$|standard$)/i
    klass = path.sub(/^v/i, '').capitalize
    path  = Prefix['_v'] + klass if When::V.const_defined?(klass) &&
                                    When::V.const_get(klass).kind_of?(Class)
  end
  return path
end

._instance(iri, namespace = nil) ⇒ When::Parts::Resource

オブジェクト生成&参照

指定した iri の When::Parts::Resource オブジェクトを取得する。 当該オブジェクトが未登録であれば生成する。

Parameters:

  • iri (String)

    International Resource Identifier

  • namespace (String) (defaults to: nil)

    (デフォルトの名前空間, 指定がないときは名前空間を省略しない)

Returns:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/when_exe/parts/resource.rb', line 165

def _instance(iri, namespace=nil)
  # 配列は個別に処理
  return iri.map {|e| _instance(e, namespace)} if iri.kind_of?(Array)

  # 文字列以外はそのまま返す
  return iri unless iri.instance_of?(String)

  # 階層がある場合は、階層をたどる
  iri = namespace + iri if namespace && iri !~ /^[_a-z\d]+:[^:]/i
  root, *leaves= iri.split(/::/)
  return leaves.inject(_instance(root)) {|obj,leaf| obj[leaf]} unless leaves==[] || iri =~ /\?/

  # 登録ずみなら、参照
  path, query = _extract_prefix(iri).split(/\?/, 2)
  iri      = query ? (path + '?' + query) : path
  if When.multi_thread
    my_mutex = nil
    @_lock_.synchronize do
      @_pool ||= {}
      unless @_pool[iri]
        my_mutex    = Mutex.new
        @_pool[iri] = my_mutex
      end
    end
    case @_pool[iri]
    when my_mutex; my_mutex.synchronize    {_create_object(iri, path, query) }
    when Mutex   ; @_pool[iri].synchronize { @_pool[iri] }
    else         ; @_pool[iri]
    end
  else
    @_pool ||= {}
    @_pool[iri] ? @_pool[iri] : _create_object(iri, path, query)
  end
end

._parse(line, type = nil) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/when_exe/parts/resource.rb', line 213

def _parse(line, type=nil)
  return line unless line.kind_of?(String)
  line.sub!(/\s#.*$/, '')
  return Locale._split($1) if type && line =~ /^#{type}:(.+)$/i
  return Locale._split(line) unless line =~ /^(\*)?([A-Z][-A-Z_]{0,255})(?:;(.*?))?:(.*)$/i

  marked, key, property, value = $~[1..4]
  element = Element.new(key, value, marked)
  if (property)
    element.attribute['.'] = property+':'+value
    property.split(/;/) do |pr|
      prop = Element.new(*pr.split(/=/, 2))
      element.attribute[prop.predicate] = prop
    end
  else
    element.attribute['.'] = value
  end
  return element
end

._path_with_prefix(obj, simple = true) ⇒ Object



201
202
203
204
205
206
207
208
209
210
# File 'lib/when_exe/parts/resource.rb', line 201

def _path_with_prefix(obj, simple=true)
  path = obj.kind_of?(Class) ? obj.to_s.sub(/^When::/, When::SourceURI).gsub(/::/, '/') :
                               obj.iri
  return path unless simple
  PrefixValues.each do |value|
    index = path.index(value)
    return PrefixIndex[value] + ':' + path[value.length..-1] if index
  end
  return path
end

Instance Method Details

#[](iri) ⇒ When::parts::Resource

IRI または child の番号によるオブジェクト参照

Parameters:

  • iri (String)

    オブジェクトの IRI

  • iri (Numeric)

    child の index

Returns:

  • (When::parts::Resource)


456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/when_exe/parts/resource.rb', line 456

def [](iri)
  case iri
  when Numeric
    return child[iri * 1]
  when String
    obj = self
    iri.split(/::/).each do |label|
      return obj.child if label == '*'
      if obj == Resource
        obj = Resource._instance(label)
      else
        case label
        when ''  ; obj = Resource
        when '.' # obj = obj
        else     ; obj = obj._pool[label.gsub(/%3A%3A/, '::')]
        end
      end
      raise ArgumentError, "IRI not found: #{iri}" unless obj
    end
    return obj
  else
    super(iri)
    #raise ArgumentError, "IRI not found: #{iri}"
  end
end

#^(other) ⇒ Enumerator

Enumerator 生成のダミー

Parameters:

  • other (Object)

Returns:



619
620
621
# File 'lib/when_exe/parts/resource.rb', line 619

def ^(other)
  return nil
end

#each(*args, &block) ⇒ Enumerator

順次実行

Parameters:

  • args (Array)

    各サブクラスの enum_for にそのまま渡す

  • block (Block)

    実行するブロック

Returns:



630
631
632
633
634
# File 'lib/when_exe/parts/resource.rb', line 630

def each(*args, &block)
  enum = enum_for(*args)
  return enum unless block
  enum.each(&block)
end

#enum_for(direction = :forward) ⇒ Object Also known as: to_enum

オブジェクトを順に取り出す enumerator

@param [Symbol] direction 取り出す方向
  [ :forward - 昇順 ]
  [ :reverse - 降順 ]


608
609
610
# File 'lib/when_exe/parts/resource.rb', line 608

def enum_for(direction=:forward)
  Enumerator.new(self, direction)
end

#hierarchy(klass = self.class) ⇒ When::Parts::Resource

self を包含するオブジェクト階層

Parameters:

  • klass (Class) (defaults to: self.class)

    階層を遡るクラス

Returns:



496
497
498
499
500
501
502
503
504
# File 'lib/when_exe/parts/resource.rb', line 496

def hierarchy(klass=self.class)
  hierarchy = []
  parent    = self
  while parent.kind_of?(klass)
    hierarchy << parent
    parent = parent.parent
  end
  hierarchy.reverse
end

#include?(other) ⇒ Boolean

self が other を包含するか

Returns:

  • (Boolean)
    true - 包含する
    false - 包含しない


512
513
514
515
516
517
518
519
# File 'lib/when_exe/parts/resource.rb', line 512

def include?(other)
  c = other
  while c.kind_of?(Resource)
    return true if c.equal?(self)
    c = c.parent
  end
  return false
end

#included?(other) ⇒ Boolean

other が self を包含するか

Returns:

  • (Boolean)
    true - 包含される
    false - 包含されない


527
528
529
# File 'lib/when_exe/parts/resource.rb', line 527

def included?(other)
  other.include?(self)
end

#iriObject

オブジェクトの IRI

@return [Sring]


437
438
439
440
441
442
443
444
445
446
447
# File 'lib/when_exe/parts/resource.rb', line 437

def iri
  return @iri if @iri
  root = @_pool['..']
  path = root.instance_of?(String) ? root : label.to_s
  path = path.gsub(/::/, '%3A%3A')
  if root.respond_to?(:iri)
    prefix = root.iri
    path = prefix + '::' + path if prefix
  end
  @iri = path
end

#leaf?Boolean

オブジェクト包含階層の末端か?

Returns:

  • (Boolean)
    true - IRIが付与された他のオブジェクトを包含していない
    false - IRIが付与された他のオブジェクトを包含している


560
561
562
# File 'lib/when_exe/parts/resource.rb', line 560

def leaf?
  !@child || (@child.length==0)
end

#m17n(source, namespace = nil, locale = nil, options = {}) ⇒ When::BasicTypes::M17n or Array<them>

When::BasicTypes::M17n の生成/参照

Parameters:

  • source (When::BasicTypes::M17n)

    処理を行わず、そのままsourceを返す

  • source (String)

    locale と 文字列の対応

  • source (Array)

    要素を個別に解釈して生成したオブジェクトのArrayを返す

  • namespace (Hash) (defaults to: nil)

    prefix の指定

  • locale (Array) (defaults to: nil)

    locale の定義順序の指定

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

Returns:



591
592
593
594
595
596
597
598
599
600
# File 'lib/when_exe/parts/resource.rb', line 591

def m17n(source, namespace=nil, locale=nil, options={})
  case source
  when Array                  ; When::BasicTypes::M17n.new(source, namespace, locale, options)
  when When::BasicTypes::M17n ; source
  when String
    return self[$1] if source =~ /^\s*\[((\.{1,2}|::)+[^\]]+)\]/
    When::BasicTypes::M17n.new(source, namespace, locale, options)
  else ; raise TypeError, "Invalid Type: #{source.class}"
  end
end

#map(&block) ⇒ Array Also known as: collect

map, collect の再定義

has-a 関係の子 Resource に対して map/collect を行う

Parameters:

  • block (Block)

    実行するブロック

Returns:



644
645
646
# File 'lib/when_exe/parts/resource.rb', line 644

def map(&block)
  @child.map(&block)
end

#nextWhen::Parts::Resource Also known as: succ

次のオブジェクト



547
548
549
550
551
# File 'lib/when_exe/parts/resource.rb', line 547

def next
  c = self
  c = c.child[0] while c.child && c.child.size > 0
  c._pool['.->']
end

#parentWhen::Parts::Resource

self を直接に包含するオブジェクト



486
487
488
# File 'lib/when_exe/parts/resource.rb', line 486

def parent
  @_pool['..'].kind_of?(Resource) ? @_pool['..'] : nil
end

#prevWhen::Parts::Resource

前のオブジェクト



535
536
537
538
539
540
541
# File 'lib/when_exe/parts/resource.rb', line 535

def prev
  c = self
  c = c.child[0] while c.child && c.child.size > 0
  c = c._pool['.<-']
  c = c.child[-1] while c && c.child && c.child.size > 0
  c
end

#registered?Boolean

IRIが付与されているか?

Returns:

  • (Boolean)
    true - IRIが付与されている
    false - IRIが付与されていない


570
571
572
573
574
575
576
577
578
# File 'lib/when_exe/parts/resource.rb', line 570

def registered?
  leaf = self
  while leaf._pool['..'].respond_to?(:_pool)
    root = leaf._pool['..']
    return false unless leaf.equal?(root._pool[leaf.label])
    leaf = root
  end
  Resource._pool.value?(leaf)
end