Class: HashTable

Inherits:
Object
  • Object
show all
Includes:
Enumerable, HashTableAux, Printer
Defined in:
lib/hash_table.rb

Direct Known Subclasses

GTF

Defined Under Namespace

Classes: HashLine

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HashTableAux

#load_file

Methods included from Printer

#print, #write

Constructor Details

#initialize(file, opts = {}) ⇒ HashTable

Returns a new instance of HashTable.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/hash_table.rb', line 126

def initialize(file,opts={})
  @header = opts[:header]
  @skip_header = opts[:skip_header] && opts[:header]
  if @header.is_a? Hash
    @types = @header.values
    @header = @header.keys
  end
  create_index opts[:idx]
  @lines = []
  @comment = opts[:comment]
  @types ||= opts[:types]

  parse_file(file) if file && File.exists?(file)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/hash_table.rb', line 81

def method_missing sym, *args, &block
  if @index[sym]
    @index[sym]
  else
    super sym, *args, &block
  end
end

Instance Attribute Details

#headerObject

Returns the value of attribute header.



76
77
78
# File 'lib/hash_table.rb', line 76

def header
  @header
end

Class Method Details

.header_offObject



70
71
72
# File 'lib/hash_table.rb', line 70

def header_off
  @use_header = nil
end

.header_onObject



67
68
69
# File 'lib/hash_table.rb', line 67

def header_on
  @use_header = true
end

.line_class(klass) ⇒ Object



60
61
62
# File 'lib/hash_table.rb', line 60

def line_class klass
  @line_type = const_get klass.to_s.camel_case
end

.line_typeObject



56
57
58
# File 'lib/hash_table.rb', line 56

def line_type
  @line_type || HashLine
end

.use_header?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/hash_table.rb', line 64

def use_header?
  @use_header
end

Instance Method Details

#[](ind) ⇒ Object



77
78
79
# File 'lib/hash_table.rb', line 77

def [](ind)
  @lines[ind]
end

#add_line(hash) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/hash_table.rb', line 141

def add_line hash
  if hash.is_a? HashLine
    @lines.push hash
  else
    @lines.push create_line(hash)
  end
end

#eachObject



120
121
122
123
124
# File 'lib/hash_table.rb', line 120

def each
  @lines.each do |l|
    yield l
  end
end

#inspectObject



116
117
118
# File 'lib/hash_table.rb', line 116

def inspect
  "#<#{self.class.name}:#{object_id} @lines=#{@lines.count}>"
end

#output(f) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/hash_table.rb', line 107

def output f
  f.puts @header.join("\t") if use_header?
  @lines.each do |l|
    l = yield l if block_given?
    next if !l || l.invalid?
    f.puts format_line(l)
  end
end

#select!(&block) ⇒ Object



95
96
97
# File 'lib/hash_table.rb', line 95

def select! &block
  @lines.select! &block
end

#sort_by!(&block) ⇒ Object



99
100
101
# File 'lib/hash_table.rb', line 99

def sort_by! &block
  @lines.sort_by! &block
end

#sum(col) ⇒ Object



89
90
91
92
93
# File 'lib/hash_table.rb', line 89

def sum(col)
  inject(0) do |sum,line|
    sum += line[col].to_f
  end
end

#use_header?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/hash_table.rb', line 103

def use_header?
  self.class.use_header?
end