Class: D8a

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/d8a/d8a.rb

Overview

  • <attr>=(d8m, val) that sets the value of the attribute. This is

optional; the attribute will be read-only if this method is not provided.

Direct Known Subclasses

FileD8a, FtpD8a

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(d8aname) ⇒ D8a

Initialization



59
60
61
62
63
# File 'lib/d8a/d8a.rb', line 59

def initialize(d8aname)
  @d8aname = d8aname
  @attrs = [:name]
  @id_attrs = []
end

Instance Attribute Details

#attrsObject (readonly)

Attrs for data



50
51
52
# File 'lib/d8a/d8a.rb', line 50

def attrs
  @attrs
end

#d8anameObject (readonly)

D8a name



46
47
48
# File 'lib/d8a/d8a.rb', line 46

def d8aname
  @d8aname
end

#id_attrsObject (readonly)

Identity attrs for data. Users of this D8a can assume that a datum has not changed if all of these attributes remain constant.



55
56
57
# File 'lib/d8a/d8a.rb', line 55

def id_attrs
  @id_attrs
end

Instance Method Details

#[](d8m, attr = nil) ⇒ Object

Returns information about a named datum.



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/d8a/d8a.rb', line 73

def [](d8m, attr = nil)
  if attr
    @attrs.include?(attr) ? __send__(attr, d8m) : nil

  else
    @attrs.inject({:name => d8m}) { |d8mattrs, attr|
      val = __send__(attr, d8m, d8mattrs)
      d8mattrs[attr] = val if val
      d8mattrs
    }
  end
end

#[]=(d8m, attr, val = nil) ⇒ Object

Sets information about a named datum.



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/d8a/d8a.rb', line 88

def []=(d8m, attr, val = nil)
  if attr.is_a?(Hash)
    attr.each { |k, v| self[d8m, k] = v }

  elsif  @attrs.include?(attr)
    begin
      __send__((attr.to_s + "=").to_sym, d8m, val)
    rescue NoMethodError
      nil
    end
  end
end

#copy(fromd8a, fromd8m, tod8a, tod8m = fromd8m) ⇒ Object

copy one datum to another



183
184
185
186
187
188
189
190
191
# File 'lib/d8a/d8a.rb', line 183

def copy(fromd8a, fromd8m, tod8a, tod8m = fromd8m)
  fromd8a.read(fromd8m) do |r|
    tod8a.write(tod8m) do |w|
      while s = r.read(65536)
        w.write(s)
      end
    end
  end
end

#delete(d8m) ⇒ Object

dummy implementation



121
122
123
# File 'lib/d8a/d8a.rb', line 121

def delete(d8m)
  raise "delete(d8m) not implemented by this D8a"
end

#diff(o) ⇒ Object

invokes block for each diff, returns entries in this not in o



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/d8a/d8a.rb', line 149

def diff(o)
  each do |d8m|
    begin
      myd8m = self[d8m]
      od8m = o[d8m]
      yield(myd8m, od8m) unless diff_d8m(myd8m, od8m, self, o).empty?
    rescue Errno::ENOENT
      yield(myd8m, nil)
    end
  end

  o.each do |d8m|
    begin
      myd8m = self[d8m]
    rescue Errno::ENOENT
      yield(nil, o[d8m])
    end
  end
end

#diff_d8m(d8m1, d8m2, d8a1, d8a2) ⇒ Object

returns the different attributes for two d8m



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/d8a/d8a.rb', line 132

def diff_d8m(d8m1, d8m2, d8a1, d8a2)
  name = (d8m1||d8m2)[:name]

  d8m1 ||= d8a1.attrs.inject({}) { |attrs,attr| attrs[attr] = nil; attrs }
  d8m2 ||= d8a2.attrs.inject({}) { |attrs,attr| attrs[attr] = nil; attrs }

  (d8m1.keys & d8m2.keys).find_all { |attr|
    begin
      d8m1[attr] != d8m2[attr]
    rescue
      true
    end
  }
end

#diffreport(o) ⇒ Object

Generates a report of differences as -> [:attr1,:attr2…]



171
172
173
174
175
176
177
178
179
# File 'lib/d8a/d8a.rb', line 171

def diffreport(o)
  result = {}

  diff(o) do |a,b|
    result[(a||b)[:name]] = diff_d8m(a, b, self, o)
  end

  result
end

#eachObject

dummy implementation



103
104
105
# File 'lib/d8a/d8a.rb', line 103

def each
  raise "each not implemented by this D8a"
end

#flushObject

flush any cached data



127
128
# File 'lib/d8a/d8a.rb', line 127

def flush
end

#name(d8m, *d8mattrs) ⇒ Object

:name attribute



67
68
69
# File 'lib/d8a/d8a.rb', line 67

def name(d8m, *d8mattrs)
  d8m
end

#read(d8m) ⇒ Object

dummy implementation



109
110
111
# File 'lib/d8a/d8a.rb', line 109

def read(d8m)
  raise "read(d8m) not implemented by this D8a"
end

#sync(to) ⇒ Object

clones this D8a to another



195
196
197
198
199
200
201
202
203
204
# File 'lib/d8a/d8a.rb', line 195

def sync(to)
  diff(to) do |myd8m,tod8m|
    if myd8m
      copy(self, myd8m[:name], to)
      to[myd8m[:name]] = myd8m
    else
      to.delete(tod8m[:name])
    end
  end
end

#write(d8m) ⇒ Object

dummy implementation



115
116
117
# File 'lib/d8a/d8a.rb', line 115

def write(d8m)
  raise "write(d8m) not implemented by this D8a"
end