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: ContentLine, Enumerator
Constant Summary collapse
- LabelProperty =
nil
Class Attribute Summary collapse
- ._prefix ⇒ Object readonly
- ._prefix_index ⇒ Object readonly
- ._prefix_values ⇒ Object readonly
Instance Attribute Summary collapse
- #_pool ⇒ Object readonly
-
#child ⇒ Array<When::Parts::Resource>
readonly
self が has-a 関係で包含するオブジェクト.
-
#keys ⇒ Array<String>
readonly
strftime で有効な locale.
-
#locale ⇒ Array<String>
readonly
Resource包含階層で使用する locale.
-
#namespace ⇒ Hash
readonly
Resource包含階層で使用する namespace.
Class Method Summary collapse
- ._decode(iri) ⇒ Object
- ._encode(iri) ⇒ Object
- ._extract_prefix(path, capitalize = false) ⇒ Object
-
._instance(iri, namespace = nil) ⇒ When::Parts::Resource
オブジェクト生成&参照.
- ._instantiate(resource) ⇒ Object
- ._parse(line, type = nil) ⇒ Object
- ._path_with_prefix(obj, simple = true) ⇒ Object
- ._replace_tags(source, tags) ⇒ Object
-
._setup_(options = {}) ⇒ void
初期化.
-
._setup_info ⇒ Hash
設定情報を取得する.
- ._simplify_path(path) ⇒ Object
-
.base_uri ⇒ String
Base URI for When_exe Resources.
-
.root_dir ⇒ String
Root Directory for When_exe Resources.
Instance Method Summary collapse
-
#[](iri) ⇒ When::parts::Resource
IRI または child の番号によるオブジェクト参照.
-
#^(other) ⇒ Enumerator
Enumerator 生成のダミー.
-
#each(*args, &block) ⇒ Enumerator
順次実行.
-
#enum_for(direction = :forward) ⇒ Object
(also: #to_enum)
オブジェクトを順に取り出す enumerator.
-
#hierarchy(klass = self.class) ⇒ When::Parts::Resource
self を包含するオブジェクト階層.
-
#include?(other) ⇒ Boolean
self が other を包含するか.
-
#included?(other) ⇒ Boolean
other が self を包含するか.
-
#iri(prefix = false) ⇒ Sring
オブジェクトの IRI.
-
#leaf? ⇒ Boolean
オブジェクト包含階層の末端か?.
-
#m17n(source, namespace = nil, locale = nil, options = {}) ⇒ When::BasicTypes::M17n or Array<them>
When::BasicTypes::M17n の生成/参照.
-
#map(&block) ⇒ Array
(also: #collect)
map, collect の再定義.
-
#next ⇒ When::Parts::Resource
(also: #succ)
次のオブジェクト.
-
#parent ⇒ When::Parts::Resource
self を直接に包含するオブジェクト.
-
#prev ⇒ When::Parts::Resource
前のオブジェクト.
-
#registered? ⇒ Boolean
IRIが付与されているか?.
Methods included from Pool
Methods included from Synchronize
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
その他のメソッド
When::Parts::Resource で定義されていないメソッドは
処理を @child (type: Array) に委譲する
994 995 996 997 998 999 1000 1001 1002 |
# File 'lib/when_exe/parts/resource.rb', line 994 def method_missing(name, *args, &block) return __method_missing(name, *args, &block) if When::Parts::MethodCash::Escape.key?(name) self.class.module_eval %Q{ def #{name}(*args, &block) @child.send("#{name}", *args, &block) end } unless When::Parts::MethodCash.escape(name) @child.send(name, *args, &block) end |
Class Attribute Details
._prefix ⇒ Object (readonly)
159 160 161 |
# File 'lib/when_exe/parts/resource.rb', line 159 def _prefix @_prefix end |
._prefix_index ⇒ Object (readonly)
159 160 161 |
# File 'lib/when_exe/parts/resource.rb', line 159 def _prefix_index @_prefix_index end |
._prefix_values ⇒ Object (readonly)
159 160 161 |
# File 'lib/when_exe/parts/resource.rb', line 159 def _prefix_values @_prefix_values end |
Instance Attribute Details
#_pool ⇒ Object (readonly)
578 579 580 |
# File 'lib/when_exe/parts/resource.rb', line 578 def _pool @_pool end |
#child ⇒ Array<When::Parts::Resource>
self が has-a 関係で包含するオブジェクト
584 585 586 |
# File 'lib/when_exe/parts/resource.rb', line 584 def child @child end |
#keys ⇒ Array<String> (readonly)
strftime で有効な locale
612 613 614 |
# File 'lib/when_exe/parts/resource.rb', line 612 def keys @keys end |
#locale ⇒ Array<String> (readonly)
Resource包含階層で使用する locale
When::BasicTypes::M17n の生成に使用する locale を定義する。
RFC 5545 に対する拡張である。
606 607 608 |
# File 'lib/when_exe/parts/resource.rb', line 606 def locale @locale end |
#namespace ⇒ Hash (readonly)
Resource包含階層で使用する namespace
When::BasicTypes::M17n の生成に使用する namespace を定義する。
RFC 5545 に対する拡張である。
xml で記述する場合には、本ライブラリ以外でも namespace を定義している。
596 597 598 |
# File 'lib/when_exe/parts/resource.rb', line 596 def namespace @namespace end |
Class Method Details
._decode(iri) ⇒ Object
360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/when_exe/parts/resource.rb', line 360 def _decode(iri) return iri unless iri =~ /%28/ iri = iri.dup begin unless iri.gsub!(/%28.*?%29/) {|token| token.gsub(/%([\dA-F]{2})/i) {$1.to_i(16).chr} } raise ArgumentError, 'Brackets do not correspond: ' + iri end end while iri =~ /%28/ iri = $1 if iri =~ /^\((.*)\)$/ iri end |
._encode(iri) ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/when_exe/parts/resource.rb', line 345 def _encode(iri) return iri unless iri =~ /\(/ iri = iri.dup begin unless iri.gsub!(/\([^()]*\)/) {|token| token.gsub(/[():?&%]/) {|char|'%' + char.ord.to_s(16)} } raise ArgumentError, 'Brackets do not correspond: ' + iri end end while iri =~ /\(/ iri end |
._extract_prefix(path, capitalize = false) ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/when_exe/parts/resource.rb', line 304 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 オブジェクトを取得する。 当該オブジェクトが未登録であれば生成する。
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/when_exe/parts/resource.rb', line 219 def _instance(iri, namespace=nil) _setup_ unless @_pool # 配列は個別に処理 return iri.map {|e| _instance(e, namespace)} if iri.kind_of?(Array) # 文字列以外はそのまま返す return iri unless iri.instance_of?(String) # 階層がある場合は、階層をたどる iri = Resource._decode(iri) iri = $1 while iri =~ /^\((.*)\)$/ iri = namespace + iri if namespace && iri !~ /^[_a-z\d]+:[^:]/i root, *leaves= Resource._encode(iri).split(/::/) if leaves.size > 0 return leaves.inject(_instance(Resource._decode(root))) {|obj,leaf| obj[Resource._decode(leaf)]} end # 登録ずみなら、参照 iri = _extract_prefix(iri) path, query = iri.split(/\?/, 2) 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 {@_pool[iri] = _create_object(iri, path, query) } when Mutex ; @_pool[iri].synchronize {@_pool[iri]} else ; @_pool[iri] end else @_pool ||= {} @_pool[iri] ||= _create_object(iri, path, query) end end |
._instantiate(resource) ⇒ Object
376 377 378 379 380 |
# File 'lib/when_exe/parts/resource.rb', line 376 def _instantiate(resource) return resource unless resource.kind_of?(Array) return resource[0].new(*resource[1..-1]) if resource[0].kind_of?(Class) return resource.map {|rsc| _instantiate(rsc)} end |
._parse(line, type = nil) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/when_exe/parts/resource.rb', line 278 def _parse(line, type=nil) return line unless line.kind_of?(String) line.sub!(/\s#.*$/, '') return Locale._split($1) if type && /^#{type}:(.+)$/i =~ line tokens = line.scan(/((?:[^\\:]|\\.)+)(?::(?!\z))?|:/).flatten return Locale._split(line) unless tokens.size > 1 && /^(\*)?([A-Z][-A-Z_]{0,255})(?:;(.+))?$/i =~ tokens[0] marked, key, property = $~[1..3] values = tokens[1..-1] value = values.join(':') unless values == [nil] content = ContentLine.new(key, value, marked) value ||= '' if property content.attribute['.'] = property + ':' + value property.scan(/((?:[^\\;]|\\.)+)(?:;(?!\z))?|;/).flatten.each do |pr| pr ||= '' pr.gsub!(/\\./) {|escape| ContentLine::RFC6350[escape] || escape} prop = ContentLine.new(*pr.split(/=/, 2)) content.attribute[prop.predicate] = prop end else content.attribute['.'] = value end return content end |
._path_with_prefix(obj, simple = true) ⇒ Object
261 262 263 264 265 266 |
# File 'lib/when_exe/parts/resource.rb', line 261 def _path_with_prefix(obj, simple=true) _setup_ unless @_pool path = obj.kind_of?(Class) ? obj.to_s.sub(/^When::/, base_uri).gsub(/::/, '/') : obj.iri simple ? _simplify_path(path) : path end |
._replace_tags(source, tags) ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/when_exe/parts/resource.rb', line 321 def (source, ) case source when When::BasicTypes::M17n source when String target = source.dup .each_pair do |key, value| target.gsub!(/#\{([?&][^=#}]+?=)?#{key}(:.*?)?\}/, '\1' + value) if value.kind_of?(String) end target.gsub(/#\{.+?(:(.*?))?\}/, '\2') when Array source.map {|target| (target, )} when Hash target = {} source.each_pair do |key, value| target[key] = ([key] || value, ) end target else source end end |
._setup_(options = {}) ⇒ void
本メソッドでマルチスレッド対応の管理変数の初期化を行っている。 このため、本メソッド自体はスレッドセーフでない。
This method returns an undefined value.
初期化
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 199 |
# File 'lib/when_exe/parts/resource.rb', line 174 def _setup_(={}) super() @_prefix = { '_wp' => 'http://en.wikipedia.org/wiki/', '_w' => base_uri + '/', '_p' => base_uri + 'Parts/', '_b' => base_uri + 'BasicTypes/', '_m' => base_uri + 'BasicTypes/M17n/', '_co' => base_uri + 'Coordinates/', '_l' => base_uri + 'Coordinates/Spatial?', '_v' => base_uri + 'V/', '_rs' => base_uri + 'RS/', '_ex' => base_uri + 'EX/', '_tm' => base_uri + 'TM/', '_e' => base_uri + 'TM/CalendarEra/', '_t' => base_uri + 'TimeStandard/', '_ep' => base_uri + 'Ephemeris/', '_c' => base_uri + 'CalendarTypes/', '_n' => base_uri + 'CalendarNote/', '_sc' => base_uri + 'Ephemeris/V50/' } @base_uri = [:base_uri] || When::SourceURI @root_dir = [:root_dir] || When::RootDir @_prefix_values = @_prefix.values.sort.reverse @_prefix_index = @_prefix.invert end |
._setup_info ⇒ Hash
設定情報を取得する
205 206 207 |
# File 'lib/when_exe/parts/resource.rb', line 205 def _setup_info {:base_uri => base_uri, :root_dir => root_dir} end |
._simplify_path(path) ⇒ Object
269 270 271 272 273 274 275 |
# File 'lib/when_exe/parts/resource.rb', line 269 def _simplify_path(path) _prefix_values.each do |value| index = path.index(value) return _prefix_index[value] + ':' + path[value.length..-1] if index end return path end |
Instance Method Details
#[](iri) ⇒ When::parts::Resource
IRI または child の番号によるオブジェクト参照
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 |
# File 'lib/when_exe/parts/resource.rb', line 640 def [](iri) case iri when Numeric return child[iri * 1] when String obj = self Resource._encode(iri).split(/::/).each do |label| return obj.child if label == '*' if obj == Resource obj = Resource._instance(Resource._decode(label)) else case label when '' ; obj = Resource when '.' # obj = obj else ; obj = obj._pool[Resource._decode(label)] 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 生成のダミー
803 804 805 |
# File 'lib/when_exe/parts/resource.rb', line 803 def ^(other) return nil end |
#each(*args, &block) ⇒ Enumerator
順次実行
814 815 816 817 818 |
# File 'lib/when_exe/parts/resource.rb', line 814 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 - 降順 ]
792 793 794 |
# File 'lib/when_exe/parts/resource.rb', line 792 def enum_for(direction=:forward) Enumerator.new(self, direction) end |
#hierarchy(klass = self.class) ⇒ When::Parts::Resource
self を包含するオブジェクト階層
680 681 682 683 684 685 686 687 688 |
# File 'lib/when_exe/parts/resource.rb', line 680 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 を包含するか
696 697 698 699 700 701 702 703 |
# File 'lib/when_exe/parts/resource.rb', line 696 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 を包含するか
711 712 713 |
# File 'lib/when_exe/parts/resource.rb', line 711 def included?(other) other.include?(self) end |
#iri(prefix = false) ⇒ Sring
オブジェクトの IRI
620 621 622 623 624 625 626 627 628 629 630 631 |
# File 'lib/when_exe/parts/resource.rb', line 620 def iri(prefix=false) unless @iri root = @_pool['..'] path = root.instance_of?(String) ? root : label.to_s if root.respond_to?(:iri) root_iri = root.iri path = root_iri + '::' + path if root_iri end @iri = path end prefix ? Resource._simplify_path(@iri) : @iri end |
#leaf? ⇒ Boolean
オブジェクト包含階層の末端か?
744 745 746 |
# File 'lib/when_exe/parts/resource.rb', line 744 def leaf? !@child || (@child.length==0) end |
#m17n(source, namespace = nil, locale = nil, options = {}) ⇒ When::BasicTypes::M17n or Array<them>
When::BasicTypes::M17n の生成/参照
775 776 777 778 779 780 781 782 783 784 |
# File 'lib/when_exe/parts/resource.rb', line 775 def m17n(source, namespace=nil, locale=nil, ={}) case source when Array ; When::BasicTypes::M17n.new(source, namespace, locale, ) when When::BasicTypes::M17n ; source when String return self[$1] if source =~ /^\s*\[((\.{1,2}|::)+[^\]]+)\]/ When::BasicTypes::M17n.new(source, namespace, locale, ) else ; raise TypeError, "Invalid Type: #{source.class}" end end |
#map(&block) ⇒ Array Also known as: collect
map, collect の再定義
has-a 関係の子 Resource に対して map/collect を行う
828 829 830 |
# File 'lib/when_exe/parts/resource.rb', line 828 def map(&block) @child.map(&block) end |
#next ⇒ When::Parts::Resource Also known as: succ
次のオブジェクト
731 732 733 734 735 |
# File 'lib/when_exe/parts/resource.rb', line 731 def next c = self c = c.child[0] while c.child && c.child.size > 0 c._pool['.->'] end |
#parent ⇒ When::Parts::Resource
self を直接に包含するオブジェクト
670 671 672 |
# File 'lib/when_exe/parts/resource.rb', line 670 def parent @_pool['..'].kind_of?(Resource) ? @_pool['..'] : nil end |
#prev ⇒ When::Parts::Resource
前のオブジェクト
719 720 721 722 723 724 725 |
# File 'lib/when_exe/parts/resource.rb', line 719 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が付与されているか?
754 755 756 757 758 759 760 761 762 |
# File 'lib/when_exe/parts/resource.rb', line 754 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 |