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



17
18
19
20
21
22
23
24
# File 'lib/rbbt/util/named_array.rb', line 17

def self.setup(array, fields, key = nil, entity_options = nil, entity_templates = nil)
  array.extend NamedArray unless NamedArray === array
  array.fields = 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



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rbbt/util/named_array.rb', line 87

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



157
158
159
# File 'lib/rbbt/util/named_array.rb', line 157

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

#collectObject



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rbbt/util/named_array.rb', line 139

def collect
  res = []

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

  res
end

#detach(file) ⇒ Object



184
185
186
187
188
189
190
191
192
# File 'lib/rbbt/util/named_array.rb', line 184

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



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rbbt/util/named_array.rb', line 111

def 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
    super &block
  end

end

#merge(array) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rbbt/util/named_array.rb', line 53

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



66
67
68
69
70
71
72
73
74
# File 'lib/rbbt/util/named_array.rb', line 66

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



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

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)
    entity = template.annotate(entity.frozen? ? entity.dup : 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
        entity = template.annotate(entity.frozen? ? entity.dup : entity)
        entity.extend AnnotatedArray if Array === entity
        entity
      else
        entity_templates[field] = nil
        entity
      end
    end
  end
end

#reportObject



194
195
196
197
198
# File 'lib/rbbt/util/named_array.rb', line 194

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

#values_at(*keys) ⇒ Object



170
171
172
173
174
175
# File 'lib/rbbt/util/named_array.rb', line 170

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



177
178
179
180
181
182
# File 'lib/rbbt/util/named_array.rb', line 177

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