Module: NamedArray
- Extended by:
- Annotation
- Defined in:
- lib/scout/named_array.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Annotation
extended, is_annotated?, purge, setup
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
154
155
156
157
158
159
160
|
# File 'lib/scout/named_array.rb', line 154
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
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/scout/named_array.rb', line 113
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
146
147
148
149
150
151
152
|
# File 'lib/scout/named_array.rb', line 146
def self.add_zipped(source, new)
source.zip(new).each do |s,n|
next if n.nil?
s.concat(n)
end
source
end
|
.field_match(field, name) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/scout/named_array.rb', line 10
def self.field_match(field, name)
if (String === field) && (String === name)
return true if field == name
return true if field.include?("(" + name + ")")
return true if name.include?("(" + field + ")")
return true if field.start_with?(name + " ")
return true if name.start_with?(field + " ")
else
field == name
end
end
|
.identify_name(names, selected, strict: false) ⇒ Object
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
52
53
54
55
56
57
|
# File 'lib/scout/named_array.rb', line 22
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/scout/named_array.rb', line 127
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
73
74
75
76
77
|
# File 'lib/scout/named_array.rb', line 73
def [](key)
pos = NamedArray.identify_name(@fields, key)
return nil if pos.nil?
super(pos)
end
|
#[]=(key, value) ⇒ Object
79
80
81
82
83
|
# File 'lib/scout/named_array.rb', line 79
def []=(key, value)
pos = NamedArray.identify_name(@fields, key)
return nil if pos.nil?
super(pos, value)
end
|
#all_fields ⇒ Object
6
7
8
|
# File 'lib/scout/named_array.rb', line 6
def all_fields
[key, fields].compact.flatten
end
|
#concat(other) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/scout/named_array.rb', line 86
def concat(other)
if Hash === other
new_fields = []
other.each do |k,v|
new_fields << k
self << v
end
self.fields.concat(new_fields)
else
super(other)
self.fields.concat(other.fields) if NamedArray === other
self
end
end
|
#identify_name(selected) ⇒ Object
59
60
61
|
# File 'lib/scout/named_array.rb', line 59
def identify_name(selected)
NamedArray.identify_name(fields, selected)
end
|
#positions(fields) ⇒ Object
63
64
65
66
67
68
69
70
71
|
# File 'lib/scout/named_array.rb', line 63
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
101
102
103
104
105
106
107
|
# File 'lib/scout/named_array.rb', line 101
def to_hash
hash = {}
self.fields.each do |field|
hash[field] = self[field]
end
IndiferentHash.setup hash
end
|
#values_at(*positions) ⇒ Object
109
110
111
|
# File 'lib/scout/named_array.rb', line 109
def values_at(*positions)
super(*identify_name(positions))
end
|