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.



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

def entity_options
  @entity_options
end

#entity_templatesObject

Returns the value of attribute entity_templates.



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

def entity_templates
  @entity_templates
end

#fieldsObject

extend ChainMethods self.chain_prefix = :named_array



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

def fields
  @fields
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

Class Method Details

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



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

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 === fields ? Annotated.purge(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



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

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



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

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

#collectObject



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

def collect
  res = []

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

  res
end

#detach(file) ⇒ Object



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

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



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

def each(&block)
  if defined?(Entity) && ! (@fields.nil? || @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



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

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



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

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



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/rbbt/util/named_array.rb', line 31

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



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

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

#shiftObject



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

def shift
  fields.shift
  super
end

#to_hashObject



210
211
212
213
214
215
216
217
# File 'lib/rbbt/util/named_array.rb', line 210

def to_hash
  hash = {}
  self.fields.zip(self) do |field,value|
    hash[field] = value
  end
  IndiferentHash.setup hash
  hash
end

#values_at(*keys) ⇒ Object



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

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



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

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