Class: Gribr::Inventory

Inherits:
Object
  • Object
show all
Defined in:
lib/gribr/inventory.rb

Direct Known Subclasses

Degrib::Inventory, Wgrib::Inventory

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records = Array.new) ⇒ Inventory

Returns a new instance of Inventory.



7
8
9
10
# File 'lib/gribr/inventory.rb', line 7

def initialize(records = Array.new)
  @records = Hash.new
  records.each { |record| add_record(record) }
end

Class Method Details

.file(file) ⇒ Object



34
35
36
# File 'lib/gribr/inventory.rb', line 34

def file(file)
  ::File.gribfile?(file) ? grib_file(file) : inventory_file(file)
end

.io(io) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gribr/inventory.rb', line 38

def io(io)

  inventory = new

  io.each_with_index do |line, index|

    if index > 0
      record = parse_record(line)
      inventory.add_record(record)
      yield(record) if block_given?
    end

  end

  inventory

end

.url(url) ⇒ Object



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

def url(url)
  open(url) { |io| io(io) }
end

Instance Method Details

#add_record(record) ⇒ Object



12
13
14
15
16
# File 'lib/gribr/inventory.rb', line 12

def add_record(record)
  set_end_position_of_previous(record)
  @records[record.number] = record
  record
end

#recordsObject



18
19
20
# File 'lib/gribr/inventory.rb', line 18

def records
  @records.values.sort_by(&:number)
end

#to_sObject



22
23
24
# File 'lib/gribr/inventory.rb', line 22

def to_s
  records.join("\n")
end