Sha256: f3ba8dfa1ba920aa8d6ebe9b75bf2c9311e093d9c7801f42d2214135d4ba45d0

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

module RTM::Sugar
  module Topic
    module HashAccess
      def [](cref)
        raise "Characteristic reference must be a String" unless cref.is_a? String
        is_name, type_reference, scope_reference = resolve_characteristic(cref)
        if is_name
          type_reference = RTM::PSI[:name_type] if type_reference == :any
          names.select{|n| n.type == topic_map.get(type_reference)}
        else
          raise "No occurrence type given" if type_reference == :any
          occurrences.select{|o| o.type == topic_map.get(type_reference)}
        end
      end

      def []=(cref, value)
        is_name, type_reference, scope_reference = resolve_characteristic(cref)
        if is_name
          nametype = type_reference || topic_map.get!(RTM::PSI[:name_type])
          n = create_name(value, nametype)
          n
        else
          raise "No occurrence type given" if type_reference == :any
          o = create_occurrence(value, type_reference)
          o
        end
      end
      
      private
      def resolve_characteristic(ref)
        ref =~ /^(-\s*)?(.+)?$/
        is_name = $1 ? true : false
        type, scope = resolve_type_and_scope($2)
        [is_name, type, scope]
      end
      
      def resolve_type_and_scope(ref)
        return [ref, :ucs] if ref.is_a? Topic
        type, scope = ref.split('@', -1)
        if type.blank?
          type = :any
        else
          type.strip!
        end
        if scope
          scope = scope.strip.split(/\s+/)
          scope = :ucs if scope.blank?
        else
          scope = :any
        end
        [type, scope]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rtm-0.1.3 lib/rtm/sugar/topic/hash_access.rb