Module: NamedArray

Defined in:
lib/rbbt/util/named_array.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#entity_optionsObject

Returns the value of attribute entity_options.



10
11
12
# File 'lib/rbbt/util/named_array.rb', line 10

def entity_options
  @entity_options
end

#entity_templatesObject

Returns the value of attribute entity_templates.



11
12
13
# File 'lib/rbbt/util/named_array.rb', line 11

def entity_templates
  @entity_templates
end

#fieldsObject

extend ChainMethods self.chain_prefix = :named_array



8
9
10
# File 'lib/rbbt/util/named_array.rb', line 8

def fields
  @fields
end

#keyObject

Returns the value of attribute key.



9
10
11
# File 'lib/rbbt/util/named_array.rb', line 9

def key
  @key
end

Class Method Details

.setup(array, fields, key = nil, entity_options = nil, entity_templates = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/rbbt/util/named_array.rb', line 22

def self.setup(array, fields, key = nil, entity_options = nil, entity_templates = nil)
  return array if array.nil?
  array.extend NamedArray unless NamedArray === array
  array.fields = Annotated.purge fields
  array.key = key
  array.entity_options = entity_options unless entity_options.nil?
  array.entity_templates = entity_templates unless entity_templates.nil?
  array
end

Instance Method Details

#[](key, clean = false) ⇒ Object

field = NamedArray === @fields ? @fields.named_array_clean_get_brackets(pos) : @fields

elem = prepare_entity(elem, field, entity_options)
elem

end



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rbbt/util/named_array.rb', line 95

def [](key, clean = false)
  pos = Misc.field_position(fields, key)
  elem = super(pos)
  return elem if clean

  return elem if @fields.nil? or @fields.empty?

  field = NamedArray === @fields ? @fields[pos, true] : @fields[pos]
  elem = prepare_entity(elem, field, entity_options)
  elem
end

#[]=(key, value) ⇒ Object

def named_array_set_brackets(key,value)

named_array_clean_set_brackets(Misc.field_position(fields, key), value)

end



168
169
170
# File 'lib/rbbt/util/named_array.rb', line 168

def []=(key, value)
  super(Misc.field_position(fields, key), value)
end

#collectObject



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rbbt/util/named_array.rb', line 150

def collect
  res = []

  each do |elem|
    if block_given?
      res << yield(elem)
    else
      res << elem
    end
  end

  res
end

#detach(file) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'lib/rbbt/util/named_array.rb', line 195

def detach(file)
  file_fields = file.fields.collect{|field| field.fullname}
  detached_fields = []
  self.fields.each_with_index{|field,i| detached_fields << i if file_fields.include? field.fullname}
  fields = self.fields.values_at *detached_fields
  values = self.values_at *detached_fields
  values = NamedArray.name(values, fields)
  values.zip_fields
end

#each(&block) ⇒ Object

def named_array_each(&block)

if defined?(Entity) and not @fields.nil? and not @fields.empty?
  @fields.zip(self).each do |field,elem|
    elem = prepare_entity(elem, field, entity_options)
    yield(elem)
    elem
  end
else
  named_array_clean_each &block
end

end



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rbbt/util/named_array.rb', line 119

def each(&block)
  if defined?(Entity) and not @fields.nil? and not @fields.empty?
    i = 0
    super do |elem|
      field = @fields[i]
      elem = prepare_entity(elem, field, entity_options)
      yield(elem)
      i += 1
      elem
    end
  else
    super &block
  end

end

#merge(array) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rbbt/util/named_array.rb', line 61

def merge(array)
  double = Array === array.first 
  new = self.dup
  (0..length - 1).each do |i|
    if double
      new[i] = new[i] + array[i]
    else
      new[i] << array[i]
    end
  end
  new
end

#positions(fields) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/rbbt/util/named_array.rb', line 74

def positions(fields)
  if Array ==  fields
    fields.collect{|field|
      Misc.field_position(@fields, field)
    }
  else
    Misc.field_position(@fields, fields)
  end
end

#prepare_entity(entity, field, options = {}) ⇒ Object



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
# File 'lib/rbbt/util/named_array.rb', line 32

def prepare_entity(entity, field, options = {})
  return entity if entity.nil?
  return entity unless defined? Entity
  template = entity_templates[field]
  entity_templates ||= {}
  if template and template.respond_to?(:annotate)
    begin entity = entity.dup if entity.frozen?;  rescue; end
    entity = template.annotate(entity)
    entity.extend AnnotatedArray if Array === entity
    entity
  else
    if entity_templates.include? field
      entity
    else
      template = Misc.prepare_entity("ENTITY_TEMPLATE", field, options)
      if template.respond_to?(:annotate)
        entity_templates[field] = template
        begin entity = entity.dup if entity.frozen?;  rescue; end
        entity = template.annotate(entity)
        entity.extend AnnotatedArray if Array === entity
        entity
      else
        entity_templates[field] = nil
        entity
      end
    end
  end
end

#reportObject



205
206
207
208
209
# File 'lib/rbbt/util/named_array.rb', line 205

def report
  fields.zip(self).collect do |field,value|
    "#{ field }: #{ Array === value ? value * "|" : value }"
  end * "\n"
end

#shiftObject



13
14
15
16
# File 'lib/rbbt/util/named_array.rb', line 13

def shift
  fields.shift
  super
end

#values_at(*keys) ⇒ Object



181
182
183
184
185
186
# File 'lib/rbbt/util/named_array.rb', line 181

def values_at(*keys)
  keys = keys.collect{|k| Misc.field_position(fields, k, true) }
  keys.collect{|k|
    self[k] unless k.nil?
  }
end

#zip_fieldsObject



188
189
190
191
192
193
# File 'lib/rbbt/util/named_array.rb', line 188

def zip_fields
  return [] if self.empty?
  zipped = Misc.zip_fields(self)
  zipped = zipped.collect{|v| NamedArray.setup(v, fields)}
  zipped 
end