Module: Entity

Defined in:
lib/rbbt/entity.rb,
lib/rbbt/entity/identifiers.rb

Defined Under Namespace

Modules: Identified Classes: DontPersist, MultipleEntity

Constant Summary collapse

UNPERSISTED_PREFIX =
"entity_unpersisted_property_"
FORMATS =
begin
  hash = {}
  class << hash
    alias orig_include? include?

    attr_accessor :find_cache

    def find(value)
      self.find_cache ||= {}
      if self.find_cache.include? value
        self.find_cache[value]
      else
        self.find_cache[value] = begin
                                   if orig_include? value
                                     self.find_cache[value] = value
                                   else
                                     found = nil
                                     each do |k,v|
                                       if value.to_s == k.to_s
                                         found = k
                                         break
                                       elsif value.to_s =~ /\(#{Regexp.quote k}\)/
                                         found = k
                                         break
                                       end
                                     end
                                     found
                                   end
                                 end
      end
    end

    def [](value)
      res = super
      return res if res
      key = find(value)
      key ? super(key) : nil
    end

    def []=(key,value)
      self.find_cache = nil
      super
    end

    def include?(value)
      find(value) != nil
    end
  end

  hash
end

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.entity_property_cacheObject

Returns the value of attribute entity_property_cache.



18
19
20
# File 'lib/rbbt/entity.rb', line 18

def entity_property_cache
  @entity_property_cache
end

.formatsObject

Returns the value of attribute formats.



18
19
20
# File 'lib/rbbt/entity.rb', line 18

def formats
  @formats
end

Instance Attribute Details

#all_formatsObject

Returns the value of attribute all_formats.



83
84
85
# File 'lib/rbbt/entity.rb', line 83

def all_formats
  @all_formats
end

#all_propertiesObject

Returns the value of attribute all_properties.



83
84
85
# File 'lib/rbbt/entity.rb', line 83

def all_properties
  @all_properties
end

Class Method Details

.extended(base) ⇒ Object



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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/rbbt/entity.rb', line 84

def self.extended(base)
  base.extend Annotation
  Entity.formats[base.to_s] = base

  base.module_eval do
    attr_accessor :_ary_property_cache

    attr_accessor :template, :list_template, :action_template, :list_action_template, :keep_id

    def self.format=(formats)
      formats = [formats] unless Array === formats
      self.all_formats ||= []
      self.all_formats = self.all_formats.concat(formats).uniq
      formats.each do |format|
        Entity.formats[format] ||= self
      end
    end

    def _ary_property_cache
      @_ary_property_cache ||= {}
    end

    def base_entity
      self.annotation_types.select{|m| Entity === m}.last
    end

    def property(*args, &block)
      class << self; self; end.property(*args,&block)
    end

    def to_yaml(*args)
      self.clean_annotations.dup.to_yaml(*args)
    end


    def encode_with(coder)
      coder.scalar = clean_annotations
    end

    def consolidate
      self.inject(nil){|acc,e| 
        if acc.nil?
          acc = e
        else
          acc.concat e
        end
      }
    end

    def all_properties
      annotation_types.select{|m| Entity === m}.collect{|e| e.all_properties}.flatten.uniq
    end

    def self.property(name, &block)
      Log.debug "Defining property #{name} for #{self}"

      case
      when (Hash === name and name.size == 1)
        name, type = name.collect.first
      when (String === name or Symbol === name)
        type = :single
      else
        raise "Format of name ( => type) not understood: #{name.inspect}"
      end

      name = name.to_s unless String === name

      persisted_name = UNPERSISTED_PREFIX + name
      self.remove_method persisted_name if methods.include? persisted_name

      case type
      when :both
        define_method name, &block 
 
      when :single, :single2array
        single_name = "_single_" << name
        define_method single_name, &block 
        define_method name do |*args, &block|
          if Array === self
            res = self.collect{|e| e.send(single_name, *args)}
            res.first.annotate(res) if Annotated === res.first && type == :single2array
            res
          else
            self.send(single_name, *args, &block)
          end
        end
      when :array, :array2single
        ary_name = "_ary_" << name
        define_method ary_name, &block 

        define_method name do |*args, &block|
          case
          when Array === self
            self.send(ary_name, *args, &block)
          when (Array === self.container and not self.container_index.nil? and self.container.respond_to? ary_name)
            cache_code = Misc.hash2md5({:name => ary_name, :args => args})
            res = (self.container._ary_property_cache[cache_code] ||=  self.container.send(name, *args))
            if Hash === res
              res[self]
            else
              res[self.container_index]
            end
          else
            res = self.make_list.send(ary_name, *args, &block)
            Hash === res ? res[self] : res[0]
          end
        end
      when :multiple
        multi_name = "_multiple_" << name

        define_method multi_name do
          if self.instance_variable_get("@multiple_result_" + name.to_s)
            return self.instance_variable_get("@multiple_result_" + name.to_s)
          end
          raise MultipleEntity, "Entity property #{name} runs with multiple entities"
        end

        define_method name do |*args|

          if self.instance_variable_get("@multiple_result_" + name.to_s)
            return self.instance_variable_get("@multiple_result_" + name.to_s)
          end

          obj = if Array === self
                  self
                elsif self.respond_to?(:container) && Array === self.container
                  self.container
                else
                  self.make_list
                end

          missing = []
          obj.each do |e|
            begin 
              res = e.send(multi_name)
              e.instance_variable_set("@multiple_result_" + name.to_s, res)
            rescue MultipleEntity
              missing << e
            end
          end

          res = missing.any? ? block.call(missing) : nil
          case res
          when Array
            missing.zip(res).each do |o,res|
              raise "Multiple function '#{name}' result 'nil' for element '#{o}'" if res.nil?
              o.instance_variable_set("@multiple_result_" + name.to_s, res)
              o.send(multi_name)
            end
          when Hash
            res.each do |o,res|
              raise "Multiple function '#{name}' result 'nil' for element '#{o}'" if res.nil?
              o.instance_variable_set("@multiple_result_" + name.to_s, res)
              o.send(multi_name)
            end
          end

          if Array === self
            res = self.collect{|o| o.send(multi_name)}
            self.instance_variable_set("@multiple_result_" + name.to_s, res)
            res
          else
            res = self.send(multi_name)
            self.instance_variable_set("@multiple_result_" + name.to_s, res)
            res
          end
        end

      else 
        raise "Type not undestood in property: #{ type }"
      end
      @all_properties ||= []
      @all_properties << name
    end

    def self.persist(method_name, type = nil, options = {})
      type = :memory if type.nil?
      options ||= {}
      options = Misc.add_defaults options, :dir => File.join(Entity.entity_property_cache, self.to_s, method_name.to_s)

      orig_method_name = method_name
      multi_name = "_multiple_" + method_name.to_s
      single_name = "_single_" + method_name.to_s

      method_name = multi_name if self.instance_methods.include?(multi_name.to_sym)
      method_name = single_name if self.instance_methods.include?(single_name.to_sym)

      orig_name = UNPERSISTED_PREFIX + method_name.to_s
      alias_method orig_name, method_name unless self.instance_methods.include? orig_name.to_sym

      define_method method_name do |*args|
        id = self.id
        persist_name = orig_method_name.to_s + ":" << (Array === id ? Misc.obj2digest(id) : id)

        persist_options = options
        persist_options = persist_options.merge(:other => {:args => args}) if args and args.any?

        begin
          Persist.persist(persist_name, type, persist_options.merge(:persist => true)) do
            self.send(orig_name, *args)
          end
        rescue DontPersist
          $!.payload
        end
      end
    end

    def self.unpersist(method_name)
      return unless persisted? method_name
      orig_name = UNPERSISTED_PREFIX + method_name.to_s

      alias_method method_name, orig_name
      remove_method orig_name
    end

    def self.persisted?(method_name)
      orig_name = UNPERSISTED_PREFIX + method_name.to_s
      instance_methods.include? orig_name.to_sym
    end

    def self.with_persisted(method_name)
      persisted = persisted? method_name
      persist method_name unless persisted
      res = yield
      unpersist method_name unless persisted
      res
    end

  end 
end

.identifier_files(field) ⇒ Object



2
3
4
5
6
# File 'lib/rbbt/entity/identifiers.rb', line 2

def self.identifier_files(field)
  entity_type = Entity.formats[field]
  return [] unless entity_type and entity_type.include? Entity::Identified 
  entity_type.identifier_files
end

Instance Method Details

#add_identifiers(file, default = nil, name = nil, description = nil) ⇒ Object



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
# File 'lib/rbbt/entity/identifiers.rb', line 89

def add_identifiers(file, default = nil, name = nil, description = nil)
  if TSV === file
    all_fields = file.all_fields
  else
    if file =~ /NAMESPACE/
      all_fields = file.sub(/NAMESPACE/,'**').glob.collect do |f|
      TSV.parse_header(f).all_fields
      end.flatten.compact.uniq
    else
      all_fields = TSV.parse_header(file).all_fields
    end
  end

  self.send(:include, Entity::Identified) unless Entity::Identified === self

  self.format = all_fields
  @formats ||= []
  @formats.concat all_fields
  @formats.uniq!

  @default_format = default if default
  @name_format = name if name
  @description_format = description if description

  @identifier_files ||= []
  @identifier_files << file
  @identifier_files.uniq!
end