Module: Entity

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

Defined Under Namespace

Modules: Identified

Constant Summary collapse

UNPERSISTED_PREFIX =
"entity_unpersisted_property_"

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.



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

def all_formats
  @all_formats
end

Class Method Details

.extended(base) ⇒ Object



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

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 self.property(name, &block)
      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
    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.hash2md5(:id => id) : id)
        persist_name << ":" << Misc.hash2md5({:args => args}) unless args.nil? or args.empty?

        persist_options = options
        persist_options = persist_options.merge(:other => {:args => args}) if 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



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

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.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