Class: HashTable
Defined Under Namespace
Classes: HashLine
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#load_file
Methods included from Printer
#print, #write
Constructor Details
#initialize(obj = nil, opts = {}) ⇒ HashTable
Returns a new instance of HashTable.
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
# File 'lib/hash_table.rb', line 200
def initialize(obj=nil,opts={})
fix_opts(opts)
create_index
@lines = []
@preamble = []
@sleeve = {}
@comment = @opts[:comment] || self.class.
if obj && obj.is_a?(String) && File.exists?(obj)
parse_file obj
elsif obj && obj.is_a?(Array)
@lines = obj
end
end
|
Class Attribute Details
Returns the value of attribute comment.
95
96
97
|
# File 'lib/hash_table.rb', line 95
def
@comment
end
|
Instance Attribute Details
Returns the value of attribute header.
116
117
118
|
# File 'lib/hash_table.rb', line 116
def
@header
end
|
#types ⇒ Object
Returns the value of attribute types.
116
117
118
|
# File 'lib/hash_table.rb', line 116
def types
@types
end
|
Class Method Details
110
111
112
|
# File 'lib/hash_table.rb', line 110
def
@use_header = nil
end
|
107
108
109
|
# File 'lib/hash_table.rb', line 107
def
@use_header = true
end
|
.line_class(klass) ⇒ Object
100
101
102
|
# File 'lib/hash_table.rb', line 100
def line_class klass
@line_type = const_get klass.to_s.camel_case
end
|
.line_type ⇒ Object
96
97
98
|
# File 'lib/hash_table.rb', line 96
def line_type
@line_type || HashLine
end
|
104
105
106
|
# File 'lib/hash_table.rb', line 104
def
@use_header
end
|
Instance Method Details
#<<(hash) ⇒ Object
217
218
219
|
# File 'lib/hash_table.rb', line 217
def << hash
add_line hash
end
|
#[](ind) ⇒ Object
117
118
119
120
121
122
123
|
# File 'lib/hash_table.rb', line 117
def [](ind)
if ind.is_a? Range
wrap @lines[ind]
else
@lines[ind]
end
end
|
#concat(other_table) ⇒ Object
221
222
223
224
225
226
227
|
# File 'lib/hash_table.rb', line 221
def concat other_table
raise TypeError unless other_table.is_a? self.class
other_table.each do |line|
add_line line
end
self
end
|
#each ⇒ Object
194
195
196
197
198
|
# File 'lib/hash_table.rb', line 194
def each
@lines.each do |l|
yield l
end
end
|
169
170
171
172
173
|
# File 'lib/hash_table.rb', line 169
def
@header.map do |h|
@sleeve[h] || h
end.join("\t")
end
|
#idx(key, value = nil) ⇒ Object
125
126
127
|
# File 'lib/hash_table.rb', line 125
def idx key, value=nil
@wrapped_index[ [key, value] ] ||= get_wrapped_table key, value
end
|
#idx_keys(key) ⇒ Object
129
130
131
|
# File 'lib/hash_table.rb', line 129
def idx_keys(key)
@bare_index[key].keys
end
|
#inspect ⇒ Object
190
191
192
|
# File 'lib/hash_table.rb', line 190
def inspect
"#<#{self.class.name}:#{object_id} @lines=#{@lines.count}>"
end
|
#output(f) ⇒ Object
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/hash_table.rb', line 179
def output f
f.puts preamble
f.puts if
@lines.each do |l|
l = yield l if block_given?
next if !l || l.invalid?
f.puts l.to_s
end
true
end
|
#preamble ⇒ Object
175
176
177
|
# File 'lib/hash_table.rb', line 175
def preamble
@preamble
end
|
#sample(*args) ⇒ Object
145
146
147
148
149
150
151
152
|
# File 'lib/hash_table.rb', line 145
def sample *args
samp = @lines.sample *args
if samp.is_a? Array
wrap samp
else
samp
end
end
|
#select!(&block) ⇒ Object
155
156
157
158
|
# File 'lib/hash_table.rb', line 155
def select! &block
@lines.select! &block
self
end
|
#sort_by!(&block) ⇒ Object
160
161
162
163
|
# File 'lib/hash_table.rb', line 160
def sort_by! &block
@lines.sort_by! &block
self
end
|
#sum(col) ⇒ Object
133
134
135
136
137
|
# File 'lib/hash_table.rb', line 133
def sum(col)
inject(0) do |sum,line|
sum += line[col].to_f
end
end
|
#update_index(key) ⇒ Object
233
234
235
236
237
238
|
# File 'lib/hash_table.rb', line 233
def update_index key
create_index_for key
@lines.each do |line|
index_line_to_key line, key
end
end
|
165
166
167
|
# File 'lib/hash_table.rb', line 165
def
self.class.
end
|
#wrap(lines) ⇒ Object
229
230
231
|
# File 'lib/hash_table.rb', line 229
def wrap lines
self.class.new lines, @opts.merge( :header => @header.clone, :types => @types.clone )
end
|