Module: Association::Index
- Defined in:
- lib/rbbt/association/index.rb
Instance Attribute Summary collapse
-
#source_field ⇒ Object
Returns the value of attribute source_field.
-
#target_field ⇒ Object
Returns the value of attribute target_field.
-
#undirected ⇒ Object
Returns the value of attribute undirected.
Class Method Summary collapse
Instance Method Summary collapse
- #match(entity) ⇒ Object
- #matches(entities) ⇒ Object
- #parse_key_field ⇒ Object
- #reverse ⇒ Object
-
#select_entities(entities) ⇒ Object
{{{ Subset.
- #subset(source, target) ⇒ Object
- #subset_entities(entities) ⇒ Object
Instance Attribute Details
#source_field ⇒ Object
Returns the value of attribute source_field.
5 6 7 |
# File 'lib/rbbt/association/index.rb', line 5 def source_field @source_field end |
#target_field ⇒ Object
Returns the value of attribute target_field.
5 6 7 |
# File 'lib/rbbt/association/index.rb', line 5 def target_field @target_field end |
#undirected ⇒ Object
Returns the value of attribute undirected.
5 6 7 |
# File 'lib/rbbt/association/index.rb', line 5 def undirected @undirected end |
Class Method Details
.setup(repo) ⇒ Object
10 11 12 13 14 |
# File 'lib/rbbt/association/index.rb', line 10 def self.setup(repo) repo.extend Association::Index repo.parse_key_field repo.unnamed = true end |
Instance Method Details
#match(entity) ⇒ Object
42 43 44 45 |
# File 'lib/rbbt/association/index.rb', line 42 def match(entity) return [] if entity.nil? prefix(entity + "~") end |
#matches(entities) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rbbt/association/index.rb', line 47 def matches(entities) entities.inject(nil) do |acc,e| m = match(e); if acc.nil? or acc.empty? acc = m else acc.concat m end acc end end |
#parse_key_field ⇒ Object
6 7 8 |
# File 'lib/rbbt/association/index.rb', line 6 def parse_key_field @source_field, @target_field, @undirected = key_field.split("~") end |
#reverse ⇒ Object
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 |
# File 'lib/rbbt/association/index.rb', line 16 def reverse @reverse ||= begin reverse_filename = persistence_path + '.reverse' if File.exists?(reverse_filename) new = Persist.open_tokyocabinet(reverse_filename, false, serializer, TokyoCabinet::BDB) else FileUtils.mkdir_p File.basename(reverse_filename) unless File.exists?(File.basename(reverse_filename)) new = Persist.open_tokyocabinet(reverse_filename, true, serializer, TokyoCabinet::BDB) new.write through do |key, value| new_key = key.split("~").reverse.join("~") new[new_key] = value end annotate(new) new.key_field = key_field.split("~").values_at(1,0,2).compact * "~" new.read end new.unnamed = true Association::Index.setup new new end end |
#select_entities(entities) ⇒ Object
{{{ Subset
61 62 63 64 65 66 67 68 69 |
# File 'lib/rbbt/association/index.rb', line 61 def select_entities(entities) source_type = Entity.formats[source_field] target_type = Entity.formats[target_field] source_entities = entities[source_field] || entities[Entity.formats[source_field].to_s] target_entities = entities[target_field] || entities[Entity.formats[target_field].to_s] [source_entities, target_entities] end |
#subset(source, target) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rbbt/association/index.rb', line 71 def subset(source, target) return [] if source.nil? or source.empty? or target.nil? or target.empty? matches = source.uniq.inject([]){|acc,e| acc.concat(match(e)) } target_matches = {} matches.each{|code| s,sep,t = code.partition "~" next if (undirected and t > s) target_matches[t] ||= [] target_matches[t] << code } target_matches.values_at(*target.uniq).flatten.compact end |
#subset_entities(entities) ⇒ Object
88 89 90 91 |
# File 'lib/rbbt/association/index.rb', line 88 def subset_entities(entities) source, target = select_entities(entities) subset source, target end |