Class: HiveMeta::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/hivemeta/record.rb

Instance Method Summary collapse

Constructor Details

#initialize(line, table, opts = {}) ⇒ Record

Returns a new instance of Record.



6
7
8
9
10
11
12
13
# File 'lib/hivemeta/record.rb', line 6

def initialize(line, table, opts = {})
  @fields = line.chomp.split(table.delimiter, -1)
  if @fields.size != table.columns.size
    raise FieldCountError if not opts[:ignore_field_count]
  end

  @table = table
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args) ⇒ Object

allow for column access via column name as a method example: rec.col_name



36
37
38
39
40
# File 'lib/hivemeta/record.rb', line 36

def method_missing(id, *args)
  return @fields[@table.indexes[id]]
rescue
  raise NoMethodError
end

Instance Method Details

#[](index) ⇒ Object

allow for column access via column name as an index example: rec

or: rec['col_name']

can also use the numeric index as stored in the file example: rec



29
30
31
32
# File 'lib/hivemeta/record.rb', line 29

def [] index
  return "#{@fields[index]}" if index.is_a? Integer
  "#{@fields[@table.indexes[index.to_sym]]}"
end

#_countObject Also known as: _size, _length

avoid collisions with column name ‘count’ ugly :(



17
18
19
# File 'lib/hivemeta/record.rb', line 17

def _count
  @fields.count
end