Module: Bio::NeXML::Mapper::Framework

Defined in:
lib/bio/db/nexml/mapper/framework.rb

Overview

Framework is the real workhorse of Mapper.

Instance Method Summary collapse

Instance Method Details

#belongs_to(name, options = {}) ⇒ Object

Define a source for the target.

belongs_to :source


10
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
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bio/db/nexml/mapper/framework.rb', line 10

def belongs_to( name, options = {} )
  update = ( options[ :update ] || self.name.key ).to_sym

  # create attribute reader for source
  attr_reader name

  # Add this target to a source.
  #    def add_to_source( source )
  #      return if @source == source or source.nil?
  #      @source = source
  #      source.add_target( self )
  #    end
  define_method( "add_to_#{name}" ) do |source,|
    # return if the target already belongs to
    # the given source or if the source is nil.
    return if ivget( name ) == source or source.nil?

    # add source and send a message to source
    # to add self as target.
    ivset( name, source )
    source.send( "add_#{update}", self )
  end

  # Set or remove source.
  #    def source=( source )
  #      return remove_from_source if source.nil?
  #      return add_to_source( self )
  #    end
  define_method( "#{name}=" ) do |source,|
    return send( "remove_from_#{name}" ) if source.nil?
    return send( "add_to_#{name}", source )
  end

  # Remove this target from an existing source.
  #    def remove_from_source
  #      return unless source = @source
  #      @source = nil
  #      source.delete_target( self )
  #    end
  define_method( "remove_from_#{name}" ) do
    # return if a sourc is not set.
    return unless source = ivget( name )

    # remove source and send a message to source
    # to remove itself as target.
    ivset( name, nil )
    source.send( "delete_#{update}", self )
  end
end

#has_n(target, options = {}) ⇒ Object

has_n :otu



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
# File 'lib/bio/db/nexml/mapper/framework.rb', line 62

def has_n( target, options = {} )
  name = ( options[ :singularize ] == false ) ? target.to_s : target.to_s.singular
  type = ( options[ :index ] == false ) ? ArrayRepository : HashRepository
  update = ( options[ :update ] || self.name.key ).to_sym

  # Return an Array of targets.
  #    def targets
  #      @targets.objects
  #    end
  define_method( target ) do
    repository( target, type ).objects
  end

  # Set an Array as a target.
  #    def targets=( targets )
  #      @targets.clear
  #      targets.each do |t|
  #        add_target( t )
  #      end
  #    end
  define_method( "#{target}=" ) do |objects,|
    repository( target, type ).clear
    objects.each do |o|                     
      send( "add_#{name}", o )  
    end                        
  end                         

  # Add a target.
  #    def add_target( target )
  #      return if @targets.include?( target )
  #      @targets.append( target )
  #      target.source = self
  #    end
  define_method( "add_#{name}" ) do |object,|
    repository = repository( target, type )
    return if repository.include?( object )
    repository.append( object )
    object.send( "#{update}=", self )
    self
  end

  # Delete a target.
  #    def delete_target( target )
  #      return unless @targets.include?( target )
  #      return unless deleted = @targets.delete( target )
  #      target.source = nil
  #    end
  define_method( "delete_#{name}" ) do |object,|
    repository = repository( target, type )
    return unless deleted = repository.delete( object )
    object.send( "#{update}=", nil )
    deleted
  end                                          

  # Return the number of targets a source has.
  #    def number_of_targets
  #      @targets.length
  #    end
  define_method( "number_of_#{target}" ) do
    repository( target, type ).length
  end

  # Return true if the source has the given target.
  #    def has_target?( target )
  #      @targets.include?( target )
  #    end
  define_method( "has_#{name}?" ) do |object,|
    repository( target, type ).include?( object )
  end

  # Iterate over each target.
  #    def each( &block )
  #      @targets.each( &block )
  #    end
  define_method("each_#{name}") do |&block|
    repository(target, type).each( &block )
  end

  if type == HashRepository
    define_method( "get_#{name}_by_id" ) do |id,|
      repository( target, type )[ id ]          
    end                                        

    define_method("each_#{name}_with_id") do |&block|
      repository(target, type).each_with_id(&block)
    end
  end
end