Module: Entity::Identified

Defined in:
lib/scout/entity/identifiers.rb

Constant Summary collapse

NAMESPACE_TAG =
'NAMESPACE'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



11
12
13
14
15
16
17
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
# File 'lib/scout/entity/identifiers.rb', line 11

def self.included(base)
  base.annotation :format
  base.annotation :namespace

  class << base
    attr_accessor :identifier_files, :formats, :default_format, :name_format, :description_format
  end

  base.property :to => :both do |target_format|

    target_format = case target_format
                    when :name
                      identity_type.name_format 
                    when :default
                      identity_type.default_format 
                    else
                      target_format
                    end

    if target_format == format
      self
    elsif Array === self
      self.annotate(identifier_index(target_format, self.format).values_at(*self))
    else
      self.annotate(identifier_index(target_format, self.format)[self])
    end.tap{|o| o.format = target_format unless o.nil? }
  end

  base.property :name => :both do
    to(:name)
  end

  base.property :default => :both do
    to(:default)
  end
end

Instance Method Details

#identifier_filesObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/scout/entity/identifiers.rb', line 48

def identifier_files 
  files = identity_type.identifier_files.dup
  return [] if files.nil?
  files.collect!{|f| f.annotate f.gsub(/\b#{NAMESPACE_TAG}\b/, namespace.to_s) } if annotations.include? :namespace and self.namespace
  if files.select{|f| f =~ /\b#{NAMESPACE_TAG}\b/ }.any?
    Log.warn "Rejecting some identifier files for lack of 'namespace': " << files.select{|f| f =~ /\b#{NAMESPACE_TAG}\b/ } * ", "
  end
  files.reject!{|f| f =~ /\b#{NAMESPACE_TAG}\b/ } 
  files
end

#identifier_index(format = nil, source = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/scout/entity/identifiers.rb', line 63

def identifier_index(format = nil, source = nil)
  Persist.memory("Entity index #{identity_type}: #{format} (from #{source || "All"})", :persist => true, :format => format, :source => source) do
    source ||= self.respond_to?(:format)? self.format : nil

    begin
      index = TSV.translation_index(identifier_files, source, format, :persist => true)
      raise "No index from #{ Misc.fingerprint source } to #{ Misc.fingerprint format }: #{Misc.fingerprint identifier_files}" if index.nil?
      index.unnamed = true
      index
    rescue
      raise $! if source.nil?
      source = nil
      retry
    end
  end
end

#identity_typeObject



59
60
61
# File 'lib/scout/entity/identifiers.rb', line 59

def identity_type
  self.annotation_types.select{|m| m.include? Entity::Identified }.last
end