Module: RTM::AR::Sugar::Topic::HashAccess

Included in:
Topic
Defined in:
lib/rtm/activerecord/sugar/topic/hash_access.rb

Overview

This module implements methods for Hash-like access in Topics.

Instance Method Summary collapse

Instance Method Details

#[](cref) ⇒ Object

Returns a list of names or occurrences depending on the arguments given.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rtm/activerecord/sugar/topic/hash_access.rb', line 11

def [](cref)
  # return occurrences by type if topic is given
  return occurrences.select{|o| o.type == cref} if cref.is_a? Topic
  raise "Characteristic reference must be a Topic or a String" unless cref.is_a? String
  # parse the given String to extract kind (name/occ), type and scope
  is_name, type_reference, scope_reference = resolve_characteristic(cref)
  if is_name
    type_reference = RTM::PSI[:name_type] if type_reference == :any
    ret = names.select{|n| n.type == topic_map.get(type_reference)}
  else
    raise "No occurrence type given" if type_reference == :any
    ret = occurrences.select{|o| o.type == topic_map.get(type_reference)}
  end
  # we now have a list of names or occurrences, now we have to select for scope
  return ret if scope_reference == :any
  if scope_reference == :ucs
    ret.select{|n| n.scope.size == 0}
  else
    ret.select{|n| scope_reference.all?{|sr| srt = topic_map.get(sr); srt && n.scope.include?(srt)}}
  end
end

#[]=(cref, value) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rtm/activerecord/sugar/topic/hash_access.rb', line 33

def []=(cref, value)
  is_name, type_reference, scope_reference = resolve_characteristic(cref)
  if is_name
    type_reference = nil if type_reference == :any
    nametype = type_reference || topic_map.get!(RTM::PSI[:name_type])
    ret = create_name(nametype, value)
  else
    raise "No occurrence type given" if type_reference == :any
    ret = create_occurrence(type_reference,value)
  end
  # return object without scope if any or ucs
  return ret if [:any, :ucs].include? scope_reference
  # set scope
  scope_reference.each do |sr|
    ret.scope << topic_map.get!(sr)
  end
end