Module: Entity

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

Defined Under Namespace

Modules: Identified

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 =~ /\(#{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.



9
10
11
# File 'lib/rbbt/entity.rb', line 9

def entity_property_cache
  @entity_property_cache
end

.formatsObject

Returns the value of attribute formats.



9
10
11
# File 'lib/rbbt/entity.rb', line 9

def formats
  @formats
end

Instance Attribute Details

#all_formatsObject

Returns the value of attribute all_formats.



74
75
76
# File 'lib/rbbt/entity.rb', line 74

def all_formats
  @all_formats
end

#all_propertiesObject

Returns the value of attribute all_properties.



74
75
76
# File 'lib/rbbt/entity.rb', line 74

def all_properties
  @all_properties
end

Class Method Details

.extended(base) ⇒ Object



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

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 marshal_dump
      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|
          if Array === self
            self.collect{|e| e.send(single_name, *args)}
          else
            self.send(single_name, *args)
          end
        end
      when :array, :array2single
        ary_name = "_ary_" << name
        define_method ary_name, &block 
        
        define_method name do |*args|
          case
          when Array === self
            self.send(ary_name, *args)
          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)
            Hash === res ? res[self] : res[0]
          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 => Entity.entity_property_cache

      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 = __method__.to_s << ":" << (Array === id ? Misc.obj2digest(id) : id)

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

        Persist.persist(persist_name, type, persist_options.merge(:persist => true)) do
          self.send(orig_name, *args)
        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