Class: RTM::AR::TMDelegator

Inherits:
Object
  • Object
show all
Defined in:
lib/rtm/activerecord/tm_delegator.rb

Direct Known Subclasses

Construct, Locator, TMSetDelegator

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ TMDelegator

Returns a new instance of TMDelegator.



6
7
8
# File 'lib/rtm/activerecord/tm_delegator.rb', line 6

def initialize(obj)
  @obj = obj
end

Class Method Details

.aka_property(prop, aka) ⇒ Object



353
354
355
356
357
358
359
360
361
# File 'lib/rtm/activerecord/tm_delegator.rb', line 353

def self.aka_property(prop, aka)
  aka = [aka].flatten
  aka.each do |a|
    #puts "generating alias #{a} for #{prop}"
    module_eval(<<-EOS, "(__AR_DELEGATOR_AKA_PROPERTY__)", 1)
      alias :#{a} :#{prop}
    EOS
  end
end

.class_delegate(sym, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rtm/activerecord/tm_delegator.rb', line 53

def self.class_delegate(sym, options={})
  module_eval(<<-EOS, "(__TMDELEGATOR__)", 1)
    class << self
      def #{sym}(*args, &block)
        __getobj__.send(:#{sym}, *args, &block)
      end
    end
  EOS

  if options[:aka]
    options[:aka] = [options[:aka]] unless options[:aka].respond_to? :each
    options[:aka].each do |aka|
      module_eval(<<-EOS, "(__TMDELEGATOR2__)", 1)
        class << self
          alias :#{aka} :#{sym}
        end
      EOS
    end
  end
end

.delegate(sym, options = {}) ⇒ Object



18
19
20
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
# File 'lib/rtm/activerecord/tm_delegator.rb', line 18

def self.delegate(sym, options={})
  to = options[:to] || sym
  options[:reload] ||= sym==:remove ? true : false # dirty hack to get a bit of reloading after delete
  module_eval(<<-EOS, "(__TMDELEGATOR__)", 1)
    def #{sym}(*args, &block)
      __getobj__.send(:#{to}, *args, &block)
     #{options[:reload] ? "parent.__getobj__.reload" :""}
    end
  EOS

  if options[:rw]
    module_eval(<<-EOS, "(__TMDELEGATOR2__)", 1)
      def #{sym}=(*args, &block)
        __getobj__.send(:#{to}=, *args, &block)
        #{options[:save] ? "__getobj__.save" : "" }
      end
    EOS
  end

  if options[:aka]
    options[:aka] = [options[:aka]] unless options[:aka].respond_to? :each
    options[:aka].each do |aka|
      module_eval(<<-EOS, "(__TMDELEGATOR3__)", 1)
        alias :#{aka} :#{sym}
      EOS

      if options[:rw]
        module_eval(<<-EOS, "(__TMDELEGATOR3__)", 1)
          alias :#{aka}= :#{sym}=
        EOS
      end
    end
  end
end

.equality(eqfields, options = {}) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/rtm/activerecord/tm_delegator.rb', line 363

def self.equality(eqfields, options={})
  field_condition = eqfields.map {|f| "self.#{f} == o.#{f}" }.join(" && ")
  module_eval(<<-EOS, "(__AR_EQUALITY__)", 1)
    def ==(o)
      #puts "hoho: #{"#"}{self.inspect} vs. #{"#"}{o.inspect}"
      return false unless o
      #{eqfields.map{|x| "#puts '#{x}'; puts self.#{x}.inspect;puts o.#{x}.inspect\n"}}
      # puts '#{field_condition}'
      return true if #{field_condition}
      false
    end
  EOS
end

.parent(sym, options = {}) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/rtm/activerecord/tm_delegator.rb', line 74

def self.parent(sym, options={})
  module_eval(<<-EOS, "(__TMDELEGATOR__)", 1)
    def #{sym}
      Construct.wrap(__getobj__.send(:#{sym}))
    end
  EOS
  aka_property(sym, [:parent, :p])
end

.property(prop, options = {}) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/rtm/activerecord/tm_delegator.rb', line 312

def self.property(prop, options={})
  module_eval(<<-EOS, "(__AR_DELEGATOR_PROPERTY__)", 1)
    def #{prop}
      #{options[:wrap]? "#{options[:type]}.wrap":""}( __getobj__.#{prop})
    end
  EOS

  if options[:rw]
    case options[:type]
    when :Topic
      module_eval(<<-EOS, "(__AR_DELEGATOR_PROPERTY2__)", 1)
        def #{prop}=(obj)
          obj = topic_map.topic_by_locator!(obj) unless obj.is_a? #{options[:type]}
          __getobj__.#{prop} = obj.__getobj__
          __getobj__.save
        end
      EOS
    when :TopicMap
      module_eval(<<-EOS, "(__AR_DELEGATOR_PROPERTY2__)", 1)
        def #{prop}=(obj)
          obj = RTM.create obj.to_s unless obj.is_a? #{options[:type]}
          __getobj__.#{prop} = obj.__getobj__
          __getobj__.save
        end
      EOS
    when :String, :Locator
      module_eval(<<-EOS, "(__AR_DELEGATOR_PROPERTY2__)", 1)
        def #{prop}=(obj)
          __getobj__.#{prop} = obj.to_s
          __getobj__.save
        end
      EOS
    else
      raise "Don't know how to do wrapping for #{options[:type]}"
    end
    
  end

  aka_property(prop, options[:aka]) if options[:aka]
end

.property_set(prop, options = {}) ⇒ Object



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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
259
260
261
262
263
264
# File 'lib/rtm/activerecord/tm_delegator.rb', line 83

def self.property_set(prop, options={})
#      puts "In #{self.name.to_s.ljust(20)} we have type #{options[:type].to_s.ljust(20)} for property #{prop}"
  if options[:wrap]
    #puts "#{self}: #{options[:type]}"
    module_eval(<<-EOS, "(__AR_DELEGATOR_PROPERTY_SET_W__)", 1)          
      def #{prop}(*args)
       if args.size == 0
         # fetch normal
         #{options[:type]}s.wrap(__getobj__.#{prop}, self, :#{options[:type]})
       else
         # fetch with filter
         # see if we have a custom filtering method
         if self.respond_to?(:filter_#{prop})
           res = filter_#{prop}(*args) # tries a custom query, returns false if it doesn't know how to handle args
           return res if res # return if successful, go on otherwise
         end
         # no custom filtering, use regular one
         #puts args.inspect
         # TODO enhance/fix condition transform code
         a1 = args.first
         unless a1.is_a?(Hash)
           a1 = topic_map.get(a1) || {}
         end
         a = {}.merge(a1)
         if a[:type] && !a[:ttype]
           a[:ttype] = a[:type]
           a.delete(:type)
         end
         # puts "#{"#"}{self.class}:#{prop} -- #{"#"}{args.first.inspect}"
         # puts
         # puts a.inspect
         b = {}
         a.each do |k,v|
           if RTM::AR::TMDM::#{options[:type]}.columns.map{|c| c.name}.include?(k)
             puts "#{options[:type]} hat spalte: #{"#"}{k}"
           else
             puts "#{options[:type]} hat NICHT spalte: #{"#"}{k}"
             b[k] = v
             a.delete(k)
           end
         end
         
         a.each do |k,v|
           if v.respond_to?(:__getobj__)
             a[(k.to_s + "_id").to_sym] = v.id
           end
         end
         res = #{options[:type]}s.wrap(__getobj__.#{prop}.find(:all, :conditions=> a), self, :#{options[:type]})
         if args.size == 1
           a1 = args.first
           res = res.select{|t| t}
         end
         res
       end
      end
EOS
  else
    module_eval(<<-EOS, "(__AR_DELEGATOR_PROPERTY_SET_NW__)", 1)          
      def #{prop}
       __getobj__.#{prop}
      end
    EOS
  end

  if options[:create]
    # some :create_args:
    # 
    # create_locator reference:string
    # [ :name => :reference, :type => :String]
    #
    # create_name value:String, scope:Collection
    # create_name value:String, type: Topic, scope:Collection
    # [ {:name => :value, :type => :String}, {:name => :type, :type => :Topic, :optional => true}, {:name => :scope, :type => :Collection} ]
    #
    # create_occurrence value:String, type: Topic, scope:Collection
    # create_occurrence resource: Locator, type: Topic, scope:Collection
    # [ {:name => :value, :type => [:String, :Locator]}, {:name => :type, :type => :Topic}, {:name => :scope, :type => :Collection} ]
    #
    # create_association_role player:topic, type: topic
    # [ {:name => :player, :type => :Topic}, {:name => :type, :type => :Topic}]
    #
    # create_variant value:string, :scope:Collection
    # create_variant resource:locator, :scope:Collection
    # [ {:name => :value, :type => [:String, :Locator]}, {:name => :scope, :type => :Collection}]

    module_eval(<<-EOS, "(__AR_DELEGATOR_PROPERTY_SET_CREATE__)", 1)
      def create_#{ options[:create]}(*args, &block)
        #puts "#{ options[:create]}"
        #puts args.inspect
        arg_defs = #{ (options[:create_args] || nil ).inspect}

        a = parse_args(args, arg_defs)
        #raise "could not parse parameters:" + a.inspect + " from " + args.inspect if a.empty?

        a = enhance_args_hash(a, arg_defs) if arg_defs

        a = a.reject{|k,v| v.nil?}

        # hack to change :type to :ttype for AR backend
        # puts a.inspect unless a.is_a? Hash
        if a[:type] && !a[:ttype]
          a[:ttype] = a[:type]
          a.delete(:type)
        end
        if [:Locator,:ItemIdentifier, :SubjectIdentifier, :SubjectLocator].include?(:#{options[:type]})
          unless a[:topic_map]
            a[:topic_map] = topic_map.__getobj__
          end
        end

        if block_given?
#              if a.respond_to?(:__getobj__)
#                a = a.__getobj__  
#              end
          obj = #{options[:type]}.wrap( __getobj__.#{prop}.new(a) )
          yield obj
          obj.save
        else
#              if a.respond_to?(:__getobj__)
#                a = a.__getobj__  
#              end
#              raise a.inspect unless a.is_a?(RTM::AR::TMDM::Topic)
          obj = #{options[:type]}.wrap( __getobj__.#{prop}.create(a) )
        end
        self.reload
        obj
      end
    EOS

    if options[:create_aka]
      aka_property("create_#{options[:create]}", options[:create_aka])
    end
  end
  
  if options[:add] && options[:add] == :scope
    module_eval(<<-RUBY, "(__AR_DELEGATOR_PROPERTY_SET_ADD__)", 1)
      def add_scope(theme)
        if theme.is_a?(Array)
          theme.each {|s| add_scope(s)}
        end
        scope.add(theme.__getobj__)
      end
RUBY
#       elsif options[:add] && (options[:add] == :subject_identifier || options[:add] == :subject_locator)
#         module_eval(<<-RUBY, "(__AR_DELEGATOR_PROPERTY_SET_ADD__)", 1)
#           def add_#{prop.to_s.singularize}(*args, &block)
#             puts "adding something to #{prop}: #{"#"}{args.inspect}"
#             obj.#{prop}.add(*args, &block)
#           end
# RUBY
#         aka_property("add_#{prop.to_s.singularize}", options[:aka].map{|a| "add_#{a}"})
#         aka_property("add_#{prop.to_s.singularize}", options[:aka].map{|a| "a#{a}"})
  elsif options[:add]
    module_eval(<<-RUBY, "(__AR_DELEGATOR_PROPERTY_SET_ADD__)", 1)
      def add_#{prop.to_s.singularize}(*args, &block)
        #{prop}.add(*args, &block)
      end
RUBY
    aka_property("add_#{prop.to_s.singularize}", options[:aka].map{|a| "add_#{a}"})
    aka_property("add_#{prop.to_s.singularize}", options[:aka].map{|a| "a#{a}"})
  end

  if options[:remove] && options[:remove] == :scope
    module_eval(<<-RUBY, "(__AR_DELEGATOR_PROPERTY_SET_REMOVE__)", 1)
      def remove_scope(theme)
        scope.remove(theme)
      end
RUBY
  elsif options[:remove]
    module_eval(<<-RUBY, "(__AR_DELEGATOR_PROPERTY_SET_REMOVE__)", 1)
      def remove_#{prop.to_s.singularize}(*args, &block)
        #{prop}.remove(*args, &block)
      end
RUBY
    aka_property("remove_#{prop.to_s.singularize}", options[:aka].map{|a| "remove_#{a}"})
    aka_property("remove_#{prop.to_s.singularize}", options[:aka].map{|a| "r#{a}"})
  end

  # TODO: aliases for property_set.add as add_property

  aka_property(prop, options[:aka]) if options[:aka]
end

.wrapper_cacheObject



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/rtm/activerecord/tm_delegator.rb', line 385

def self.wrapper_cache
  module_eval(<<-EOS, "(__AR_WRAPPER_CACHE__)", 1)
    def self.wrap(obj)
      return nil unless obj
      raise "Double wrapping" if obj.respond_to?(:__getobj__)
      t = self.wrapped(obj)
      if t
        t.__setobj__(obj)
        return t
      end
      self.new(obj)
    end

    def self.wrapped(unwrapped_obj)
      @@wrapped ||= {}
      return @@wrapped[unwrapped_obj.id] if unwrapped_obj.respond_to? :id
      @@wrapped[unwrapped_obj]
    end
    def self.reset_wrapped
      @@wrapped = {}
    end
    def initialize(*args)
      super
      @@wrapped ||= {}
      @@wrapped[self.id]=self
    end
  EOS
end

Instance Method Details

#__getobj__Object



10
11
12
# File 'lib/rtm/activerecord/tm_delegator.rb', line 10

def __getobj__
  @obj
end

#__setobj__(obj) ⇒ Object



14
15
16
# File 'lib/rtm/activerecord/tm_delegator.rb', line 14

def __setobj__(obj)
  @obj = obj
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


376
377
378
379
380
# File 'lib/rtm/activerecord/tm_delegator.rb', line 376

def eql?(o)
  return false unless o
  return true if self.class == o.class && self.id == o.id
  false
end

#hashObject



381
382
383
# File 'lib/rtm/activerecord/tm_delegator.rb', line 381

def hash
  return self.id.hash
end