Class: RTM::AR::TMSetDelegator

Inherits:
TMDelegator show all
Includes:
Enumerable
Defined in:
lib/rtm/activerecord/tm_set_delegator.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TMDelegator

#__getobj__, #__setobj__, aka_property, class_delegate, delegate, #eql?, equality, #hash, parent, property, property_set, wrapper_cache

Constructor Details

#initialize(obj, parent, type) ⇒ TMSetDelegator

attr_reader :content_class_name



8
9
10
11
12
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 8

def initialize(obj,parent,type)
  @obj = obj
  @parent = parent
  @type = type
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



198
199
200
201
202
203
204
205
206
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 198

def method_missing(method_name, *args)
  if @obj.size > 0 && first.respond_to?(method_name) && (![:__getobj__, :__setobj__].include?(method_name))
    a = []
    inject(a) {|all,single| all << single.send(method_name, *args)}
    a
  else
    old_method_missing(method_name, *args)
  end
end

Class Method Details

.wrap(obj, parent = nil, type = nil) ⇒ Object

This class method wraps a Set completely while the instance method wraps one single contained object



15
16
17
18
19
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 15

def self.wrap(obj, parent=nil, type=nil)
  return nil unless obj
  raise "Double wrapping" if obj.respond_to?(:__getobj__)
  self.new(obj, parent, type)
end

Instance Method Details

#&(other) ⇒ Object



193
194
195
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 193

def &(other)
  @obj.to_a & other.to_a
end

#==(x) ⇒ Object



217
218
219
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 217

def ==(x)
  self.to_a == x.to_a
end

#[](i) ⇒ Object



175
176
177
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 175

def [](i)
  wrap(@obj[i])
end

#add(obj) ⇒ Object Also known as: <<



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 21

def add(obj)
  return unless obj

  old = @obj.detect { |x| x == obj } # can't that be done easier?
  if old
    old.merge obj
  else
    if obj.respond_to? :__getobj__
      @obj << obj.__getobj__
    else
      case @type
      when :Topic
        @obj << @parent.topic_map.get!(obj).__getobj__
        @parent.__getobj__.reload

      when :ItemIdentifier
        if @parent.class.name.to_s =~ /::Topic$/
          tbi = @parent.topic_map._item_identifier(obj)
          if tbi && tmc=tbi.construct
            if tmc.class.name =~ /::Topic$/
              return @parent if tmc == @parent.__getobj__
              result = Topic.wrap(tmc).merge @parent
              return result
            else
              raise "Duplicate Item Identifier"
            end
          end
          tbsi = @parent.topic_map._subject_identifier(obj.to_s)
          if tbsi
            if t=tbsi.topic
              return @parent if t == @parent.__getobj__
              result = Topic.wrap(t).merge @parent
              # after merging, we still need to add the II
              result.__getobj__.item_identifiers.create(:reference => obj.to_s, :topic_map_id => @parent.topic_map.__getobj__.id)
              return result
            end
          end
        end
        result = @obj << @parent.topic_map._item_identifier!(obj.to_s)
        return result

      when :SubjectIdentifier
        # check for existing item identifier
        # puts
        # puts "trying to add SI to a topic"
        tbi = @parent.topic_map._item_identifier(obj)
        if tbi && tmc=tbi.construct
          # puts "something with this II exists"
          if tmc.class.name =~ /::Topic$/
            # puts "it exists as a topic"
            # puts tmc == @parent.__getobj__
            return @parent if tmc == @parent.__getobj__
            
            # puts "trying to merge"
            # puts "BEFORE"
            begin
            result = Topic.wrap(tmc).merge(@parent)
            rescue Exception => e
              puts e.inspect
              raise e
            end
            # puts "AFTER"
            # puts "done merging, now adding SI"
            
            #result = @parent.merge(Topic.wrap(tmc))
            # after merging, we still need to add the SI
            result.__getobj__.subject_identifiers.create(:reference => obj.to_s, :topic_map_id => @parent.topic_map.__getobj__.id)
            puts "done adding SI, now reloading"
            result.reload
            puts "done reloading"
            return result
          else
            warn("This subject identifier IRI already belongs to another topic map construct (not a topic)")
          end
        end
        # puts "checking for existing SI"
        # check for existing subject identifier
        tbsi = @parent.topic_map._subject_identifier(obj.to_s)
        if tbsi
          puts "SI exists"
          if true && t=tbsi.topic #the single = is intentional, the "true &&" just makes netbeans not raise a warning
            return @parent if t == @parent.__getobj__
            result = Topic.wrap(t).merge @parent
            return result
          end
        end
        @obj.create(:reference => obj.to_s, :topic_map_id => @parent.topic_map.__getobj__.id)

      when :SubjectLocator
        tbsl = @parent.topic_map._subject_locator(obj.to_s)
        if tbsl
          if true && t=tbsl.topic #the single = is intentional, the "true &&" just makes netbeans not raise a warning
            return @parent if t == @parent.__getobj__
            result = Topic.wrap(t).merge @parent
            return result
          end
        end
        @obj.create(:reference => obj.to_s, :topic_map_id => @parent.topic_map.__getobj__.id)

      end
    end
  end
end

#add_all(objs) ⇒ Object



126
127
128
129
130
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 126

def add_all(objs)
  return unless objs
  objs.each {|obj| add(obj)}
  true
end

#content_classObject



179
180
181
182
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 179

def content_class
  #      @content_class ||= RTM.const_get(@content_class_name)
  @content_class ||= RTM.const_get("#{@content_class_name}MemImpl")
end

#delete(obj) ⇒ Object Also known as: remove



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 145

def delete(obj)
  obj = obj.__getobj__ if obj.respond_to? :__getobj__
  case @type
  when :ItemIdentifier, :SubjectIdentifier, :SubjectLocator
    obj = @obj.find_by_reference(@parent.topic_map.resolve(obj.to_s)) if obj.is_a? String
  when :Topic
    obj = @parent.topic_map.get!(obj).__getobj__
  end
  @obj.delete(obj)
  # item_identifiers: remove also from topicMap
  #removed_event obj if respond_to? :removed_event
end

#each(&b) ⇒ Object



132
133
134
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 132

def each(&b)
  @obj.each { |e| yield wrap(e)}
end

#empty?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 141

def empty?
  @obj.empty?
end

#find(*args) ⇒ Object



184
185
186
187
188
189
190
191
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 184

def find(*args)
  res = @obj.find(*args)
  if res.respond_to? :each
    Constructs.wrap(res)
  else
    Construct.wrap(res)
  end
end

#firstObject



165
166
167
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 165

def first
  wrap(@obj.entries.first)
end

#include?(obj) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 159

def include?(obj)
  return @obj.include?(obj.__getobj__)
  #@obj.each { |e| return true if e == obj } # T#ODO support for get
  #false
end

#lastObject



168
169
170
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 168

def last
  wrap(@obj.entries.last)
end

#old_method_missingObject



197
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 197

alias :old_method_missing :method_missing

#old_respond_to?Object



208
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 208

alias :old_respond_to? :respond_to?

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


209
210
211
212
213
214
215
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 209

def respond_to?(method_name)
  resp = old_respond_to?(method_name)
  return resp if resp # i.e. if true
  return false if [:__getobj__, :__setobj__].include?(method_name)
  # ... and ask first child otherwise
  @obj.size > 0 && first.respond_to?(method_name)
end

#sizeObject Also known as: length



136
137
138
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 136

def size
  @obj.size
end

#to_sObject



172
173
174
# File 'lib/rtm/activerecord/tm_set_delegator.rb', line 172

def to_s
  "[#{@obj.entries.map { |e| wrap(e).to_s }.join(", ") }]"
end