lib/when_exe/parts/resource.rb in when_exe-0.3.5 vs lib/when_exe/parts/resource.rb in when_exe-0.3.6

- old
+ new

@@ -17,10 +17,27 @@ # @private LabelProperty = nil # @private + ConstTypes = { + 'Terms' => ['BasicTypes::M17n', '%s', '_m:%s%s' ], + 'Era' => ['TM::CalendarEra', '%s', '_e:%s%s' ], + 'Residue' => ['Coordinates', '%s', '_co:%s%s' ], + 'Week' => ['CalendarNote', '%sWeek', '_n:%sWeek%s' ], + 'Note' => ['CalendarNote', '%s', '_n:%s%s' ], + 'Notes' => ['CalendarNote', '%s', '_n:%s/Notes%s'], + nil => ['CalendarTypes', '%s', '_c:%s%s' ] + } + + # @private + ConstList = [] + + # private + IRIHeader = /^[_a-z\d]+:[^:]/i + + # @private class ContentLine RFC6350 = { "\\\\" => "\\", "\\n" => "\n", @@ -153,19 +170,40 @@ # def root_dir @root_dir ||= When::RootDir end + # + # 略称を iri に変換する + # + # @param [String or Symbol] abbreviation 略称 + # + # @return [String] iri or nil + # # @private + def _abbreviation_to_iri(abbreviation, abbreviation_types=ConstTypes) + abbreviation_types[:pattern] ||= /^([A-Z].*?)(#{abbreviation_types.keys.compact.join('|')})?(\?.+|::.+)?$/ + abbreviation =~ abbreviation_types[:pattern] + return nil unless $1 + klass, name, iri = abbreviation_types[$2] + if klass.kind_of?(String) + klass = klass.split('::').inject(When) {|k,n| k.const_get(n)} + abbreviation_types[$2][0] = klass + end + klass.const_defined?(name % $1) ? iri % [$1,$3] : nil + end + + # @private attr_reader :_prefix, :_prefix_values, :_prefix_index private :_prefix, :_prefix_values, :_prefix_index # 初期化 # # @param [Hash] options 以下の通り - # @option options [String] :base_uri Base URI for When_exe Resources (Default When::SourceURI) - # @option options [String] :root_dir Root Directory for When_exe Resources Cash data (Default When::RootDir) + # @option options [String] :base_uri Base URI for When_exe Resources (Default When::SourceURI) + # @option options [String] :root_dir Root Directory for When_exe Resources Cash data (Default When::RootDir) + # @option options [Boolean] :leave_const If true, leave Constants of When module defined # # @return [void] # # @note # 本メソッドでマルチスレッド対応の管理変数の初期化を行っている。 @@ -194,10 +232,17 @@ } @base_uri = options[:base_uri] || When::SourceURI @root_dir = options[:root_dir] || When::RootDir @_prefix_values = @_prefix.values.sort.reverse @_prefix_index = @_prefix.invert + unless options[:leave_const] || ConstList.empty? + ConstList.delete_if do |constant| + When.send(:remove_const, constant) if When.const_defined?(constant) + true + end + When._define_common_calendar_types + end end # 設定情報を取得する # # @return [Hash] 設定情報 @@ -217,20 +262,24 @@ # @return [When::Parts::Resource] # def _instance(iri, namespace=nil) _setup_ unless @_pool - # 配列は個別に処理 - return iri.map {|e| _instance(e, namespace)} if iri.kind_of?(Array) + case iri + when Array ; return iri.map {|e| _instance(e, namespace)} # 配列は個別に処理 + when Resource ; return iri # 登録済みはそのまま + when String ; # 解析処理へ + else ; raise ArgumentError, "can't convert #{iri.class} to String" # 例外 + end - # 文字列以外はそのまま返す - return iri unless iri.instance_of?(String) + # 内部文字列化 + iri = When::EncodingConversion.to_internal_encoding(iri) # 階層がある場合は、階層をたどる iri = Resource._decode(iri) iri = $1 while iri =~ /^\((.*)\)$/ - iri = namespace + iri if namespace && iri !~ /^[_a-z\d]+:[^:]/i + iri = namespace + iri if namespace && iri !~ IRIHeader root, *leaves= Resource._encode(iri).split(/::/) if leaves.size > 0 return leaves.inject(_instance(Resource._decode(root))) {|obj,leaf| obj[Resource._decode(leaf)]} end @@ -276,13 +325,13 @@ # @private def _parse(line, type=nil) return line unless line.kind_of?(String) line.sub!(/\s#.*$/, '') - return Locale._split($1) if type && /^#{type}:(.+)$/i =~ line + return When::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] + return When::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 ||= '' @@ -408,11 +457,11 @@ end end # external Resource begin - object = When::Parts::Locale.send(:wikipedia_object, path, {:query=>query}) + object = When::Locale.send(:wikipedia_object, path, {:query=>query}) return object if object OpenURI args = [path, "1".respond_to?(:force_encoding) ? 'r:utf-8' : 'r'] args << {:ssl_verify_mode=>OpenSSL::SSL::VERIFY_NONE} if path =~ /^https:/ open(*args) do |file| @@ -440,14 +489,14 @@ # 内部形式定義の取得 def _class(path) list = [When] path[Resource.base_uri.length..-1].split(/\//).each do |mod| - if list[0].const_defined?(mod) + if list[0].kind_of?(Module) && list[0].const_defined?(mod) list.unshift(list[0].const_get(mod)) else - return nil unless (list[0] == When::V) + return nil unless list[0] == When::V list.unshift(When::V::Root) return list end end return list @@ -864,12 +913,12 @@ def _set_variables(options) @options = options[:options] || {} if options.key?(:options) options.each_pair do |key,value| unless (key =~ /^options$|^\.|^[A-Z]/) case "#{key}" - when 'namespace' ; value = Locale._namespace(value) - when 'locale' ; value = Locale._locale(value) + when 'namespace' ; value = When::Locale._namespace(value) + when 'locale' ; value = When::Locale._locale(value) end instance_variable_set("@#{key}", value) end end end @@ -894,13 +943,13 @@ when 'namespace' options[key] ||= {} if content.attribute['altid'] options[key][content.attribute['prefix'].object] = content.object else - options[key] = options[key].update(Locale._namespace(value)) + options[key] = options[key].update(When::Locale._namespace(value)) end when 'locale' - options[key] = Locale._locale(value) + options[key] = When::Locale._locale(value) else _parse_altid(properties, content) key_list << key unless key_list.include?(key) end end