Module: NamedArray
Class Method Summary
collapse
Instance Method Summary
collapse
extended, is_extended?, purge
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
131
132
133
134
135
136
137
|
# File 'lib/scout/named_array.rb', line 131
def method_missing(name, *args)
if identify_name(name)
return self[name]
else
return super(name, *args)
end
end
|
Class Method Details
._zip_fields(array, max = nil) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/scout/named_array.rb', line 91
def self._zip_fields(array, max = nil)
return [] if array.nil? or array.empty? or (first = array.first).nil?
max = array.collect{|l| l.length }.max if max.nil?
rest = array[1..-1].collect{|v|
v.length == 1 & max > 1 ? v * max : v
}
first = first * max if first.length == 1 and max > 1
first.zip(*rest)
end
|
.add_zipped(source, new) ⇒ Object
124
125
126
127
128
129
|
# File 'lib/scout/named_array.rb', line 124
def self.add_zipped(source, new)
source.zip(new).each do |s,n|
s.concat(n)
end
source
end
|
.field_match(field, name) ⇒ Object
6
7
8
9
10
11
12
13
14
|
# File 'lib/scout/named_array.rb', line 6
def self.field_match(field, name)
if (String === field) && (String === name)
field == name ||
field.start_with?(name) || field.include?("(" + name + ")") ||
name.start_with?(field) || name.include?("(" + field + ")")
else
field == name
end
end
|
.identify_name(names, selected, strict: false) ⇒ Object
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
|
# File 'lib/scout/named_array.rb', line 16
def self.identify_name(names, selected, strict: false)
res = (Array === selected ? selected : [selected]).collect do |field|
case field
when nil
0
when Range
field
when Integer
field
when Symbol
field == :key ? field : identify_name(names, field.to_s)
when (names.nil? and String)
if field =~ /^\d+$/
identify_field(key_field, fields, field.to_i)
else
raise "No name information available and specified name not numeric: #{ field }"
end
when Symbol
names.index{|f| f.to_s == field.to_s }
when String
pos = names.index{|f| f.to_s == field }
next pos if pos
if field =~ /^\d+$/
next identify_names(names, field.to_i)
end
next pos if strict
pos = names.index{|name| field_match(field, name) }
next pos if pos
nil
else
raise "Field '#{ Log.fingerprint field }' was not understood. Options: (#{ Log.fingerprint names })"
end
end
Array === selected ? res : res.first
end
|
.zip_fields(array) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/scout/named_array.rb', line 105
def self.zip_fields(array)
if array.length < 10000
_zip_fields(array)
else
zipped_slices = []
max = array.collect{|l| l.length}.max
array.each_slice(10000) do |slice|
zipped_slices << _zip_fields(slice, max)
end
new = zipped_slices.first
zipped_slices[1..-1].each do |rest|
rest.each_with_index do |list,i|
new[i].concat list
end
end
new
end
end
|
Instance Method Details
#[](key) ⇒ Object
67
68
69
70
71
|
# File 'lib/scout/named_array.rb', line 67
def [](key)
pos = NamedArray.identify_name(@fields, key)
return nil if pos.nil?
super(pos)
end
|
#concat(other) ⇒ Object
73
74
75
76
77
|
# File 'lib/scout/named_array.rb', line 73
def concat(other)
super(other)
self.fields.concat(other.fields) if NamedArray === other
self
end
|
#identify_name(selected) ⇒ Object
53
54
55
|
# File 'lib/scout/named_array.rb', line 53
def identify_name(selected)
NamedArray.identify_name(fields, selected)
end
|
#positions(fields) ⇒ Object
57
58
59
60
61
62
63
64
65
|
# File 'lib/scout/named_array.rb', line 57
def positions(fields)
if Array == fields
fields.collect{|field|
NamedArray.identify_name(@fields, field)
}
else
NamedArray.identify_name(@fields, fields)
end
end
|
#to_hash ⇒ Object
79
80
81
82
83
84
85
|
# File 'lib/scout/named_array.rb', line 79
def to_hash
hash = {}
self.fields.zip(self) do |field,value|
hash[field] = value
end
IndiferentHash.setup hash
end
|
#values_at(*positions) ⇒ Object
87
88
89
|
# File 'lib/scout/named_array.rb', line 87
def values_at(*positions)
super(*identify_name(positions))
end
|